您在代码中定义的 renderItem
函数用于渲染穿梭框中的每个项,但直接返回的字符串不会被解析为 HTML。为了让自定义渲染生效,您可以使用 Vue 的插槽来实现。
以下是修正后的代码示例,利用插槽来渲染带有颜色的文本:
<template>
<div class="form-tool">
<a-button class="margin-right-10 button-light-blue" @click="openModal">批量打标</a-button>
<a-modal v-model:visible="open" title="批量打标" @ok="handleOk" width="800px">
<a-transfer
v-model:target-keys="targetKeys"
v-model:selected-keys="selectedKeys"
:data-source="dataSource"
:titles="['待选状态', '已选状态']"
@change="handleChange"
>
<!-- 使用 slot-scope 来定制渲染项 -->
<template #leftRender="{ item }">
<span :style="{ color: item.color }">{{ item.title }}</span>
</template>
<template #rightRender="{ item }">
<span :style="{ color: item.color }">{{ item.title }}</span>
</template>
</a-transfer>
</a-modal>
</div>
</template>
<script setup>
import { ref } from 'vue';
const open = ref(false); // 用于控制模态框的显示
const targetKeys = ref([]); // 用于存储已选择的状态
const selectedKeys = ref([]); // 用于存储当前选中的状态
// 打开模态框的函数
const openModal = () => {
open.value = true;
};
// 确认按钮的回调函数
const handleOk = () => {
open.value = false;
console.log('已选择的状态:', targetKeys.value);
};
// 穿梭框数据源
const dataSource = ref([
{ key: '1', title: '状态1', color: '#f50' },
{ key: '2', title: '状态2', color: '#2db7f5' },
{ key: '3', title: '状态3', color: '#87d068' },
{ key: '4', title: '状态4', color: '#108ee9' },
{ key: '5', title: '状态5', color: '#ffbf00' },
{ key: '6', title: '状态6', color: '#ff7f50' },
{ key: '7', title: '状态7', color: '#8a2be2' },
{ key: '8', title: '状态8', color: '#a52a2a' },
{ key: '9', title: '状态9', color:'#deb887' },
{ key:'10',title:'状态10 ',color:'#5f9ea0'},
]);
// 穿梭框变化时的回调函数
const handleChange = (nextTargetKeys, direction, moveKeys) => {
console.log('targetKeys:', nextTargetKeys);
console.log('direction:', direction);
console.log('moveKeys:', moveKeys);
};
</script>
<style scoped>
/* 可以添加样式 */
</style>
更改说明:
使用插槽:通过
#leftRender
和#rightRender
插槽来自定义左侧和右侧项的渲染。动态绑定样式:使用
v-bind
或简写形式:
来动态设置样式,这样才能使颜色生效。
这样,你应该能够看到颜色与文本一起正确地显示在穿梭框中。
内容由零声教学AI助手提供,问题来源于学员提问