我最近开始使用DHTMLX javascript based Gantt chart。我有一些过滤器的问题,我觉得这些问题与图表本身无关,但与javascript有关。
因此,甘特chart can be filtered通过if
声明传递一些条件,并在满足这些条件时返回true
。我的所有条件值都来自一系列输入,这些输入代表范围。
<input type="text" class="k-textbox" id="bedsStart" />
<input type="text" class="k-textbox" id="bedsEnd" />
<input type="text" class="k-textbox" id="deckStart" />
<input type="text" class="k-textbox" id="deckEnd" />
<button type="button" id="filterSort">Set Filters</button>
单击过滤器按钮时,将触发单击事件,并从输入字段收集值。
$("#filterSort").on("click", function (e) {
let bedsStart = $('#bedsStart').val();
let bedsEnd = $('#bedsEnd').val();
let deckStart = $('#deckStart').val();
let deckEnd = $('#deckEnd').val();
然后根据DHTMLX的文档将这些值作为条件传递给过滤器:
gantt.attachEvent("onBeforeTaskDisplay", function (id, task) {
if (task.crane >= craneStart && task.crane <= craneEnd) {
return true
}
return false;
});
gantt.init("ganttReport");
}
问题是如果任何文本输入为空/ null,则过滤器失败并且不返回任何内容。有没有办法可以处理这些空值,不包括它们或动态指定要使用的过滤器?
过滤器似乎只能返回true
或false
,因此有多个if
语句不起作用,因为它始终是最后一个语句,将导致前一次迭代被覆盖的结果,即
if(condition1)
{
return true
}
if(condition2)
{
return true
}
return false
上面的代码不起作用,只会使用condition2,因为condition1会被覆盖。所以我不能以这种方式或使用if else
来阐述我的条件。
任何人都可以提供一个解决方案,我可以将所有这些条件传递给这个单一的语句,但处理空值?
这是一个工作示例,您可以尝试只使用startDate / endDate或两者:https://codesandbox.io/s/7qz1kw9vq