ZBLOG

x

要使左边的待选状态只能选择一个值且不可多选,你可以在<a-transfer>组件中添加一个 :show-search="false" 属性以禁用搜索框,并在 @change 事件处理函数中添加逻辑来限制选择数量。下面是修改后的代码:

<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;"
        :show-search="false" <!-- 禁用搜索框 -->
      />
    </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([]);
const selectedKeys = ref([]);
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, direction, moveKeys) => {
  if (direction === 'right') {
    // 如果是从待选状态移动到已选状态,强制只保留最后一个被选中的值
    const lastSelectedKey = moveKeys[moveKeys.length - 1];
    targetKeys.value = [lastSelectedKey]; // 保持目标键只有一个
  } else if (direction === 'left') {
    // 如果是从已选状态移回待选状态
    targetKeys.value = nextTargetKeys;
  }
};

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.map((tag) => tag.id);
    } else {
      message.error(response.message);
    }
  });
};

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

修改点说明:

  1. 仅允许单一选择:通过在 handleChange 方法中,无论用户如何选择,都强制将 targetKeys 数组设为包含唯一元素。
  2. 禁用多选功能:通过调整逻辑,确保无论如何都只会有一个值被设置。

这样就能实现左边的待选状态只能单独选择一个标签,其他标签不可多选。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?