如何合并或拆分重叠的日期?

问题描述 投票:0回答:1

我目前陷入一个问题并试图找到可行的解决方案。对于上下文,我正在使用 EXTJS,在我们的系统中,我们有一个选项卡面板,每个选项卡内都有一个包含数据的网格。我们可以放下东西,动态创建选项卡并填充网格。我们删除的内容有持续时间、日期列表,我们修改选项卡以反映这些日期。例如,我删除了具有日期 (1/9) 的内容,创建了一个标题为 1/9 的选项卡,并将该日期的数据加载到网格中。每当删除一个项目时,服务器都会正确返回日期列表,并在此基础上创建选项卡并使用日期修改标题。日期可以是单个日期,也可以是自己的列表。这里有两种类型的数据返回。类型 1:

[1/8,1/9]
和类型 2
[ [1/8 ], [1/9] ]
或两者的组合。
[1/5, [1/6, 1/7], 1/8]

我正在做的是循环列表,如果当前项目是一个数组,我将连接日期(1/6 - 1/7),为列表中的每个项目创建一个选项卡(上例中为 3)。日期可以是不连续的或连续的。

问题是在删除某些内容之前,可能存在带有自己日期的现有选项卡,也许与我们从服务器返回的日期相同。我想要的是根据条件合并或分离重叠日期。我想忽略从不重叠的服务器返回的日期。例如,如果有一个带有日期

(1/5, 1/6) 
的选项卡,并且我从服务器返回
(1/7
),我们只需忽略,不会创建任何选项卡。

示例:

给定一个带有日期的选项卡:

[1/5、1/6、1/7、1/8、1/9]

放下物品后,我会从服务器返回[ [1/8], [1/9] ]

我想修改第一个选项卡并将其更改为 1/5 - 1/8,然后创建第二个选项卡并指定 1/9 作为日期。

原因是因为如果我要删除项目并从服务器返回

[ [1/8], [1/9] ] 而没有任何先前的选项卡,我们将有 2 个选项卡,因为它是列表中的列表。

给定一个带有日期的选项卡:

[1/5、1/6、1/7、1/8、1/9]

放下物品后,我从服务器取回

[1/8, 1/9] 我想要 1 个带有日期的选项卡

1/5 - 1/9

原因:因为日期是单个的并且不在列表中列出,并且它们都与选项卡日期重叠,所以我们不添加或修改选项卡

日期不重叠示例

给定一个带有日期的选项卡:

[1/9]

丢弃物品后,我会从服务器返回

[1/8, 1/9]

[ [1/8], [1/9] ](取决于物品掉落)

我想要 1 个日期为 1/9 的选项卡,因为该选项卡没有 1/8,因此日期会被忽略。

可能有多个选项卡,其中单个/多个日期重叠或不重叠服务器返回的日期。

我的做法:

    我正在迭代现有选项卡并获取每个选项卡的日期。
  1. 我正在与服务器日期进行比较,看看是否发生重叠
  2. 如果选项卡没有任何日期或选项卡日期不与服务器日期重叠,我会忽略该选项卡
  3. 我没有实际的代码,因为我被困在逻辑部分,但这里是我尝试过的伪代码(javascript语言)

Exp:存在 1 个带有日期的选项卡

[1/5, 1/6, 1/7, 1/8, 1/9]

并且我删除了项目并且服务器返回 [ [1/8, 1/9] ] const itemDates = content; (from server) tabs.forEach(function(tab, index){ let tabDates = tab.getDates(); // skip if tab has no dates if(tabDates.length > 0) { // use tab index to retrieve date from itemDates const currentItemDate = itemDates[index]; // it could be null since index can be out of bounds if(currentItemDate){ //we will ignore if currentItemDate is a list because it either has to be already present in the tab //or it will be dates that are not overlapping // if single date, we want to create new tab as neccessary if(!Array.isArray(currentItemDate){ // there is at least one date overlap if(tabDates.some(date => currentItemDate.includes(date)) { } // } } } });


javascript extjs
1个回答
0
投票

function updateTabs(existingTabs, serverDates) { serverDates.forEach(serverDate => { // Check if serverDate is an array (list of dates) or a single date if (Array.isArray(serverDate)) { // Handle the case where serverDate is a list of dates serverDate.forEach(date => { updateTabs(existingTabs, [date]); // Recursively handle each date in the list }); } else { // Handle the case where serverDate is a single date let overlappingTabs = existingTabs.filter(tab => tabHasOverlap(tab, serverDate)); if (overlappingTabs.length === 0) { // No overlapping tabs, create a new tab with the single date createNewTab(serverDate); } else { // Overlapping tabs exist, update them overlappingTabs.forEach(tab => updateTab(tab, serverDate)); } } }); } function tabHasOverlap(tab, date) { // Check if the given date overlaps with any date in the tab return tab.getDates().some(tabDate => date.includes(tabDate) || tabDate.includes(date)); } function createNewTab(date) { // Create a new tab with the given date // You may implement the logic to create a new tab here console.log("Create new tab with date:", date); } function updateTab(tab, date) { // Update the existing tab with the given date // You may implement the logic to update a tab here console.log("Update tab with date:", date); } // Example usage: const existingTabs = [ { getDates: () => ["1/5", "1/6", "1/7", "1/8", "1/9"] }, // Add other existing tabs as needed ]; const serverDates = [["1/8", "1/9"]]; updateTabs(existingTabs, serverDates);

© www.soinside.com 2019 - 2024. All rights reserved.