1
0

u-waterfall.vue 6.1 KB

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