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.

224 lines
9.0 KiB

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