有关element-plus的transfer组件默认数据回填问题?

<BaseModal width="640px" v-model="glGameModal.visible" :title="glGameModal.title"
    @defaultCancel="glGameCancel" @defaultConfirm="glGameConfirm">
    <el-row :gutter="10">
        <el-col :span="6">
            <el-input placeholder="请输入搜索内容" class="search-input"
                v-model="glGameModal.model.glGameSearchQuery" />
        </el-col>
        <el-col :span="4">
            <el-button type="primary" @click="fetchData">搜索</el-button>
        </el-col>
    </el-row>
    <el-transfer
        v-model="glGameModal.model.glGameSelect"
        :data="glGameModal.model.glGameSearchList"
        :right-default-checked="initialSelectedKeys"
        :titles="['待选', '已选']"
        @change="handleTransferChange"
    >
        <template #default="{ option }">
            <span :title="option.label" class="custom-item"
                :class="{ disabled: isDisabled(option) }">
                {{ option.label }}
            </span>
        </template>
    </el-transfer>
</BaseModal>

import { reactive, computed, watch } from 'vue';
import { ElButton, ElInput, ElRow, ElCol, ElTransfer } from 'element-plus';
import BaseModal from '@/components/BaseModal.vue';

const glGameModal: any = reactive({
    title: "关联游戏",
    visible: false,
    model: {
        glGameSearchQuery: "",
        glGameSearchList: [],
        glGameInitSelect: [
            { key: 2, label: "选项2" },
            { key: 3, label: "选项3" },
            { key: 6, label: "选项6" },
        ],
        glGameSelect: [],
        rowInfo: {},
        gameList: [],
    },
});

const fetchData = async () => {
    const data = [
        { key: 1, label: "选项1" },
        { key: 2, label: "选项2" },
        { key: 3, label: "选项3" },
        { key: 4, label: "选项4" },
        { key: 5, label: "选项5" },
        { key: 6, label: "选项6" },
        { key: 7, label: "选项7" },
        { key: 8, label: "选项8" },
        { key: 9, label: "选项9" },
        { key: 10, label: "选项10" },
    ];
    glGameModal.model.glGameSearchList = data;
    glGameModal.model.glGameSearchList
        .filter((item) => item.label.includes(glGameModal.model.glGameSearchQuery))
        .map((item) => ({
            ...item,
            disabled: isDisabled(item),
        }));
};

const glGameCancel = () => {
    console.log("取消操作");
    glGameModal.visible = false;
};


const initialSelectedKeys = computed(() => {
    return glGameModal.model.glGameInitSelect.map((item) => item.key);
});

const glGameConfirm = () => {
    const params = {
        columnNo: glGameModal.model.rowInfo.columnNo,
        catalogueNo: "text",
        skuList: [],
    };
    glGameModal.visible = false;
};

const isDisabled = (item) => {
    return glGameModal.model.glGameSelect.some((selectItem) => selectItem.key === item.key);
};

const handleTransferChange = (newVal, oldVal, movedKeys) => {
    glGameModal.model.glGameSelect = newVal;
};

尝试多重方法无效,在手册DEMO上
:right-default-checked看到是用的是KEY,不太会用,大佬给指点下。

阅读 5.9k
avatarAI BotBETA
### 解决方法

在 Element Plus 的 `el-transfer` 组件中,`:right-default-checked` 属性用于指定在组件初始化时哪些选项应该被默认选中。这个属性接收一个数组,数组中的每个元素是待选列表中选项的 `key` 值。

在你的代码中,`initialSelectedKeys` 是一个计算属性,它正确地返回了 `glGameModal.model.glGameInitSelect` 中每个选项的 `key` 值。然而,问题在于 `glGameModal.model.glGameSelect` 应该在组件初始化时就被设置为 `initialSelectedKeys` 的值,以确保在模态框打开时,这些选项默认被选中。

