ZBLOG

Cancel

在你的代码中,fetchSelectedTags 函数的目的是获取已选标签,并将它们设置为 selectedKeystargetKeys。如果左侧的可选项(dataSource)和右侧的已选项(selectedKeys)之间存在不匹配,你可以尝试以下步骤来确保已选择的标签能够正确显示在“已选状态”框中。

解决方案

  1. 确保数据源一致性: 确保你在调用 fetchSelectedTags 方法之前已经成功地获取了所有可能的标签数据。在使用 a-transfer 组件时,左侧的数据源 (dataSource) 应该包含右侧 (已选状态) 的数据。

  2. 修改 renderItem: 确保 renderItem 能够正确渲染有颜色属性的项目。

  3. 更新 selectedKeys 和 targetKeys: 当从后端获取到客户标签数据后,遍历 dataSource 数据,根据每个 tag ID 判断其是否出现在 selectedKeys 中。这样可以保证当页面加载时,只要存在对应的数据,就能够在已选框中展示相应的标签。

修改代码示例

  1. const fetchAvailableTags = () => {
  2. queryTagList().then((response) => {
  3. if (response.success) {
  4. dataSource.value = response.data.map((tag) => ({
  5. key: tag.id,
  6. title: tag.tagsDescription,
  7. color: tag.color || '#000',
  8. }));
  9. // 需要在这里根据当前 tagsId 更新 selectedKeys 和 targetKeys
  10. updateSelectedAndTargetKeys();
  11. } else {
  12. message.error(response.message);
  13. }
  14. });
  15. };
  16. const updateSelectedAndTargetKeys = () => {
  17. const existingIds = new Set(dataSource.value.map(tag => tag.key));
  18. // 更新 selectedKeys 和 targetKeys
  19. const matchedIds = existingIds.forEach(id => {
  20. if (selectedKeys.value.includes(id)) {
  21. targetKeys.value.push(id); // 将匹配到的 id 添加到 targetKeys 中
  22. }
  23. });
  24. // 如果没有匹配 id,需要清空或做处理。
  25. };
  26. // fetchSelectedTags 不需要更改,但一定要确保是在获得 dataSource 后调用。
  27. const fetchSelectedTags = () => {
  28. getCustomerTags({ customerId: custId.value }).then((response) => {
  29. if (response.success) {
  30. selectedKeys.value = response.data.map((tag) => tag.id);
  31. updateSelectedAndTargetKeys(); // 在这里根据更新后的 dataSource 来更新目标
  32. } else {
  33. message.error(response.message);
  34. }
  35. });
  36. };

总结

  • 确保你在执行选择操作之前已经加载了所有数据。
  • 将获取到的已选标签与可用标签进行比对,以便正确反映它们之间的关系。
  • 如果有多个状态需要同步,请考虑使用 computed 属性来减少重复代码。这样一来,你就能保持数据的一致性并且简化逻辑。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?