如何更改 tr.append($("<td>" 中 table 数据的文本颜色

how to change the text color of table data in tr.append($("<td>"

我想查看 table 数据 状态 文本颜色在活动时为红色,在非活动时为绿色。

我尝试了很多不同的方法但都失败了。任何有用的建议将不胜感激。 感谢您的时间和精力。 怎么可能?

HTML 代码如下:

<form>
    <div class="form-group">
        <label for="upload-csvd43">Select Payload csv file</label>
        <input type="file" class="form-control-file" id="upload-csvd43" accept=".csv">
    </div>
</form>
<button type="submit" class="btn btn-success" id="btn-upload-csvd43">Parse Data</button>
<button type="reset" class="btn btn-danger" onclick="resetpage()" >Reset</button>

<div class="container">
    <br><br>
    <table class="table table-bordered table-hover table-condensed" id="countryTable">
        <thead>
        <tr>
            <th>Sr.</th>
            <th>Date</th>
            <th>Data</th>          
            <th>Status</th>
        </tr>
        </thead>
        <tbody>

        </tbody>
    </table>

<br>
<br>
</div>

我使用以下脚本来解析 table 数据。 papaparse 用于解析 csv 文件。

<script type="text/javascript">
   document.getElementById('btn-upload-csvd43').addEventListener('click', ()=> {
        Papa.parse(document.getElementById('upload-csvd43').files[0], {
            download: true,
            header: true,
            complete: function(results) {
              
                let countKey = Object.keys(results.data).length;
                var tbody = $("#countryTable > tbody");
                tbody.empty();
               for (let i=0; i<countKey-1;i++)
               {
                       var raw = results.data[i].Data;
                       let Status = 'Inactive';
                       let tp= ((parseInt(raw.slice(1,2),16))>>2) & 0x01;
                       if (tp)
                           status = 'Active';
                      
                       var tr = $("<tr>");
                       tr.append($("<td>", {
                           'text': i+1
                       }));
                       tr.append($("<td>", {
                           'text': results.data[i].Timestamp
                       }));
                       tr.append($("<td>", {
                           'text': results.data[i].Data
                       }));
                       
                       tr.append($("<td>", {
                           'text': status
                       }));
                       
                       tbody.append(tr);
                   }

               }


            }
        });

    });

    function resetpage() {
        window.location.reload();
        }

</script>

您可以检查 status 值是否为 Active/Inactive,具体取决于使用 'class': status == "Active" ? "red_color" : "green_color" 添加所需的 class 到 td。

演示代码 :

//this is just for demo...
var results = {
  "data": [{
    "Data": "12734934394848484415",
    "Timestamp": "12:30"
  }, {
    "Data": "157349343948484844153",
    "Timestamp": "11:30"
  }]
}
document.getElementById('btn-upload-csvd43').addEventListener('click', () => {
  /* Papa.parse(document.getElementById('upload-csvd43').files[0], {
       download: true,
       header: true,
       complete: function(results) {*/

  let countKey = 3;
  var tbody = $("#countryTable > tbody");
  tbody.empty();
  for (let i = 0; i < countKey - 1; i++) {
    var raw = results.data[i].Data;
    let status = 'Inactive';
    let tp = ((parseInt(raw.slice(1, 2), 16)) >> 2) & 0x01;

    if (tp)
      status = 'Active';

    var tr = $("<tr>");
    tr.append($("<td>", {
      'text': i + 1
    }));
    tr.append($("<td>", {
      'text': results.data[i].Timestamp
    }));
    tr.append($("<td>", {
      'text': results.data[i].Data
    }));
    //check if status is active/incative add required class
    tr.append($("<td>", {
      'text': status,
      'class': status == "Active" ? "red_color" : "green_color"
    }));

    tbody.append(tr);

  }


  /*}
    }
  });*/

});
.red_color {
  color: red
}

.green_color {
  color: green
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
  <div class="form-group">
    <label for="upload-csvd43">Select Payload csv file</label>
    <input type="file" class="form-control-file" id="upload-csvd43" accept=".csv">
  </div>
</form>
<button type="submit" class="btn btn-success" id="btn-upload-csvd43">Parse Data</button>
<button type="reset" class="btn btn-danger" onclick="resetpage()">Reset</button>

<div class="container">
  <br><br>
  <table class="table table-bordered table-hover table-condensed" id="countryTable">
    <thead>
      <tr>
        <th>Sr.</th>
        <th>Date</th>
        <th>Data</th>
        <th>Status</th>
      </tr>
    </thead>
    <tbody>

    </tbody>
  </table>

  <br>
  <br>
</div>