你可以在 `glGameModal` 的初始化或打开模态框的逻辑中设置 `glGameModal.model.glGameSelect` 的值。例如,你可以在打开模态框的函数中添加如下代码:

const openModal = () => {
glGameModal.visible = true;
// 设置默认选中的值
glGameModal.model.glGameSelect = initialSelectedKeys.value;
};


然而,由于你的代码中没有直接提供打开模态框的函数,你可以考虑在 `fetchData` 函数或任何初始化模态框数据的逻辑中设置这个值。注意,由于 `initialSelectedKeys` 是一个计算属性,你需要使用 `.value` 来访问它的值。

另外,确保 `fetchData` 函数中不会修改 `glGameModal.model.glGameSelect` 的值,除非用户进行了交互(比如通过搜索过滤了选项)。如果 `fetchData` 函数只是用来更新搜索列表,那么它不应该影响已选中的值。

最后,请确保 `handleTransferChange` 函数正确更新了 `glGameModal.model.glGameSelect`,以便在用户交互时保持状态同步。
1 个回答

使用 :default-checked 属性而不是 :right-default-checked 来设置初始选中的键。
我把你的代码修改了一下

vue组件部分

<BaseModal width="640px" v-model="glGameModal.visible" :title="glGameModal.title"
    @defaultCancel="glGameCancel" @defaultConfirm="glGameConfirm">
    <el-row :gutter="10">
        <el-col :span="6">
            <el-input placeholder="请输入搜索内容" class="search-input"
                v-model="glGameModal.model.glGameSearchQuery" />
        </el-col>
        <el-col :span="4">
            <el-button type="primary" @click="fetchData">搜索</el-button>
        </el-col>
    </el-row>
    <el-transfer
        v-model="glGameModal.model.glGameSelect"
        :data="glGameModal.model.glGameSearchList"
        :default-checked="initialSelectedKeys"
        :titles="['待选', '已选']"
        @change="handleTransferChange"
    >
        <template #default="{ option }">
            <span :title="option.label" class="custom-item"
                :class="{ disabled: isDisabled(option) }">
                {{ option.label }}
            </span>
        </template>
    </el-transfer>
</BaseModal>

JavaScript 部分

import { reactive, computed } from 'vue';
import { ElButton, ElInput, ElRow, ElCol, ElTransfer } from 'element-plus';
import BaseModal from '@/components/BaseModal.vue';

const glGameModal = reactive({
    title: "关联游戏",
    visible: false,
    model: {
        glGameSearchQuery: "",
        glGameSearchList: [],
        glGameInitSelect: [
            { key: 2, label: "选项2" },
            { key: 3, label: "选项3" },
            { key: 6, label: "选项6" },
        ],
        glGameSelect: [],
        rowInfo: {},
        gameList: [],
    },
});

const fetchData = async () => {
    const data = [
        { key: 1, label: "选项1" },
        { key: 2, label: "选项2" },
        { key: 3, label: "选项3" },
        { key: 4, label: "选项4" },
        { key: 5, label: "选项5" },
        { key: 6, label: "选项6" },
        { key: 7, label: "选项7" },
        { key: 8, label: "选项8" },
        { key: 9, label: "选项9" },
        { key: 10, label: "选项10" },
    ];
    glGameModal.model.glGameSearchList = data.filter((item) => item.label.includes(glGameModal.model.glGameSearchQuery));
};

const glGameCancel = () => {
    console.log("取消操作");
    glGameModal.visible = false;
};

const initialSelectedKeys = computed(() => {
    return glGameModal.model.glGameInitSelect.map((item) => item.key);
});

const glGameConfirm = () => {
    const params = {
        columnNo: glGameModal.model.rowInfo.columnNo,
        catalogueNo: "text",
        skuList: [],
    };
    glGameModal.visible = false;
};

const isDisabled = (item) => {
    return glGameModal.model.glGameSelect.some((selectItem) => selectItem.key === item.key);
};

const handleTransferChange = (newVal) => {
    glGameModal.model.glGameSelect = newVal;
};
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