eiml-flow-layout.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <view class="viewSpaceBetween">
  3. <view class="column" :style="{'width':columnWidth+'%'}" v-for="(itemTab,indexTab) in tabList"
  4. v-bind:key="indexTab">
  5. <view v-for="(item,index) in itemTab" v-bind:key="index">
  6. <child :item="item"></child>
  7. </view>
  8. </view>
  9. <!--下段代码是预加载,渲染出来后遍能读取到组件的属性了,然后进行排版
  10. 想要代码简单且通用,就需要牺牲一些东西-->
  11. <scroll-view style="width: 0rpx;height: 0rpx;">
  12. <view :id="'child'+index" v-for="(item,index) in list" v-bind:key="index">
  13. <child :item="item"></child>
  14. </view>
  15. </scroll-view>
  16. </view>
  17. </template>
  18. <script>
  19. import child from "@/components/eiml-flow-child/eiml-flow-child.vue"
  20. export default {
  21. props: ["columnNum", "list"],
  22. components: {
  23. 'child': child
  24. },
  25. watch: {
  26. list(data1, data2) {
  27. if (!data1.length) {
  28. this.tabList = []
  29. this.lastChildCount = 0
  30. }
  31. if (this.lastChildCount != this.list.length) {
  32. this.queryHeight();
  33. }
  34. }
  35. },
  36. data() {
  37. return {
  38. defColumnNum: 2, //默认列数,如果不传,则默认两列
  39. columnWidth: 0,
  40. lastChildCount: 0,
  41. tabList: []
  42. }
  43. },
  44. mounted() {
  45. // console.log("------------mounted")
  46. // this.queryHeight();
  47. },
  48. methods: {
  49. queryHeight() {
  50. if (this.columnNum == null || this.columnNum == "") {
  51. this.columnNum = this.defColumnNum;
  52. }
  53. this.columnWidth = 100 / this.columnNum;
  54. var that = this;
  55. var temHeightList = {};
  56. const query = uni.createSelectorQuery().in(this);
  57. for (var i = 0; i < this.list.length; i++) {
  58. query.select('#child' + i).boundingClientRect(data => {
  59. temHeightList[data.id] = data.height;
  60. if (data.id == ('child' + (that.list.length - 1))) {
  61. that.refreshLayout(temHeightList);
  62. }
  63. }).exec();
  64. }
  65. },
  66. refreshLayout(listHeight) {
  67. var temTabList = [];
  68. var temTabHeightList = [];
  69. for (var i = 0; i < this.columnNum; i++) {
  70. var temArr = [];
  71. temTabList[i] = temArr;
  72. temTabHeightList[i] = 0;
  73. }
  74. for (var i = 0; i < this.list.length; i++) {
  75. var item = this.list[i];
  76. var itemHeight = listHeight['child' + i];
  77. var minHeight = 1000000;
  78. var minIndex = 0;
  79. for (var j = 0; j < temTabHeightList.length; j++) {
  80. var tempHeight = temTabHeightList[j];
  81. if (tempHeight < minHeight) {
  82. minHeight = tempHeight;
  83. minIndex = j;
  84. }
  85. }
  86. temTabList[minIndex].push(item);
  87. temTabHeightList[minIndex] += itemHeight;
  88. }
  89. this.tabList = temTabList;
  90. this.lastChildCount = this.list.length;
  91. // console.log("=======刷新完成======",temTabList)
  92. }
  93. }
  94. }
  95. </script>
  96. <style>
  97. .viewSpaceBetween {
  98. display: -webkit-flex;
  99. display: flex;
  100. -webkit-justify-content: space-between;
  101. justify-content: space-between;
  102. align-items: flex-start;
  103. margin: 0 20rpx;
  104. background-color: #f5f5f5;
  105. }
  106. .sort-product {
  107. margin: 20rpx;
  108. }
  109. .column {
  110. flex-grow: 1;
  111. }
  112. </style>