简体   繁体   English

如何使用条件过滤javascript甘特图并处理空输入?

[英]How do I filter a javascript gantt chart using conditions and handle empty inputs?

I've recently started using the DHTMLX javascript based Gantt chart . 我最近开始使用基于DHTMLX javascript的甘特图 I'm having some issues with the filters which I don't feel are exclusively related to the chart itself, but, related to javascript. 我的过滤器出现了一些问题,我认为这些问题并不完全与图表本身有关,但是与javascript有关。

So, the Gantt chart can be filtered by passing in some conditions via an if statement and returning true when those conditions are met. 因此, 可以通过通过if语句传入某些条件并在满足这些条件时返回true 来过滤甘特 All my conditions values come from a selection of inputs, these inputs represent ranges. 我所有的条件值均来自选择的输入,这些输入代表范围。

<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>

When you click on the filter button the on click event is triggered and the values are gathered from the input fields. 当您单击过滤器按钮时,将触发on单击事件,并且将从输入字段中收集值。

$("#filterSort").on("click", function (e) {        
    let bedsStart = $('#bedsStart').val();
    let bedsEnd = $('#bedsEnd').val();
    let deckStart = $('#deckStart').val();
    let deckEnd = $('#deckEnd').val();

These values are then passed as conditions to the filter as per the documentation from DHTMLX: 然后根据DHTMLX的文档将这些值作为条件传递给过滤器:

    gantt.attachEvent("onBeforeTaskDisplay", function (id, task) {
        if (task.crane >= craneStart && task.crane <= craneEnd) {
            return true
        }        
        return false;
    });
    gantt.init("ganttReport");
}

The problem is if any of the text inputs are empty/null then the filter fails and returns nothing. 问题是,如果任何文本输入为空/空,则过滤器将失败并且不返回任何内容。 Is there a way I can handle these nulls values by not including them or dynamically specify what filters to use? 有没有一种方法可以通过不包含它们或动态指定要使用的过滤器来处理这些null值?

The filter seems to only be capable of returning true or false so having multiple if statements don't work as it's always the last statement that will deliver an outcome with the previous iterations being overwritten ie 该过滤器似乎只能返回truefalse因此具有多个if语句不起作用,因为它始终是最后一个将覆盖先前迭代而提供结果的语句,即

if(condition1)
{
    return true
}
if(condition2)
{
    return true
}
return false

The above code wouldn't work and only condition2 would be used as condition1 would be overwritten. 上面的代码将无法正常工作,只能使用condition2,因为condition1将被覆盖。 So I can't just set out my conditions in this manner or using if else . 所以我不能仅仅以这种方式设定我的条件或使用if else

Can anyone offer a solution to how I can pass all these conditions to this single statement yet handle the nulls? 谁能提供一种解决方案,说明我如何将所有这些条件传递给此单个语句而又处理空值?

这是一个有效的示例,您可以尝试仅使用startDate / endDate或同时使用这两种方法: https : //codesandbox.io/s/7qz1kw9vq

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM