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