uni-grid.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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-color':borderColor}">
  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.$nextTick(() => {
  68. this.init()
  69. })
  70. },
  71. methods: {
  72. init() {
  73. setTimeout(() => {
  74. this._getSize((width) => {
  75. console.info('_getSize width',width);
  76. this.children.forEach((item, index) => {
  77. item.width = width
  78. })
  79. })
  80. }, 50)
  81. },
  82. change(e) {
  83. this.$emit('change', e)
  84. },
  85. _getSize(fn) {
  86. // #ifndef APP-NVUE
  87. uni.createSelectorQuery()
  88. .in(this)
  89. .select(`#${this.elId}`)
  90. .boundingClientRect()
  91. .exec(ret => {
  92. this.width = parseInt((ret[0].width - 1) / this.column) + 'px'
  93. fn(this.width)
  94. })
  95. // #endif
  96. // #ifdef APP-NVUE
  97. dom.getComponentRect(this.$refs['uni-grid'], (ret) => {
  98. this.width = parseInt((ret.size.width - 1) / this.column) + 'px'
  99. fn(this.width)
  100. })
  101. // #endif
  102. }
  103. }
  104. }
  105. </script>
  106. <style scoped>
  107. .uni-grid-wrap {
  108. /* #ifndef APP-NVUE */
  109. display: flex;
  110. /* #endif */
  111. flex: 1;
  112. flex-direction: column;
  113. /* #ifdef H5 */
  114. width: 100%;
  115. /* #endif */
  116. }
  117. .uni-grid {
  118. /* #ifndef APP-NVUE */
  119. display: flex;
  120. /* #endif */
  121. flex-direction: row;
  122. flex-wrap: wrap;
  123. }
  124. .uni-grid--border {
  125. position: relative;
  126. /* #ifdef APP-NVUE */
  127. border-left-color: #e5e5e5;
  128. border-left-style: solid;
  129. border-left-width: 0.5px;
  130. /* #endif */
  131. /* #ifndef APP-NVUE */
  132. z-index: 1;
  133. border-left: 1px #e5e5e5 solid;
  134. /* #endif */
  135. }
  136. </style>