在多个日期分发事件

时间:2018-10-03 11:04:17

标签: javascript c react-native matrix

您好,我有一个日历视图(3天视图),我必须在3天视图上显示多个日间事件。我无法生成事件矩阵

3/10/18 | 4/10/18 | 5/10/18
3.......|3........|9.......
........|5........|5.......
7.......|7........|........

3 ....,5 ....,9 ....和7 ....是多个日期事件,它们以3 x 3矩阵分层。

一般的事件结构是这样的

{ id: 123, name: "3", startTimestamp: 1538524800 endTimestamp: 1538611200 }

我尝试过:-

  1. 遍历日期,并为每个日期查找该日期中的事件。
  2. 每个日期和事件的查找矩阵

    但是我无法将日期中的事件对齐,例如3个事件在[0] [0]和[0] [1]处对齐,而5个在[1] [1]和[1] [2]处对齐]

我在向[0] [2]的空白空间添加9时遇到了问题,我正在获取每一列的索引,并且在[2] [2]的空白处留有9。

这是我当前的代码段。

let events = [{ id: 3, name: "3", start: 3, end: 4}, { id: 5, name: "5", start: 4, end: 5}, { id: 7, name: "7", start: 3, end: 4}, { id: 9, name: "9", start: 5, end: 9}];

let dates = [3, 4, 5];

function abc1 () {
 let result = [
  [
   undefined, undefined, undefined
  ], [
   undefined, undefined, undefined
  ], [
   undefined, undefined, undefined
  ]
 ];
 for (let i=0; i<dates.length; i++) {
  let date = dates[i];
  let index = 0;
  for (let j=0; j<events.length; j++) {
   let event = events[j];
   if (event.start<= date && date <= event.end) {
    result[index][i] = event.id;
    index ++;
   }
  }
 }
 console.log("Result ", result);
};

Result  [[3, 3, 5], [7, 5, 9], [undefined, 7, undefined]]

2 个答案:

答案 0 :(得分:0)

您是否尝试过使用3维数组?

答案 1 :(得分:0)

struct EventsModel {
    var id:Int
    var name: String
    var start:Int
    var end:Int
}
let events = [EventsModel(id: 3, name: "3", start: 3, end: 4), EventsModel(id: 5, name: "5", start: 4, end: 5 ), EventsModel(id: 7, name: "7", start: 3, end: 4), EventsModel(id: 9, name: "9", start: 5, end: 9)]

var dates:[Int] = [3, 4, 5]
var finaloutput:[[String]] = []
dates = dates.sorted()
for event in events {
    var outEventArr:[String] = []
    for date in dates {
        if date >= event.start && date <= event.end {
            outEventArr.append(event.name)
        } else {
            outEventArr.append("undefined")
        }
    }
    finaloutput.append(outEventArr)
}
print(finaloutput)

输出:-:[[“ 3”,“ 3”,“未定义”],[“未定义”,“ 5”,“ 5”],[“ 7”,“ 7” ,“ undefined”],[“ undefined”,“ undefined”,“ 9”]]

PS:-请原谅我迅速编写代码。