简体   繁体   English

如何基于5个布尔值显示或隐藏元素?

[英]How to show or hide element based on 5 boolean values?

I have a page that has "items" on it that you can drag around. 我的页面上有“项目”,您可以在页面上拖动。 There are filters that the user can apply to only show an item based on a certain criteria. 用户可以应用某些过滤器以仅基于特定条件显示项目。

There are also folders that the user can see on the same page where various items are stored. 用户还可以在同一页面上看到存储各种项目的文件夹。 The user can click on a folder to view its items. 用户可以单击文件夹以查看其项目。

I am having trouble with the logic for showing or hiding items based on the filters. 我在基于过滤器显示或隐藏项目的逻辑上遇到麻烦。 (Also, I am using AngularJS's ng-class directive to apply an "item-showing" CSS class). (此外,我正在使用AngularJS的ng-class指令应用“显示项目”的CSS类)。

For example, I want to: 例如,我要:

Always hide the item if the item is in a different folder than the currently viewed folder. 如果项目位于与当前查看的文件夹不同的文件夹中,则始终隐藏该项目。

Otherwise: If the "must be starred" filter is applied, and the item is starred, show it. 否则:如果应用了“必须加星标”过滤器,并且该项目已加星标,请显示它。
OR 要么
If the "must be complete" filter is applied, and the item is marked as complete, show it. 如果应用了“必须完成”过滤器,并且该项目被标记为完成,则显示它。

If the "must be starred" and "must be complete" filters are applied, and the item is neither starred nor complete, hide it. 如果应用了“必须加注星标”和“必须加注星标”过滤器,并且该项目既未加注星标又未完成,则将其隐藏。

I've tried several different ways of doing this, such as: 我尝试了几种不同的方法来做到这一点,例如:

class="item" ng-class="{'item-showing': item.isInCurrentFolder && ((item.mustBe.starred && item.starred) || (item.mustBe.complete && item.complete))}"

CSS: CSS:

.item {
    /* various styles */
    display: none;
}

.item-showing {
    display: block;
}

Each item's JavaScript properties look similar to this: 每个项目的JavaScript属性看起来都与此类似:

{
    // various properties, and then...

    isInCurrentFolder: true,
    starred: true,
    complete: true,
    mustBe: {
        starred: false,
        complete: false
    }
}

I have a service that will change mustBe.starred and mustBe.complete to true or false , depending on whether the user clicks a checkbox to apply the "must be starred" or "must be complete" filters. 我有一项服务,可以将mustBe.starredmustBe.complete更改为truefalse ,具体取决于用户单击复选框以应用“必须加星标”还是“必须完成”过滤器。

Long story short, the logic is flawed, and/or the architecture for each item model is flawed. 长话短说,逻辑有缺陷,和/或每个项目模型的体系结构都有缺陷。 Clicking the checkboxes to apply the filters might hide an item, when it should show it, etc. 单击复选框以应用过滤器可能会隐藏项目,何时显示项目等。

Any ideas? 有任何想法吗?

Let's break down your logic. 让我们分解一下逻辑。

  • Always hide the item if the item is in a different folder than the currently viewed folder 如果项目位于与当前查看的文件夹不同的文件夹中,则始终隐藏该项目

     item.isInCurrentFolder 

    If the condition is not met, we won't be even testing any further. 如果不满足条件,我们将不再进行任何测试。

  • If the "must be starred" filter is applied, and the item is starred, show it. 如果应用了“必须加星标”过滤器,并且该项目已加星标,请显示它。

    Rephrased: If we don't care about starring, or if we care and it is starred 改写: 如果我们不关心主演,或者我们关心并且已主演

     !item.mustBe.starred || item.starred 
  • If the "must be complete" filter is applied, and the item is marked as complete, show it. 如果应用了“必须完成”过滤器,并且该项目被标记为完成,则显示它。

    Apply the same reasoning 应用相同的推理

     !item.mustBe.complete || item.complete 
  • Combine logic 结合逻辑

    and them, as each individual condition must be met 它们,因为必须满足每个单独的条件

     item.isInCurrentFolder && (!item.mustBe.starred || item.starred) && (!item.mustBe.complete || item.complete) 

Little demo in action: http://jsfiddle.net/vkQtp/ 实际操作中的小演示: http : //jsfiddle.net/vkQtp/

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

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