fix(transfer): 修复穿梭框逻辑, 新增 item 插槽

This commit is contained in:
就眠仪式 2021-10-20 20:46:55 +08:00
parent f568d29499
commit 29531eda88
2 changed files with 57 additions and 14 deletions

View File

@ -53,11 +53,48 @@ export default {
::: :::
::: field transfer 属性 ::: field 使用插槽
:::
::: demo
<template>
<lay-transfer :dataSource="dataSource1">
<template v-slot:item="{ data }">
{{data.id}}
</template>
</lay-transfer>
</template>
<script>
import { ref } from 'vue'
export default {
setup() {
const dataSource1 = [{id:'1', title:'易大师'},{id:'2', title:'战争之王'}]
return {
dataSource1
}
}
}
</script>
:::
::: field transfer 属性
::: :::
| Name | Description | Accepted Values | | Name | Description | Accepted Values |
| -------- | ---- | ----------------------- | | -------- | ---- | ----------------------- |
| dataSource | 数据来源 | -- | | dataSource | 数据来源 | -- |
| title | 标题 | -- | | title | 标题 | -- |
| id | 主键 | -- |
::: field transfer 插槽
:::
| Name | Description | Accepted Values |
| -------- | ---- | ----------------------- |
| item | 列表项 | { data } |

View File

@ -19,7 +19,8 @@
skin="primary" skin="primary"
:label="dataSource[id]" :label="dataSource[id]"
> >
<span>{{ dataSource.title }}</span> <slot v-if="slot.item" name="item" :data="dataSource" />
<span v-else>{{ dataSource.title }}</span>
</transfer-checkbox> </transfer-checkbox>
</li> </li>
</ul> </ul>
@ -56,7 +57,8 @@
skin="primary" skin="primary"
:label="dataSource[id]" :label="dataSource[id]"
> >
<span>{{ dataSource.title }}</span> <slot v-if="slot.item" name="item" :data="dataSource" />
<span v-else>{{ dataSource.title }}</span>
</transfer-checkbox> </transfer-checkbox>
</li> </li>
</ul> </ul>
@ -66,17 +68,18 @@
</template> </template>
<script setup name="LayTransfer"></script> <script setup name="LayTransfer"></script>
<script setup lang="ts"> <script setup lang="ts">
import { defineProps, Ref, ref, watch } from 'vue' import { computed, defineProps, Ref, ref, useSlots, watch } from 'vue'
import { Recordable } from '/@src/module/type' import { Recordable } from '/@src/module/type'
import transferCheckbox from './component/checkbox.vue' import transferCheckbox from './component/checkbox.vue'
import { check } from 'prettier' import { check } from 'prettier'
const slot = useSlots()
const props = withDefaults( const props = withDefaults(
defineProps<{ defineProps<{
id?: string id?: string
title?: string[] title?: string[]
dataSource: Recordable[] dataSource: Recordable[]
selectedKeys: Array<string>
}>(), }>(),
{ {
id: 'id', id: 'id',
@ -148,14 +151,15 @@ watch(
) )
const add = function () { const add = function () {
// leftDataSource
if(leftSelectedKeys.value.length === 0){ if(leftSelectedKeys.value.length === 0){
return return
} }
leftDataSource.value.forEach(item => {
rightDataSource.value = leftDataSource.value.filter( if( leftSelectedKeys.value.indexOf(item.id) != -1 ){
(item) => leftSelectedKeys.value.indexOf(item.id) != -1 rightDataSource.value.push(item)
) }
})
leftDataSource.value = leftDataSource.value.filter( leftDataSource.value = leftDataSource.value.filter(
(item) => leftSelectedKeys.value.indexOf(item.id) === -1 (item) => leftSelectedKeys.value.indexOf(item.id) === -1
) )
@ -166,9 +170,11 @@ const remove = function () {
if(rightSelectedKeys.value.length === 0){ if(rightSelectedKeys.value.length === 0){
return return
} }
leftDataSource.value = rightDataSource.value.filter( rightDataSource.value.forEach(item => {
(item) => rightSelectedKeys.value.indexOf(item.id) != -1 if( rightSelectedKeys.value.indexOf(item.id) != -1 ){
) leftDataSource.value.push(item)
}
})
rightDataSource.value = rightDataSource.value.filter( rightDataSource.value = rightDataSource.value.filter(
(item) => rightSelectedKeys.value.indexOf(item.id) === -1 (item) => rightSelectedKeys.value.indexOf(item.id) === -1
) )