iview/src/components/transfer/list.vue

130 lines
4.6 KiB
Vue
Raw Normal View History

<template>
2016-11-18 17:37:59 +08:00
<div :class="classes" :style="style">
<div :class="prefixCls + '-header'">
2016-11-18 14:37:02 +08:00
<Checkbox :checked.sync="checkedAll" :disabled="checkedAllDisabled" @on-change="toggleSelectAll"></Checkbox>
<span>{{ 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.sync="query"
:placeholder="filterPlaceholder"></Search>
</div>
2016-11-18 12:02:09 +08:00
<ul :class="prefixCls + '-content'">
<li
v-for="item in showItems | filterBy filterData"
2016-11-27 01:42:39 +08:00
:class="itemClasses(item)"
2016-11-18 14:37:02 +08:00
@click.prevent="select(item)">
<Checkbox :checked="isCheck(item)" :disabled="item.disabled"></Checkbox>
<span>{{{ showLabel(item) }}}</span>
2016-11-18 14:37:02 +08:00
</li>
2016-11-18 12:02:09 +08:00
<li :class="prefixCls + '-content-not-found'">{{ notFoundText }}</li>
</ul>
</div>
2016-11-18 15:01:49 +08:00
<div :class="prefixCls + '-footer'" v-if="showFooter" v-el:footer><slot></slot></div>
</div>
</template>
<script>
import Search from './search.vue';
import Checkbox from '../checkbox/checkbox.vue';
export default {
components: { Search, Checkbox },
props: {
prefixCls: String,
data: Array,
renderFormat: Function,
checkedKeys: Array,
style: Object,
title: [String, Number],
filterable: Boolean,
filterPlaceholder: String,
filterMethod: Function,
notFoundText: String,
validKeysCount: Number
},
data () {
return {
showItems: [],
2016-11-18 15:01:49 +08:00
query: '',
showFooter: true
2016-12-25 22:49:42 +08:00
};
},
computed: {
2016-11-18 17:37:59 +08:00
classes () {
return [
`${this.prefixCls}`,
{
[`${this.prefixCls}-with-footer`]: this.showFooter
}
2016-12-25 22:49:42 +08:00
];
2016-11-18 17:37:59 +08:00
},
bodyClasses () {
return [
`${this.prefixCls}-body`,
{
2016-11-18 15:51:54 +08:00
[`${this.prefixCls}-body-with-search`]: this.filterable,
[`${this.prefixCls}-body-with-footer`]: this.showFooter
}
2016-12-25 22:49:42 +08:00
];
},
count () {
const validKeysCount = this.validKeysCount;
2016-11-27 01:42:39 +08:00
return (validKeysCount > 0 ? `${validKeysCount}/` : '') + `${this.data.length}`;
},
checkedAll () {
return this.data.filter(data => !data.disabled).length === this.validKeysCount && this.validKeysCount !== 0;
},
checkedAllDisabled () {
return this.data.filter(data => !data.disabled).length <= 0;
}
},
methods: {
2016-11-27 01:42:39 +08:00
itemClasses (item) {
return [
`${this.prefixCls}-content-item`,
{
[`${this.prefixCls}-content-item-disabled`]: item.disabled
}
2016-12-25 22:49:42 +08:00
];
2016-11-27 01:42:39 +08:00
},
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);
},
updateFilteredData () {
2016-11-18 14:37:02 +08:00
this.showItems = this.data;
},
toggleSelectAll (status) {
this.checkedKeys = status ?
this.data.filter(data => !data.disabled || this.checkedKeys.indexOf(data.key) > -1).map(data => data.key) :
this.data.filter(data => data.disabled && this.checkedKeys.indexOf(data.key) > -1).map(data => data.key);
},
filterData (value) {
return this.filterMethod(value, this.query);
}
},
created () {
this.updateFilteredData();
2016-11-18 15:01:49 +08:00
},
2016-11-18 15:51:54 +08:00
compiled () {
2016-11-18 15:01:49 +08:00
this.showFooter = this.$els.footer.innerHTML !== '';
},
watch: {
data () {
this.updateFilteredData();
}
}
2016-12-25 22:49:42 +08:00
};
2016-11-27 01:42:39 +08:00
</script>