u-waterfall.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <view class="u-waterfall">
  3. <view id="u-left-column" class="u-column"><slot name="left" :leftList="leftList"></slot></view>
  4. <view id="u-right-column" class="u-column"><slot name="right" :rightList="rightList"></slot></view>
  5. </view>
  6. </template>
  7. <script>
  8. /**
  9. * waterfall 瀑布流
  10. * @description 这是一个瀑布流形式的组件,内容分为左右两列,结合uView的懒加载组件效果更佳。相较于某些只是奇偶数左右分别,或者没有利用vue作用域插槽的做法,uView的瀑布流实现了真正的 组件化,搭配LazyLoad 懒加载和loadMore 加载更多组件,让您开箱即用,眼前一亮。
  11. * @tutorial https://www.uviewui.com/components/waterfall.html
  12. * @property {Array} flow-list 用于渲染的数据
  13. * @property {String Number} add-time 单条数据添加到队列的时间间隔,单位ms,见上方注意事项说明(默认200)
  14. * @example <u-waterfall :flowList="flowList"></u-waterfall>
  15. */
  16. export default {
  17. name: "u-waterfall",
  18. props: {
  19. value: {
  20. // 瀑布流数据
  21. type: Array,
  22. required: true,
  23. default: function() {
  24. return [];
  25. }
  26. },
  27. // 每次向结构插入数据的时间间隔,间隔越长,越能保证两列高度相近,但是对用户体验越不好
  28. // 单位ms
  29. addTime: {
  30. type: [Number, String],
  31. default: 200
  32. },
  33. // id值,用于清除某一条数据时,根据此idKey名称找到并移除,如数据为{idx: 22, name: 'lisa'}
  34. // 那么该把idKey设置为idx
  35. idKey: {
  36. type: String,
  37. default: 'id'
  38. }
  39. },
  40. provide() {
  41. return {
  42. uWaterfall: this
  43. }
  44. },
  45. data() {
  46. return {
  47. leftList: [],
  48. rightList: [],
  49. tempList: [],
  50. children: []
  51. }
  52. },
  53. watch: {
  54. copyFlowList(nVal, oVal) {
  55. // 取差值,即这一次数组变化新增的部分
  56. let startIndex = Array.isArray(oVal) && oVal.length > 0 ? oVal.length : 0;
  57. // 拼接上原有数据
  58. this.tempList = this.tempList.concat(this.cloneData(nVal.slice(startIndex)));
  59. this.splitData();
  60. }
  61. },
  62. mounted() {
  63. this.tempList = this.cloneData(this.copyFlowList);
  64. this.splitData();
  65. },
  66. computed: {
  67. // 破坏flowList变量的引用,否则watch的结果新旧值是一样的
  68. copyFlowList() {
  69. return this.cloneData(this.value);
  70. }
  71. },
  72. methods: {
  73. async splitData() {
  74. if (!this.tempList.length) return;
  75. let leftRect = await this.$uGetRect('#u-left-column');
  76. let rightRect = await this.$uGetRect('#u-right-column');
  77. // 如果左边小于或等于右边,就添加到左边,否则添加到右边
  78. let item = this.tempList[0];
  79. // 解决多次快速上拉后,可能数据会乱的问题,因为经过上面的两个await节点查询阻塞一定时间,加上后面的定时器干扰
  80. // 数组可能变成[],导致此item值可能为undefined
  81. if(!item) return ;
  82. if (leftRect.height < rightRect.height) {
  83. this.leftList.push(item);
  84. } else if (leftRect.height > rightRect.height) {
  85. this.rightList.push(item);
  86. } else {
  87. // 这里是为了保证第一和第二张添加时,左右都能有内容
  88. // 因为添加第一张,实际队列的高度可能还是0,这时需要根据队列元素长度判断下一个该放哪边
  89. if (this.leftList.length <= this.rightList.length) {
  90. this.leftList.push(item);
  91. } else {
  92. this.rightList.push(item);
  93. }
  94. }
  95. // console.log(this.rightList)
  96. // 移除临时列表的第一项
  97. this.tempList.splice(0, 1);
  98. // 如果临时数组还有数据,继续循环
  99. if (this.tempList.length) {
  100. setTimeout(() => {
  101. this.splitData();
  102. }, this.addTime)
  103. }
  104. },
  105. cloneData(data) {// 复制而不是引用对象和数组
  106. return JSON.parse(JSON.stringify(data));
  107. },
  108. $uGetRect(selector, all) {
  109. return new Promise(resolve => {
  110. uni.createSelectorQuery().
  111. in(this)[all ? 'selectAll' : 'select'](selector)
  112. .boundingClientRect(rect => {
  113. if (all && Array.isArray(rect) && rect.length) {
  114. resolve(rect)
  115. }
  116. if (!all && rect) {
  117. resolve(rect)
  118. }
  119. })
  120. .exec()
  121. })
  122. },
  123. modify(id, key, value) {// 修改某条数据的某个属性
  124. // 如果findIndex找不到合适的条件,就会返回-1
  125. let index = -1;
  126. index = this.leftList.findIndex(val => val[this.idKey] == id);
  127. if(index != -1) {
  128. // 如果index不等于-1,说明已经找到了要找的id,修改对应key的值
  129. this.leftList[index][key] = value;
  130. } else {
  131. // 同理于上方面的方法
  132. index = this.rightList.findIndex(val => val[this.idKey] == id);
  133. if(index != -1) this.rightList[index][key] = value;
  134. }
  135. // 修改父组件的数据中的对应id的条目
  136. index = this.value.findIndex(val => val[this.idKey] == id);
  137. if(index != -1) {
  138. // 首先复制一份value的数据
  139. let data = this.cloneData(this.value);
  140. // 修改对应索引的key属性的值为value
  141. data[index][key] = value;
  142. // 修改父组件通过v-model绑定的变量的值
  143. this.$emit('input', data);
  144. }
  145. }
  146. }
  147. }
  148. </script>
  149. <style lang="scss" scoped>
  150. .u-waterfall {
  151. display: flex;
  152. flex-direction: row;
  153. align-items: flex-start;
  154. }
  155. .u-column {
  156. display: flex;
  157. flex: 1;
  158. flex-direction: column;
  159. height: auto;
  160. }
  161. .u-image {
  162. width: 100%;
  163. }
  164. </style>