uni-grid.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. export default {
  13. name: 'UniGrid',
  14. props: {
  15. // 每列显示个数
  16. column: {
  17. type: Number,
  18. default: 3
  19. },
  20. // 是否显示边框
  21. showBorder: {
  22. type: Boolean,
  23. default: true
  24. },
  25. // 边框颜色
  26. borderColor: {
  27. type: String,
  28. default: '#e5e5e5'
  29. },
  30. // 是否正方形显示,默认为 true
  31. square: {
  32. type: Boolean,
  33. default: true
  34. },
  35. highlight: {
  36. type: Boolean,
  37. default: true
  38. }
  39. },
  40. provide() {
  41. return {
  42. grid: this
  43. }
  44. },
  45. data() {
  46. const elId = `Uni_${Math.ceil(Math.random() * 10e5).toString(36)}`
  47. return {
  48. elId,
  49. width: 0
  50. }
  51. },
  52. created() {
  53. this.children = []
  54. },
  55. mounted() {
  56. this.init()
  57. },
  58. methods: {
  59. init() {
  60. setTimeout(() => {
  61. this._getSize((width) => {
  62. this.children.forEach((item, index) => {
  63. item.width = width
  64. })
  65. })
  66. }, 50)
  67. },
  68. change(e) {
  69. this.$emit('change', e)
  70. },
  71. _getSize(fn) {
  72. // #ifndef APP-NVUE
  73. uni.createSelectorQuery()
  74. .in(this)
  75. .select(`#${this.elId}`)
  76. .boundingClientRect()
  77. .exec(ret => {
  78. this.width = parseInt((ret[0].width-1) / this.column) + 'px'
  79. fn(this.width)
  80. })
  81. // #endif
  82. // #ifdef APP-NVUE
  83. dom.getComponentRect(this.$refs['uni-grid'], (ret) => {
  84. this.width = parseInt((ret.size.width-1) / this.column) + 'px'
  85. fn(this.width)
  86. })
  87. // #endif
  88. }
  89. }
  90. }
  91. </script>
  92. <style lang="scss" scoped>
  93. .uni-grid-wrap {
  94. /* #ifndef APP-NVUE */
  95. display: flex;
  96. /* #endif */
  97. flex: 1;
  98. flex-direction: column;
  99. /* #ifdef H5 */
  100. width: 100%;
  101. /* #endif */
  102. }
  103. .uni-grid {
  104. /* #ifndef APP-NVUE */
  105. display: flex;
  106. /* #endif */
  107. // flex: 1;
  108. flex-direction: row;
  109. flex-wrap: wrap;
  110. }
  111. .uni-grid--border {
  112. border-left-color: $uni-border-color;
  113. border-left-style: solid;
  114. border-left-width: 1px;
  115. }
  116. </style>