iview/src/components/transfer/transfer.vue

279 lines
9.8 KiB
Vue
Raw Normal View History

2017-03-08 17:04:07 +08:00
<!-- <template>
<div :class="classes">
<List
2017-03-07 15:06:38 +08:00
ref="left"
:prefix-cls="prefixCls + '-list'"
:data="leftData"
:render-format="renderFormat"
2017-03-07 15:06:38 +08:00
:checked-keys="leftCheckedKeys"
@on-checked-keys-change="handleLeftCheckedKeysChange"
:valid-keys-count="leftValidKeysCount"
:style="listStyle"
:title="titles[0]"
:filterable="filterable"
:filter-placeholder="filterPlaceholder"
:filter-method="filterMethod"
:not-found-text="notFoundText">
<slot></slot>
2017-03-08 17:04:07 +08:00
</List>
<Operation
:prefix-cls="prefixCls"
:operations="operations"
:left-active="leftValidKeysCount > 0"
2017-03-08 17:04:07 +08:00
:right-active="rightValidKeysCount > 0">
</Operation>
<List
2017-03-07 15:06:38 +08:00
ref="right"
:prefix-cls="prefixCls + '-list'"
:data="rightData"
:render-format="renderFormat"
2017-03-07 15:06:38 +08:00
:checked-keys="rightCheckedKeys"
@on-checked-keys-change="handleRightCheckedKeysChange"
:valid-keys-count="rightValidKeysCount"
:style="listStyle"
:title="titles[1]"
:filterable="filterable"
:filter-placeholder="filterPlaceholder"
:filter-method="filterMethod"
:not-found-text="notFoundText">
2017-03-08 17:04:07 +08:00
<slot></slot>
</List>
</div>
2017-03-08 17:04:07 +08:00
</template> -->
<script>
import List from './list.vue';
import Operation from './operation.vue';
2017-01-12 10:21:47 +08:00
import { t } from '../../locale';
const prefixCls = 'ivu-transfer';
export default {
2017-03-08 17:04:07 +08:00
render (createElement) {
function cloneVNode (vnode) {
2017-03-08 17:08:10 +08:00
const clonedChildren = vnode.children && vnode.children.map(vnode => cloneVNode(vnode));
2017-03-08 17:04:07 +08:00
const cloned = createElement(vnode.tag, vnode.data, clonedChildren);
cloned.text = vnode.text;
cloned.isComment = vnode.isComment;
cloned.componentOptions = vnode.componentOptions;
cloned.elm = vnode.elm;
cloned.context = vnode.context;
cloned.ns = vnode.ns;
cloned.isStatic = vnode.isStatic;
cloned.key = vnode.key;
return cloned;
}
const vNodes = this.$slots.default;
const clonedVNodes = vNodes.map(vnode => cloneVNode(vnode));
return createElement('div', {
'class': this.classes
}, [
createElement('List', {
ref: 'left',
props: {
prefixCls: this.prefixCls + '-list',
data: this.leftData,
renderFormat: this.renderFormat,
checkedKeys: this.leftCheckedKeys,
validKeysCount: this.leftValidKeysCount,
style: this.listStyle,
title: this.titles[0],
filterable: this.filterable,
filterPlaceholder: this.filterPlaceholder,
filterMethod: this.filterMethod,
notFoundText: this.notFoundText
},
on: {
'on-checked-keys-change': this.handleLeftCheckedKeysChange
}
}, vNodes),
createElement('Operation', {
props: {
prefixCls: this.prefixCls,
operations: this.operations,
leftActive: this.leftValidKeysCount > 0,
rightActive: this.rightValidKeysCount > 0
}
}),
createElement('List', {
ref: 'right',
props: {
prefixCls: this.prefixCls + '-list',
data: this.rightData,
renderFormat: this.renderFormat,
checkedKeys: this.rightCheckedKeys,
validKeysCount: this.rightValidKeysCount,
style: this.listStyle,
title: this.titles[1],
filterable: this.filterable,
filterPlaceholder: this.filterPlaceholder,
filterMethod: this.filterMethod,
notFoundText: this.notFoundText
},
on: {
'on-checked-keys-change': this.handleRightCheckedKeysChange
}
}, clonedVNodes),
]);
},
components: { List, Operation },
props: {
data: {
type: Array,
default () {
2016-12-25 22:49:42 +08:00
return [];
}
},
renderFormat: {
type: Function,
default (item) {
return item.label || item.key;
}
},
targetKeys: {
type: Array,
default () {
2016-12-25 22:49:42 +08:00
return [];
}
},
selectedKeys: {
type: Array,
default () {
2016-12-25 22:49:42 +08:00
return [];
}
},
listStyle: {
type: Object,
default () {
2016-12-25 22:49:42 +08:00
return {};
}
},
titles: {
type: Array,
default () {
2017-01-12 10:21:47 +08:00
return [t('i.transfer.titles.source'), t('i.transfer.titles.target')];
}
},
operations: {
type: Array,
default () {
2016-12-25 22:49:42 +08:00
return [];
}
},
filterable: {
type: Boolean,
default: false
},
filterPlaceholder: {
type: String,
2017-01-12 10:21:47 +08:00
default () {
return t('i.transfer.filterPlaceholder');
}
},
filterMethod: {
type: Function,
default (data, query) {
const type = ('label' in data) ? 'label' : 'key';
return data[type].indexOf(query) > -1;
}
},
notFoundText: {
type: String,
2017-01-12 10:21:47 +08:00
default () {
return t('i.transfer.notFoundText');
}
}
},
data () {
return {
prefixCls: prefixCls,
leftData: [],
rightData: [],
leftCheckedKeys: [],
rightCheckedKeys: []
2016-12-25 22:49:42 +08:00
};
},
computed: {
classes () {
return [
`${prefixCls}`
2016-12-25 22:49:42 +08:00
];
},
leftValidKeysCount () {
return this.getValidKeys('left').length;
},
rightValidKeysCount () {
return this.getValidKeys('right').length;
}
},
methods: {
getValidKeys (direction) {
return this[`${direction}Data`].filter(data => !data.disabled && this[`${direction}CheckedKeys`].indexOf(data.key) > -1).map(data => data.key);
},
splitData (init = false) {
this.leftData = [...this.data];
this.rightData = [];
if (this.targetKeys.length > 0) {
this.targetKeys.forEach((targetKey) => {
this.rightData.push(
this.leftData.filter((data, index) => {
if (data.key === targetKey) {
this.leftData.splice(index, 1);
return true;
}
return false;
})[0]);
});
}
if (init) {
this.splitSelectedKey();
}
},
splitSelectedKey () {
const selectedKeys = this.selectedKeys;
if (selectedKeys.length > 0) {
this.leftCheckedKeys = this.leftData
.filter(data => selectedKeys.indexOf(data.key) > -1)
.map(data => data.key);
this.rightCheckedKeys = this.rightData
.filter(data => selectedKeys.indexOf(data.key) > -1)
.map(data => data.key);
}
},
moveTo (direction) {
const targetKeys = this.targetKeys;
const opposite = direction === 'left' ? 'right' : 'left';
const moveKeys = this.getValidKeys(opposite);
const newTargetKeys = direction === 'right' ?
moveKeys.concat(targetKeys) :
targetKeys.filter(targetKey => !moveKeys.some(checkedKey => targetKey === checkedKey));
this.$refs[opposite].toggleSelectAll(false);
this.$emit('on-change', newTargetKeys, direction, moveKeys);
2017-03-07 15:06:38 +08:00
// todo 事件
// this.$dispatch('on-form-change', newTargetKeys, direction, moveKeys);
},
handleLeftCheckedKeysChange (keys) {
this.leftCheckedKeys = keys;
},
handleRightCheckedKeysChange (keys) {
this.rightCheckedKeys = keys;
}
},
watch: {
targetKeys () {
this.splitData(false);
}
},
created () {
this.splitData(true);
}
2016-12-25 22:49:42 +08:00
};
</script>