u-waterfall.vue 6.1 KB

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