简体   繁体   English

更改jQuery Ajax表中一行的字体颜色

[英]change font color of a row in jquery ajax table

I want to change the font color of values in a row based on the condition that applied inside the function. 我想根据函数内部应用的条件更改一行中的值的字体颜色。 If the TotalStudent is greater than room capacity, the student information gets added to the table with the font color of red. 如果TotalStudent大于房间容量,则将学生信息以红色字体添加到表中。 Below is what I've tried. 以下是我尝试过的方法。 I've used ajax method and within success function I'm generating table to insert values. 我使用了ajax方法,并在成功函数中生成表以插入值。 The condition is running fine. 条件运行良好。

Create.cshtml Create.cshtml

$.ajax({
  type: "POST",
  url: '@Url.Action("AddStudent", "Student")',
  data: model,
  dataType: "json",
  success: function(data) {
    $('#dialog').dialog('close');
    alert(data.countstudent);
    alert(data.roomcapacity);
    //var student = data.countstudent;
    if (data.countstudent > data.roomcapacity) {
      var tblEndrolled = $("#tblEnrolled");
      $.each(data.record, function(index, item) {
        $('.status').css("color", "red");
        var tr = $("<tr></tr>");
        tr.html(("<td>" + '<input type="submit" id="' + item.Id + '" value="Remove" class="" />' + "</td>") +
                " " + ("<td class = 'status'>" + item.FirstName + "</td>") +
                " " + ("<td>" + item.LastName + "</td>") +
                " " + ("<td>" + item.EmailAddress + "</td>") +
                " " + ("<td>" + item.Phone + "</td>"));
        tblEndrolled.append(tr);
      });
    }

    <div>
      <strong style = "font-size:20px" > Enrolled Students: < /strong> 
        <table class = "table table-bordered table-responsive table-hover" id="tblEnrolled">
        <tr>
        <th>Action</th>
                @*<th>User Id</th>
                *@<th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
                <th>Phone</th>
        </tr>
        </table> 
        </div>

$('.status').css("color", "red"); just change color on all elements which already had class status. 只需更改所有具有类状态的元素的颜色即可。 So, if your change $('.status')... and row additional your new row will have red color. 因此,如果更改$('.status')...并增加一行,则新行将显示为红色。 Indeed same colors will have all elements with status class. 实际上,相同的颜色将具有status类的所有元素。

Put the class in tr tag 将课程放在tr标签中

 var tr = $("<tr class = 'status'>></tr>");

Remove this ( use are setting style before you are generation the tr ) 删除此代码(在生成tr之前使用设置风格)

$('.status').css("color", "red");

And add class in css file 并在css文件中添加类

.status {
  color:red;
}

OR 要么

set style after tr is appended to table. 将tr附加到表后设置样式。

tblEndrolled.append(tr);
$('.status').css("color", "red");

Demo 演示版

 .status { color:red; } 
 <table class = "table table-bordered table-responsive table-hover" id="tblEnrolled"> <tr> <th>Action</th> <th>First Name</th> <th>Last Name</th> <th>Email</th> <th>Phone</th> </tr> <tr class="status"> <td>test</td> <td>test</td> <td>test</td> <td>test</td> </tr> </table> 

It needs two modication : 它需要两种治疗方法:

  1. 1st just add class status at the creation of tr. 第一个只是在创建tr时添加类状态。
  2. 2nd apply css style at the end of each loop, like below: 第二个在每个循环的末尾应用css样式,如下所示:

.

//var student = data.countstudent;
if (data.countstudent > data.roomcapacity) {
    var tblEndrolled = $("#tblEnrolled");
    $.each(data.record, function(index, item) {
    var tr = $("<tr class = 'status'></tr>");
        tr.html(("<td>" + '<input type="submit" id="' + item.Id + '" value="Remove" class="" />' + "</td>") +
                " " + ("<td >" + item.FirstName + "</td>") +
                " " + ("<td>" + item.LastName + "</td>") +
                " " + ("<td>" + item.EmailAddress + "</td>") +
                " " + ("<td>" + item.Phone + "</td>"));
        tblEndrolled.append(tr);
    });
    $('.status').css("color", "red");
}

I hope it will fulfill your requirement. 希望它能满足您的要求。

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

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