swipe-action.vue
7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<template>
<view class="container">
<uni-card is-full :is-shadow="false">
<text class="uni-h6">通过滑动触发选项的容器,容器内可放置列表等组件,通过左右滑动来触发一些操作。</text>
</uni-card>
<uni-section
title="基本用法"
type="line"
></uni-section>
<uni-swipe-action>
<uni-swipe-action-item
:left-options="options2"
:threshold="0"
:right-options="options1"
@click="bindClick"
>
<view class="content-box" @click="contentClick">
<text class="content-text">使用数据填充</text>
</view>
</uni-swipe-action-item>
<uni-swipe-action-item @click="bindClick">
<template v-slot:left>
<view class="slot-button">
<text
class="slot-button-text"
@click="bindClick({position:'left',content:{text:'置顶'}})"
>置顶</text>
</view>
</template>
<view class="content-box" @click="contentClick">
<text class="content-text">使用左右插槽</text>
</view>
<template v-slot:right>
<view class="slot-button" @click="bindClick({position:'right',content:{text:'删除'}})"><text class="slot-button-text">删除</text></view>
</template>
</uni-swipe-action-item>
<uni-swipe-action-item
:right-options="options1"
@click="bindClick"
>
<template v-slot:left>
<view class="slot-button"><text
class="slot-button-text"
@click="bindClick({position:'left',content:{text:'置顶'}})"
>置顶</text></view>
</template>
<view class="content-box" @click="contentClick">
<text class="content-text">数据与插槽混合使用</text>
</view>
</uni-swipe-action-item>
</uni-swipe-action>
<uni-section
title="禁止滑动"
type="line"
></uni-section>
<uni-swipe-action>
<uni-swipe-action-item :disabled="true">
<view class="content-box">
<text class="content-text">禁止左右滚动</text>
</view>
</uni-swipe-action-item>
</uni-swipe-action>
<uni-section
title="使用变量控制开关"
type="line"
></uni-section>
<view class="example-body">
<view
class="button"
@click="setOpened"
>
<text class="button-text">当前状态:{{ isOpened }}</text>
</view>
</view>
<uni-swipe-action>
<uni-swipe-action-item
:left-options="options2"
:right-options="options2"
:show="isOpened"
:auto-close="false"
@change="change"
@click="bindClick"
>
<view class="content-box">
<text class="content-text">使用变量控制SwipeAction的开启状态</text>
</view>
</uni-swipe-action-item>
</uni-swipe-action>
<uni-section
title="swipe-action 列表"
type="line"
></uni-section>
<uni-swipe-action ref="swipeAction">
<uni-swipe-action-item
v-for="(item, index) in swipeList"
:right-options="item.options"
:key="item.id"
@change="swipeChange($event, index)"
@click="swipeClick($event, index)"
>
<view class="content-box">
<text class="content-text">{{ item.content }}</text>
</view>
</uni-swipe-action-item>
</uni-swipe-action>
</view>
</template>
<script>
export default {
components: {},
data() {
return {
show: false,
isOpened: 'none',
options1: [{
text: '取消置顶'
}],
options2: [{
text: '取消',
style: {
backgroundColor: '#007aff'
}
},
{
text: '确认',
style: {
backgroundColor: '#F56C6C'
}
}
],
swipeList: [{
options: [{
text: '添加',
style: {
backgroundColor: '#F56C6C'
}
}],
id: 0,
content: '左滑点击添加新增一条数据'
},
{
id: 1,
options: [{
text: '置顶'
},
{
text: '删除',
style: {
backgroundColor: 'rgb(255,58,49)'
}
}
],
content: 'item2'
},
{
id: 2,
options: [{
text: '置顶'
},
{
text: '标记为已读',
style: {
backgroundColor: 'rgb(254,156,1)'
}
},
{
text: '删除',
style: {
backgroundColor: 'rgb(255,58,49)'
}
}
],
content: 'item3'
}
]
};
},
onReady() {
// 模拟延迟赋值
setTimeout(() => {
this.isOpened = 'right';
}, 1000);
uni.$on('update',res=>{
console.log(111);
this.swipeClick({
content:{
text:'添加'
}
})
})
},
methods: {
contentClick(){
console.log('点击内容');
uni.showToast({
title:'点击内容',
icon:'none'
})
},
bindClick(e) {
console.log(e);
uni.showToast({
title: `点击了${e.position === 'left' ? '左侧' : '右侧'} ${e.content.text}按钮`,
icon: 'none'
});
},
setOpened() {
if (this.isOpened === 'none') {
this.isOpened = 'left';
return;
}
if (this.isOpened === 'left') {
this.isOpened = 'right';
return;
}
if (this.isOpened === 'right') {
this.isOpened = 'none';
return;
}
},
change(e) {
this.isOpened = e;
console.log('返回:', e);
},
swipeChange(e, index) {
console.log('返回:', e);
console.log('当前索引:', index);
},
swipeClick(e, index) {
let {
content
} = e;
if (content.text === '删除') {
uni.showModal({
title: '提示',
content: '是否删除',
success: res => {
if (res.confirm) {
this.swipeList.splice(index, 1);
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
} else if (content.text === '添加') {
if (this.swipeList.length < 10) {
this.swipeList.push({
id: new Date().getTime(),
options: [{
text: '置顶'
},
{
text: '标记为已读',
style: {
backgroundColor: 'rgb(254,156,1)'
}
},
{
text: '删除',
style: {
backgroundColor: 'rgb(255,58,49)'
}
}
],
content: '新增' + new Date().getTime()
});
uni.showToast({
title: `添加了一条数据`,
icon: 'none'
});
} else {
uni.showToast({
title: `最多添加十条数据`,
icon: 'none'
});
}
} else {
uni.showToast({
title: `点击了${e.content.text}按钮`,
icon: 'none'
});
}
}
}
};
</script>
<style lang="scss">
.content-box {
flex: 1;
/* #ifdef APP-NVUE */
justify-content: center;
/* #endif */
height: 44px;
line-height: 44px;
padding: 0 15px;
position: relative;
background-color: #fff;
border-bottom-color: #f5f5f5;
border-bottom-width: 1px;
border-bottom-style: solid;
}
.content-text {
font-size: 15px;
}
.example-body {
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
flex-direction: row;
justify-content: center;
padding: 10px 0;
background-color: #fff;
}
.button {
border-color: #e5e5e5;
border-style: solid;
border-width: 1px;
padding: 4px 8px;
border-radius: 4px;
}
.button-text {
font-size: 15px;
}
.slot-button {
/* #ifndef APP-NVUE */
display: flex;
height: 100%;
/* #endif */
flex: 1;
flex-direction: row;
justify-content: center;
align-items: center;
padding: 0 20px;
background-color: #ff5a5f;
}
.slot-button-text {
color: #ffffff;
font-size: 14px;
}
</style>