AI销管
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

177 lines
5.7 KiB

  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. data() {
  41. return {
  42. leftList: [],
  43. rightList: [],
  44. tempList: [],
  45. children: []
  46. }
  47. },
  48. watch: {
  49. copyFlowList(nVal, oVal) {
  50. // 取差值,即这一次数组变化新增的部分
  51. let startIndex = Array.isArray(oVal) && oVal.length > 0 ? oVal.length : 0;
  52. // 拼接上原有数据
  53. this.tempList = this.tempList.concat(this.cloneData(nVal.slice(startIndex)));
  54. this.splitData();
  55. }
  56. },
  57. mounted() {
  58. this.tempList = this.cloneData(this.copyFlowList);
  59. this.splitData();
  60. },
  61. computed: {
  62. // 破坏flowList变量的引用,否则watch的结果新旧值是一样的
  63. copyFlowList() {
  64. return this.cloneData(this.value);
  65. }
  66. },
  67. methods: {
  68. async splitData() {
  69. if (!this.tempList.length) return;
  70. let leftRect = await this.$uGetRect('#u-left-column');
  71. let rightRect = await this.$uGetRect('#u-right-column');
  72. // 如果左边小于或等于右边,就添加到左边,否则添加到右边
  73. let item = this.tempList[0];
  74. // 解决多次快速上拉后,可能数据会乱的问题,因为经过上面的两个await节点查询阻塞一定时间,加上后面的定时器干扰
  75. // 数组可能变成[],导致此item值可能为undefined
  76. if(!item) return ;
  77. if (leftRect.height < rightRect.height) {
  78. this.leftList.push(item);
  79. } else if (leftRect.height > rightRect.height) {
  80. this.rightList.push(item);
  81. } else {
  82. // 这里是为了保证第一和第二张添加时,左右都能有内容
  83. // 因为添加第一张,实际队列的高度可能还是0,这时需要根据队列元素长度判断下一个该放哪边
  84. if (this.leftList.length <= this.rightList.length) {
  85. this.leftList.push(item);
  86. } else {
  87. this.rightList.push(item);
  88. }
  89. }
  90. // 移除临时列表的第一项
  91. this.tempList.splice(0, 1);
  92. // 如果临时数组还有数据,继续循环
  93. if (this.tempList.length) {
  94. setTimeout(() => {
  95. this.splitData();
  96. }, this.addTime)
  97. }
  98. },
  99. // 复制而不是引用对象和数组
  100. cloneData(data) {
  101. return JSON.parse(JSON.stringify(data));
  102. },
  103. // 清空数据列表
  104. clear() {
  105. this.leftList = [];
  106. this.rightList = [];
  107. // 同时清除父组件列表中的数据
  108. this.$emit('input', []);
  109. this.tempList = [];
  110. },
  111. // 清除某一条指定的数据,根据id实现
  112. remove(id) {
  113. // 如果findIndex找不到合适的条件,就会返回-1
  114. let index = -1;
  115. index = this.leftList.findIndex(val => val[this.idKey] == id);
  116. if(index != -1) {
  117. // 如果index不等于-1,说明已经找到了要找的id,根据index索引删除这一条数据
  118. this.leftList.splice(index, 1);
  119. } else {
  120. // 同理于上方面的方法
  121. index = this.rightList.findIndex(val => val[this.idKey] == id);
  122. if(index != -1) this.rightList.splice(index, 1);
  123. }
  124. // 同时清除父组件的数据中的对应id的条目
  125. index = this.value.findIndex(val => val[this.idKey] == id);
  126. if(index != -1) this.$emit('input', this.value.splice(index, 1));
  127. },
  128. // 修改某条数据的某个属性
  129. modify(id, key, value) {
  130. // 如果findIndex找不到合适的条件,就会返回-1
  131. let index = -1;
  132. index = this.leftList.findIndex(val => val[this.idKey] == id);
  133. if(index != -1) {
  134. // 如果index不等于-1,说明已经找到了要找的id,修改对应key的值
  135. this.leftList[index][key] = value;
  136. } else {
  137. // 同理于上方面的方法
  138. index = this.rightList.findIndex(val => val[this.idKey] == id);
  139. if(index != -1) this.rightList[index][key] = value;
  140. }
  141. // 修改父组件的数据中的对应id的条目
  142. index = this.value.findIndex(val => val[this.idKey] == id);
  143. if(index != -1) {
  144. // 首先复制一份value的数据
  145. let data = this.cloneData(this.value);
  146. // 修改对应索引的key属性的值为value
  147. data[index][key] = value;
  148. // 修改父组件通过v-model绑定的变量的值
  149. this.$emit('input', data);
  150. }
  151. }
  152. }
  153. }
  154. </script>
  155. <style lang="scss" scoped>
  156. @import "../../libs/css/style.components.scss";
  157. .u-waterfall {
  158. @include vue-flex;
  159. flex-direction: row;
  160. align-items: flex-start;
  161. }
  162. .u-column {
  163. @include vue-flex;
  164. flex: 1;
  165. flex-direction: column;
  166. height: auto;
  167. }
  168. .u-image {
  169. width: 100%;
  170. }
  171. </style>