u-waterfall.vue 9.7 KB

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