简体   繁体   English

根据 json 属性动态改变颜色

[英]Change colour dynamically according to json attribute

I have a json file in which am storing my data in a tree to generate some kind of a diagram, I want to be able to change the colour of the diagram conditionally according to an attribute in json, when it is false the colours becomes red, when it's true it becomes green.我有一个 json 文件,其中将我的数据存储在树中以生成某种图表,我希望能够根据 json 中的属性有条件地更改图表的颜色,当它为假时,颜色变为红色,当它为真时,它变为绿色。 This is my json file这是我的 json 文件

"children": [
    {
      "children": [
        {
          "children": [],
          "id": 50,
          "name": "gfj",
            "checked": true

        }
      ],
      "id": 51,
      "name": "malek"
    },
    {
      "children": [
        {
          "children": [
            {
              "children": [],
              "id": 49,
              "name": "nice",
                "checked": true

            }
          ],
          "id": 48,
          "name": "amira",
            "checked": false
        }
      ],
      "id": 47,
      "name": "mahdi"
    }
  ],





am able to get the attribute checked from the json file inside my js file but don't know how to change the colour given the colour is changed in css file我能够从我的 js 文件中的 json 文件中检查属性,但不知道如何更改颜色,因为 css 文件中的颜色已更改


_checked = function(d){ return d.checked; },

This is the css file:这是 css 文件:


/*
    Example fishbone styling... note that you can't actually change
    line markers here, which is annoying
*/

html, body{ margin: 0; padding: 0; overflow: hidden;}

/* get it? gill? */
*{ font-family: "serif", "serif"; }


.label-0{ font-size: 2em }
.label-1{ font-size: 1.5em; fill: #06405a; }
.label-2{ font-size: 1em; fill: #06405a; }
.label-3{ font-size: .9em; fill: #888; }
.label-4{ font-size: .8em; fill: #aaa; }

.link-0{ stroke: #188fc6; stroke-width: 3px}
.link-1{ stroke: #188fc6; stroke-width: 2px}
.link-2, .link-3, .link-4{ stroke: #188fc6; stroke-width: 2px; }



the links are the arrows in the diagram and every arrow has its own colour and size, my question how do I use the attribute checked that am getting in the js file to change the colour dynamically in the css file and thank you so much.链接是图中的箭头,每个箭头都有自己的颜色和大小,我的问题是如何使用在 js 文件中检查的属性来动态更改 css 文件中的颜色,非常感谢。

I guess you can't change dinamically your css file.我猜你不能改变你的 css 文件。 However, you can get the behavior you want by doing something similar to the scheme I propose below.但是,您可以通过执行类似于我在下面提出的方案的操作来获得所需的行为。

The point is to change your style dinamically, depending on the "checked" attribute of each of the items in your json.关键是根据 json 中每个项目的“已检查”属性来动态更改您的样式。 To do this, you can use, for instance, the Angular directive ngClass为此,您可以使用例如 Angular 指令ngClass

CSS CSS

istrueclass {
   color:green;
}

isfalseclass {
   color:red;
}
...

HTML HTML

<div *ngFor="child1 of children">
   <div *ngFor="child2 of child1">
      <div *ngFor="lastChild of child2">
                <span [ngClass]="{ 
                   'istrueclass': lastChild.checked,
                   'isfalseclass': lastChild.checked,
                }">
                   {{ lastChild.name }}
                </span>
   </div>
</div>

you can dynamically set the class value to html element based on what you get returned from json file.您可以根据从 json 文件返回的内容将 class 值动态设置为 html 元素。

for example:例如:

Javascript File Javascript 文件

//grab your html element
const htmlElement = document.getElementById('id_here');

if (_checked === true) {
   htmlElement.removeClass('true_case')
   htmlElement.addClass('false_case')
} else {
   htmlElement.removeClass('true_case')
   htmlElement.addClass('false_case')
}

And accordinlgy set the css styles for these classes.并且相应地为这些类设置了 css styles。

.true_case {
  color: green;
}
.false_case {
   color: red;
}

this solution might show some error [this error can be solved easily] but the approach is perfectly correct and can help you.此解决方案可能会显示一些错误[此错误可以轻松解决],但该方法是完全正确的并且可以帮助您。

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

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