ZBLOG

x

要使你的 Vue 组件的 a-transfer 组件在待选状态中只能选择一个值,并且在已选状态中替换掉之前的选项,可以通过以下步骤进行修改。

你需要调整 handleChange 方法,以确保只允许选择一个目标键。具体实现如下:

  1. 更新 handleChange 方法:在选择新的项目时,将之前已选中的项目移除,只保留最新选择的那个。

  2. 设置 v-model 的 targetKeys 属性为数组长度为1,这样就可以保证只能有一个值被选中。

以下是更新后的代码:

<template>
  <div class="singleMarking">
    <div class="cancel-btn-container">
      <a-button class="cancel-btn" @click="handleCancel">x</a-button>
    </div>
    <div style="margin-top: 40px">
      <a-transfer
        v-model:target-keys="targetKeys"
        v-model:selected-keys="selectedKeys"
        :data-source="dataSource"
        :titles="['待选状态', '已选状态']"
        :render="renderItem"
        @change="handleChange"
        list-style="width: 100%;height:300px;overflow-y: auto;"
      />
    </div>
    <div class="action-btns">
      <a-button class="cancel-btn" @click="handleCancel">取消</a-button>
      <a-button class="confirm-btn" type="primary" @click="handleConfirm">确认</a-button>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { message } from 'ant-design-vue';
import { useRoute } from 'vue-router';
import { queryTagList, getCustomerTags, singleCustomerTags } from '@/utils';

const formState = reactive({});
const targetKeys = ref([]); // 存储已选择的key,初始为空
const selectedKeys = ref([]); // 用于控制当前选中的key
const route = useRoute();
const custId = ref('');
const name = ref('');
const dataSource = ref([]);

const handleCancel = () => {
  window.parent.postMessage({ type: 'close-dialog', action: 'cancel' }, '*');
};

const handleChange = (nextTargetKeys) => {
  if (nextTargetKeys.length > 0) {
    // 如果有新选择,更新targetKeys,仅保留最新选择的项
    targetKeys.value = [nextTargetKeys[nextTargetKeys.length - 1]];
  }
};

const handleConfirm = () => {
  const params = {
    tagsId: targetKeys.value,
    customerId: custId.value,
    customerName: name.value
  };
  
  singleCustomerTags(params).then((response) => {
    if (response.success) {
      message.success(response.message);
    } else {
      message.error(response.message);
    }
    
    handleCancel();
  });
};

const fetchAvailableTags = () => {
  queryTagList().then((response) => {
    if (response.success) {
      dataSource.value = response.data.map((tag) => ({
        key: tag.id,
        title: tag.tagsDescription,
        color: tag.color || '#000'
      }));
    } else {
      message.error(response.message);
    }
  });
};

const fetchSelectedTags = () => {
  getCustomerTags({ customerId: custId.value }).then((response) => {
    if (response.success) {
      selectedKeys.value = response.data.map((tag) => tag.id);
      targetKeys.value = response.data.length > 0 ? [response.data[0].id] : []; // 初始化时取第一个已选项
    } else {
      message.error(response.message);
    }
  });
};

onMounted(() => {
  custId.value = route.query.custId;
  name.value = route.query.name;
  
  fetchAvailableTags();
  
});
</script>

修改说明:

  • handleChange 方法中,当用户在左侧(待选状态)选择新项时,我们将 targetKeys 设置为新选中的项,这样就保证了只有一项会被保留。
  • 对于获取已有标签的方法(fetchSelectedTags),我们初始化时直接从获取的数据中取第一项作为默认已选项。

这样实现后,你就可以确保在待选状态只能单个选择,并且替换掉已选状态的值。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:http://sdn.0voice.com/?id=7486

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?