list.vue
5.12 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
<template>
<div :class="classes" :style="listStyle">
<div :class="prefixCls + '-header'">
<Checkbox :value="checkedAll" :disabled="checkedAllDisabled" @on-change="toggleSelectAll"></Checkbox>
<span :class="prefixCls + '-header-title'" @click="toggleSelectAll(!checkedAll)">{{ title }}</span>
<span :class="prefixCls + '-header-count'">{{ count }}</span>
</div>
<div :class="bodyClasses">
<div :class="prefixCls + '-body-search-wrapper'" v-if="filterable">
<Search
:prefix-cls="prefixCls + '-search'"
:query="query"
@on-query-clear="handleQueryClear"
@on-query-change="handleQueryChange"
:placeholder="filterPlaceholder"></Search>
</div>
<ul :class="prefixCls + '-content'">
<li
v-for="item in filterData"
:class="itemClasses(item)"
@click.prevent="select(item)">
<Checkbox :value="isCheck(item)" :disabled="item.disabled"></Checkbox>
<span v-html="showLabel(item)"></span>
</li>
<li :class="prefixCls + '-content-not-found'">{{ notFoundText }}</li>
</ul>
</div>
<div :class="prefixCls + '-footer'" v-if="showFooter"><slot></slot></div>
</div>
</template>
<script>
import Search from './search.vue';
import Checkbox from '../checkbox/checkbox.vue';
export default {
name: 'TransferList',
components: { Search, Checkbox },
props: {
prefixCls: String,
data: Array,
renderFormat: Function,
checkedKeys: Array,
listStyle: Object,
title: [String, Number],
filterable: Boolean,
filterPlaceholder: String,
filterMethod: Function,
notFoundText: String,
validKeysCount: Number
},
data () {
return {
showItems: [],
query: '',
showFooter: true
};
},
watch: {
data () {
this.updateFilteredData();
}
},
computed: {
classes () {
return [
`${this.prefixCls}`,
{
[`${this.prefixCls}-with-footer`]: this.showFooter
}
];
},
bodyClasses () {
return [
`${this.prefixCls}-body`,
{
[`${this.prefixCls}-body-with-search`]: this.filterable,
[`${this.prefixCls}-body-with-footer`]: this.showFooter
}
];
},
count () {
const validKeysCount = this.validKeysCount;
return (validKeysCount > 0 ? `${validKeysCount}/` : '') + `${this.data.length}`;
},
checkedAll () {
return this.filterData.filter(data => !data.disabled).length === this.validKeysCount && this.validKeysCount !== 0;
},
checkedAllDisabled () {
return this.filterData.filter(data => !data.disabled).length <= 0;
},
filterData () {
return this.showItems.filter(item => this.filterMethod(item, this.query));
}
},
methods: {
itemClasses (item) {
return [
`${this.prefixCls}-content-item`,
{
[`${this.prefixCls}-content-item-disabled`]: item.disabled
}
];
},
showLabel (item) {
return this.renderFormat(item);
},
isCheck (item) {
return this.checkedKeys.some(key => key === item.key);
},
select (item) {
if (item.disabled) return;
const index = this.checkedKeys.indexOf(item.key);
index > -1 ? this.checkedKeys.splice(index, 1) : this.checkedKeys.push(item.key);
this.$parent.handleCheckedKeys();
},
updateFilteredData () {
this.showItems = this.data;
},
toggleSelectAll (status) {
const keys = status ?
this.filterData.filter(data => !data.disabled || this.checkedKeys.indexOf(data.key) > -1).map(data => data.key) :
this.filterData.filter(data => data.disabled && this.checkedKeys.indexOf(data.key) > -1).map(data => data.key);
this.$emit('on-checked-keys-change', keys);
},
handleQueryClear () {
this.query = '';
},
handleQueryChange (val) {
this.query = val;
}
},
created () {
this.updateFilteredData();
},
mounted () {
this.showFooter = this.$slots.default !== undefined;
}
};
</script>