eiml-flow.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. <slot :name="item"></slot>
  7. </view>
  8. </view>
  9. <!--
  10. 下段代码是预加载,渲染出来后遍能读取到组件的属性了,然后进行排版
  11. 想要代码简单且通用,就需要牺牲一些东西
  12. -->
  13. <scroll-view style="width: 0rpx;height: 0rpx;">
  14. <view v-for="(item,index) in childCount" v-bind:key="index">
  15. <slot :name="index"></slot>
  16. </view>
  17. </scroll-view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. props: ["columnNum"],
  23. data() {
  24. return {
  25. defColumnNum: 2, //默认列数,如果不传,则默认两列
  26. columnWidth: 0,
  27. childCount: 0,
  28. lastChildCount: 0, //记录上次加载的数量,当数量发生改变时,则需要刷新
  29. tabList: []
  30. }
  31. },
  32. updated() {
  33. this.childCount = Object.getOwnPropertyNames(this.$slots).length;
  34. if (this.childCount != this.lastChildCount) {
  35. var that = this;
  36. this.$nextTick(function() {
  37. that.refreshList();
  38. });
  39. }
  40. },
  41. mounted() {
  42. this.childCount = Object.getOwnPropertyNames(this.$slots).length;
  43. var that = this;
  44. this.$nextTick(function() {
  45. that.refreshList();
  46. });
  47. },
  48. methods: {
  49. getTemp() {
  50. return 0;
  51. },
  52. refreshList() {
  53. var slots = this.$slots;
  54. this.childCount = Object.getOwnPropertyNames(slots).length;
  55. this.lastChildCount = this.childCount;
  56. if (this.columnNum == null || this.columnNum == "") {
  57. this.columnNum = this.defColumnNum;
  58. }
  59. this.columnWidth = 100 / this.columnNum;
  60. var temTabList = [];
  61. var temTabHeightList = [];
  62. for (var i = 0; i < this.columnNum; i++) {
  63. var temArr = [];
  64. temTabList[i] = temArr;
  65. temTabHeightList[i] = 0;
  66. }
  67. for (var i = 0; i < this.childCount; i++) {
  68. var item = slots[i];
  69. var offsetHeight = 0;
  70. if (item[0].elm != null) {
  71. offsetHeight = item[0].elm.offsetHeight;
  72. }
  73. if (offsetHeight > 0) {
  74. //查询出最小的列,将item添加到该列
  75. var tempIndex = 0;
  76. var tempHeight = 1000000;
  77. var tempStr = "";
  78. for (var j = 0; j < temTabHeightList.length; j++) {
  79. var temItem = temTabHeightList[j];
  80. tempStr += "j:" + temItem + ",";
  81. if (temItem < tempHeight) {
  82. tempIndex = j;
  83. tempHeight = temItem;
  84. }
  85. }
  86. temTabList[tempIndex].push(i);
  87. var tempHeight = temTabHeightList[tempIndex];
  88. tempHeight += item[0].elm.offsetHeight;
  89. temTabHeightList[tempIndex] = tempHeight;
  90. } else {
  91. var tab = i % this.columnNum;
  92. temTabList[tab].push(i);
  93. }
  94. }
  95. this.tabList = temTabList;
  96. }
  97. }
  98. }
  99. </script>
  100. <style lang="scss">
  101. .viewSpaceBetween {
  102. display: -webkit-flex;
  103. display: flex;
  104. -webkit-justify-content: space-between;
  105. justify-content: space-between;
  106. align-items: flex-start;
  107. }
  108. .column {
  109. flex-grow: 1;
  110. }
  111. </style>