uni-grid.vue 2.8 KB

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