简体   繁体   English

如何基于xml节点的值分配类?

[英]How can I assign a class based on the value of an xml node?

I am using the for loop below to retrieve the data form a "x-ml" result set and I build at body out of the results. 我正在使用下面的for循环从“ x-ml”结果集中检索数据,然后根据结果构建正文。 What I would like to do is assign a class to several rows based on the nodes value. 我想做的就是根据节点值将一个类分配给几行。 For example: 例如:

-If Percent Reporting > 90% then assign class "green", -如果百分比报告> 90%,则将类别分配为“绿色”,

-If Percent Reporting > 70% and less than 90% then assign class "yellow", -如果百分比报告> 70%且小于90%,则将类别分配为“黄色”,

-If Percent Reporting < 70% then assign class "red". -如果报告百分比<70%,则将类别分配为“红色”。

I was thinking I could analyze this while I was building the t body and assign the classes individually to each td or cell, but I'm not sure how to go about it. 我当时想我可以在构建t主体并将每个类分别分配给每个td或单元时进行分析,但是我不确定该如何做。

Ultimately I want to conditional format three of my columns differently based on their values. 最终,我想根据它们的值以不同的方式设置其三列的条件格式。

Any ideas or tips would be much appreciated. 任何想法或技巧将不胜感激。

for (i = 0; i < x.length; i++) {
    tbody += "<tr><td>" + x[i].getElementsByTagName("Site")[0].childNodes[0].nodeValue + 
    "</td><td>" + x[i].getElementsByTagName("TotalUnits")[0].childNodes[0].nodeValue +
    "</td><td>" + x[i].getElementsByTagName("PercentReporting")[0].childNodes[0].nodeValue +
    "</td><td>" + x[i].getElementsByTagName("PercentNotReporting")[0].childNodes[0].nodeValue +
    "</td><td>" + x[i].getElementsByTagName("PercentBypassed")[0].childNodes[0].nodeValue +
    "</td><td>" + x[i].getElementsByTagName("NumberLogins")[0].childNodes[0].nodeValue +
    "</td><td>" + x[i].getElementsByTagName("NumberAlarms")[0].childNodes[0].nodeValue +
    "</td><td>" + x[i].getElementsByTagName("TotalEnergySavings")[0].childNodes[0].nodeValue +
    "</td></tr>";
}

Just calculate the class you want to add and add it to the row: 只需计算要添加的类,然后将其添加到行中即可:

for (i = 0; i < x.length; i++) {
var percentReporting = x[i].getElementsByTagName("PercentReporting")[0].childNodes[0].nodeValue;
    var class = "";
    if(percentReporting > 90)
        class = "green";
    else if(percentReporting > 70)
        class = "yellow";
    else
        class = "red";

    tbody += "<tr><td>" + x[i].getElementsByTagName("Site")[0].childNodes[0].nodeValue + 
    "</td><td class ='" + class +"'>" + x[i].getElementsByTagName("TotalUnits")[0].childNodes[0].nodeValue +
    "</td><td>" + x[i].getElementsByTagName("PercentReporting")[0].childNodes[0].nodeValue +
    "</td><td>" + x[i].getElementsByTagName("PercentNotReporting")[0].childNodes[0].nodeValue +
    "</td><td>" + x[i].getElementsByTagName("PercentBypassed")[0].childNodes[0].nodeValue +
    "</td><td>" + x[i].getElementsByTagName("NumberLogins")[0].childNodes[0].nodeValue +
    "</td><td>" + x[i].getElementsByTagName("NumberAlarms")[0].childNodes[0].nodeValue +
    "</td><td>" + x[i].getElementsByTagName("TotalEnergySavings")[0].childNodes[0].nodeValue +
    "</td></tr>";
}

In your loop just create a variable for your class and use some if conditions to set the class. 在循环中,只需为您的类创建一个变量,然后使用一些if条件来设置该类。

for (i = 0; i < x.length; i++) {
        var statusClass = 'good', //default value
            percentReporting = parseFloat(x[i].getElementsByTagName("PercentReporting")[0].childNodes[0].nodeValue);

        if (percentReporting < 70)
            statusClass = 'bad';
        else if (percentReporting < 90 && percentReporting > 70)
            statusClass = 'warning';

        tbody += "<tr><td>" + x[i].getElementsByTagName("Site")[0].childNodes[0].nodeValue +
            "</td><td>" + x[i].getElementsByTagName("TotalUnits")[0].childNodes[0].nodeValue +
            "</td><td class='" + statusClass + "'>" + percentReporting +
            "</td><td>" + x[i].getElementsByTagName("PercentNotReporting")[0].childNodes[0].nodeValue +
            "</td><td>" + x[i].getElementsByTagName("PercentBypassed")[0].childNodes[0].nodeValue +
            "</td><td>" + x[i].getElementsByTagName("NumberLogins")[0].childNodes[0].nodeValue +
            "</td><td>" + x[i].getElementsByTagName("NumberAlarms")[0].childNodes[0].nodeValue +
            "</td><td>" + x[i].getElementsByTagName("TotalEnergySavings")[0].childNodes[0].nodeValue +
            "</td></tr>";
    }

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

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