将对象数组从一个数组移动到另一个数组,单击按钮。

huangapple go评论107阅读模式
英文:

Moving array of object from one array to another array on button click

问题

在上面的代码中,我已经实现了我的逻辑。

英文:

I have two array of object and two button two move data from one to another after selecting a single or multiple data, but it is not working properly in Nextjs project.

Here is my code snippet! it works in certain case but failed in some cases also.

when I click the arrowfunction button it's work when slecting single data properly but give strange result selecting multiple data.

I have tried many other ways! but none of them was solving the problem. It would be great if anyone of you help in this problem.

  1. const [riskSummary, setRiskSummary] = useState<Ser[]>([
  2. {
  3. ser: {
  4. id: '1',
  5. url: 'https://example.com',
  6. text: '株式会社ABC 退会/解約率 - ブログ',
  7. },
  8. search_engine_source: {
  9. search_engine: SearchEngine.GooglePc,
  10. detail: SearchEngineDetail.Suggestion,
  11. },
  12. isChecked: false,
  13. },
  14. {
  15. ser: {
  16. id: '2',
  17. url: 'https://example.com',
  18. text: 'Longwebsitename|SampleSample|SampleSampleSampleSample...',
  19. },
  20. search_engine_source: {
  21. search_engine: SearchEngine.GooglePc,
  22. detail: SearchEngineDetail.Suggestion,
  23. },
  24. isChecked: false,
  25. },
  26. ]);
  27. const [neutralSummary, setNeutralSummary] = useState<Ser[]>([
  28. {
  29. ser: { id: '3', url: 'https://example.com', text: 'title' },
  30. search_engine_source: {
  31. search_engine: SearchEngine.GooglePc,
  32. detail: SearchEngineDetail.Suggestion,
  33. },
  34. isChecked: false,
  35. },
  36. {
  37. ser: { id: '4', url: 'https://example.com', text: 'title' },
  38. search_engine_source: {
  39. search_engine: SearchEngine.GooglePc,
  40. detail: SearchEngineDetail.Suggestion,
  41. },
  42. isChecked: false,
  43. },
  44. {
  45. ser: { id: '5', url: 'https://example.com', text: 'title' },
  46. search_engine_source: {
  47. search_engine: SearchEngine.GooglePc,
  48. detail: SearchEngineDetail.Suggestion,
  49. },
  50. isChecked: false,
  51. },
  52. ]);
  53. const handleRiskSummary = (index: number) => {
  54. const updatedListItems = [...riskSummary];
  55. updatedListItems[index].isChecked = !updatedListItems[index].isChecked;
  56. setRiskSummary(updatedListItems);
  57. };
  58. const handleNeutralSummary = (index: number) => {
  59. const updatedListItems = [...neutralSummary];
  60. updatedListItems[index].isChecked = !updatedListItems[index].isChecked;
  61. setNeutralSummary(updatedListItems);
  62. };
  63. const handleArrowLineRightClick = () => {
  64. const selectedItems = neutralSummary.filter((item) => item.isChecked);
  65. const updatedRiskSummary = [...riskSummary];
  66. const updatedNeutralSummary = neutralSummary.filter(
  67. (item) => !item.isChecked,
  68. );
  69. selectedItems.forEach((item) => {
  70. const newItem = {
  71. ...item,
  72. ser: { ...item.ser, id: uuidv4() }, // Generate unique IDs using uuidv4
  73. isChecked: false,
  74. };
  75. updatedRiskSummary.push(newItem);
  76. });
  77. setRiskSummary(updatedRiskSummary);
  78. setNeutralSummary(updatedNeutralSummary);
  79. };
  80. const handleArrowLineLeftClick = () => {
  81. const selectedItems = riskSummary.filter((item) => item.isChecked);
  82. const updatedNeutralSummary = [...neutralSummary];
  83. const updatedRiskSummary = riskSummary.filter((item) => !item.isChecked);
  84. selectedItems.forEach((item) => {
  85. const newItem = {
  86. ...item,
  87. ser: { ...item.ser, id: uuidv4() }, // Generate unique IDs using uuidv4
  88. isChecked: false,
  89. };
  90. updatedNeutralSummary.push(newItem);
  91. });
  92. setNeutralSummary(updatedNeutralSummary);
  93. setRiskSummary(updatedRiskSummary);
  94. };
  95. <List
  96. listItems={neutralSummary}
  97. listTitle="中立まとめ"
  98. onChange={handleNeutralSummary}
  99. />
  100. </Flex>
  101. <Flex
  102. direction="col"
  103. className="col-span-2 h-max self-center"
  104. alignItems="center"
  105. >
  106. <Button
  107. onClick={handleArrowLineRightClick}
  108. className="!bg-secondary hover:!bg-neutral"
  109. iconName="ArrowLineRight"
  110. />
  111. <Button
  112. onClick={handleArrowLineLeftClick}
  113. className="!bg-secondary hover:!bg-neutral"
  114. iconName="ArrowLineLeft"
  115. />
  116. </Flex>
  117. <Flex direction="col" className="col-span-5 h-max">
  118. <List
  119. listItems={riskSummary}
  120. listTitle="リスクまとめ"
  121. onChange={handleRiskSummary}
  122. />
  123. </Flex>

In the avobe code I have implemented my logic!

答案1

得分: 0

所有的代码逻辑都是正确的!但问题是将相同的文本用作三个对象的标题!当我将名称更改为title1、title2、title3时,问题就解决了。

英文:

ALl the code logic here was right! but the problem was keeping the same text for three objects as title! when I changed the name to tiltle1,title2,title3, the problem solved

huangapple
  • 本文由 发表于 2023年6月22日 04:47:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定