uni-grid.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <view class="uni-grid-wrap">
  3. <view :id="elId" ref="uni-grid" class="uni-grid" :class="{ 'uni-grid--border': showBorder }" :style="{ 'border-left-style':'solid','border-left-color':borderColor, 'border-left-width':showBorder?'1px':0 }">
  4. <slot />
  5. </view>
  6. </view>
  7. </template>
  8. <script>
  9. // #ifdef APP-NVUE
  10. const dom = uni.requireNativePlugin('dom');
  11. // #endif
  12. /**
  13. * Grid 宫格
  14. * @description 宫格组件
  15. * @tutorial https://ext.dcloud.net.cn/plugin?id=27
  16. * @property {Number} column 每列显示个数
  17. * @property {String} borderColor 边框颜色
  18. * @property {Boolean} showBorder 是否显示边框
  19. * @property {Boolean} square 是否方形显示
  20. * @property {Boolean} Boolean 点击背景是否高亮
  21. * @event {Function} change 点击 grid 触发,e={detail:{index:0}},index 为当前点击 gird 下标
  22. */
  23. export default {
  24. name: 'UniGrid',
  25. props: {
  26. // 每列显示个数
  27. column: {
  28. type: Number,
  29. default: 3
  30. },
  31. // 是否显示边框
  32. showBorder: {
  33. type: Boolean,
  34. default: true
  35. },
  36. // 边框颜色
  37. borderColor: {
  38. type: String,
  39. default: '#e5e5e5'
  40. },
  41. // 是否正方形显示,默认为 true
  42. square: {
  43. type: Boolean,
  44. default: true
  45. },
  46. highlight: {
  47. type: Boolean,
  48. default: true
  49. }
  50. },
  51. provide() {
  52. return {
  53. grid: this
  54. }
  55. },
  56. data() {
  57. const elId = `Uni_${Math.ceil(Math.random() * 10e5).toString(36)}`
  58. return {
  59. elId,
  60. width: 0
  61. }
  62. },
  63. created() {
  64. this.children = []
  65. },
  66. mounted() {
  67. this.init()
  68. },
  69. methods: {
  70. init() {
  71. setTimeout(() => {
  72. this._getSize((width) => {
  73. this.children.forEach((item, index) => {
  74. item.width = width
  75. })
  76. })
  77. }, 50)
  78. },
  79. change(e) {
  80. this.$emit('change', e)
  81. },
  82. _getSize(fn) {
  83. // #ifndef APP-NVUE
  84. uni.createSelectorQuery()
  85. .in(this)
  86. .select(`#${this.elId}`)
  87. .boundingClientRect()
  88. .exec(ret => {
  89. this.width = parseInt((ret[0].width - 1) / this.column) + 'px'
  90. fn(this.width)
  91. })
  92. // #endif
  93. // #ifdef APP-NVUE
  94. dom.getComponentRect(this.$refs['uni-grid'], (ret) => {
  95. this.width = parseInt((ret.size.width - 1) / this.column) + 'px'
  96. fn(this.width)
  97. })
  98. // #endif
  99. }
  100. }
  101. }
  102. </script>
  103. <style scoped>
  104. .uni-grid-wrap {
  105. /* #ifndef APP-NVUE */
  106. display: flex;
  107. /* #endif */
  108. flex: 1;
  109. flex-direction: column;
  110. /* #ifdef H5 */
  111. width: 100%;
  112. /* #endif */
  113. }
  114. .uni-grid {
  115. /* #ifndef APP-NVUE */
  116. display: flex;
  117. /* #endif */
  118. /* flex: 1;
  119. */
  120. flex-direction: row;
  121. flex-wrap: wrap;
  122. }
  123. .uni-grid--border {
  124. border-left-color: #e5e5e5;
  125. border-left-style: solid;
  126. border-left-width: 1px;
  127. }
  128. </style>