diff --git a/package/component/src/component/transfer/index.vue b/package/component/src/component/transfer/index.vue index 30bfe859..23270adf 100644 --- a/package/component/src/component/transfer/index.vue +++ b/package/component/src/component/transfer/index.vue @@ -35,6 +35,8 @@ const props = withDefaults(defineProps(), { height: "360px", }); +const emits = defineEmits(['update:modelValue','change']) + const leftDataSource: Ref = ref([...props.dataSource]); const rightDataSource: Ref = ref([]); const _leftDataSource: Ref = ref([...props.dataSource]); @@ -60,6 +62,29 @@ const allLeftChange = (isChecked: boolean) => { } }; +watch(() => props.modelValue, () => { + + let targetDataSource: any[] = []; + + props.dataSource.forEach(ds => { + if(props.modelValue.includes(ds[props.id])) { + targetDataSource.push(ds); + } + }) + + leftDataSource.value = props.dataSource.filter( + (item) => !props.modelValue.includes(item[props.id]) + ); + + _leftDataSource.value = props.dataSource.filter( + (item) => !props.modelValue.includes(item[props.id]) + ); + + rightDataSource.value = [...targetDataSource]; + _rightDataSource.value = [...targetDataSource]; + +}, {immediate: true}) + watch( leftSelectedKeys, () => { @@ -118,25 +143,26 @@ watch( ); const add = () => { + if (leftSelectedKeys.value.length === 0) { return; } + + let targetKeys: any[] = []; + leftDataSource.value.forEach((item) => { - if (leftSelectedKeys.value.indexOf(item.id) != -1) { - rightDataSource.value.push(item); + if (leftSelectedKeys.value.indexOf(item[props.id]) != -1) { + targetKeys.push(item[props.id]); } }); - leftDataSource.value = leftDataSource.value.filter( - (item) => leftSelectedKeys.value.indexOf(item.id) === -1 - ); - _leftDataSource.value.forEach((item) => { - if (leftSelectedKeys.value.indexOf(item.id) != -1) { - _rightDataSource.value.push(item); - } - }); - _leftDataSource.value = _leftDataSource.value.filter( - (item) => leftSelectedKeys.value.indexOf(item.id) === -1 - ); + + rightDataSource.value.forEach((item) => { + targetKeys.push(item[props.id]); + }) + + emits("change", targetKeys) + emits("update:modelValue", targetKeys) + leftSelectedKeys.value = []; }; @@ -144,22 +170,18 @@ const remove = () => { if (rightSelectedKeys.value.length === 0) { return; } + + let targetKeys: any[] = []; + rightDataSource.value.forEach((item) => { - if (rightSelectedKeys.value.indexOf(item.id) != -1) { - leftDataSource.value.push(item); + if (rightSelectedKeys.value.indexOf(item[props.id]) == -1) { + targetKeys.push(item[props.id]); } }); - rightDataSource.value = rightDataSource.value.filter( - (item) => rightSelectedKeys.value.indexOf(item.id) === -1 - ); - _rightDataSource.value.forEach((item) => { - if (rightSelectedKeys.value.indexOf(item.id) != -1) { - _leftDataSource.value.push(item); - } - }); - _rightDataSource.value = _rightDataSource.value.filter( - (item) => rightSelectedKeys.value.indexOf(item.id) === -1 - ); + + emits("change", targetKeys) + emits("update:modelValue", targetKeys) + rightSelectedKeys.value = []; }; diff --git a/package/document-component/src/document/zh-CN/components/transfer.md b/package/document-component/src/document/zh-CN/components/transfer.md index 8653dc8e..e14d2d01 100644 --- a/package/document-component/src/document/zh-CN/components/transfer.md +++ b/package/document-component/src/document/zh-CN/components/transfer.md @@ -13,7 +13,7 @@ ::: demo 使用 `lay-transfer` 标签, 创建穿梭框