简体   繁体   English

使用jQuery突出显示数据表行不起作用

[英]Using jQuery to highlight datatable row not working

I have a feeling that this is going to be rather simple to answer and that I am missing something rather minor. 我感觉这将很容易回答,而且我缺少一些细微之处。

So here it goes. 所以就到这里。

What I have is a table that is being populated based off some mySQL. 我所拥有的是基于某些mySQL填充的表。 The datatable code looks like this: 数据表代码如下所示:

$("#etlTable").DataTable({
    "dom": '<"top"iflp<"clear">>rt<"bottom"iflp<"clear">>',
    "iDisplayLength": 100,
    "ordering": false,
    "autowidth": true,
    "columns": [{ "data": "file_name","class": "nowrap" }, 
                { "data": "start_time", "class": "nowrap" },
                { "data": "end_time"},
                { "data": "duration"},
                { "data": "outcome", "class": "chk"}, 
                { "data": "client_name" },
                { "data": "details" }
               ],
    "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
        if (aData[4] == "Fail") {
            $(nRow).children().each(function (index, td) {
                $(this).addClass('res');
            });
        }
    }
});

I think that this might be it seems to be the if statement that is causing the issue. 我认为这可能是导致问题的if语句。 But I am not sure what to do next. 但是我不确定下一步该怎么做。

Ideally I would like to highlight the table row when the 'Outcome' column value = "Fail" 理想情况下,当“结果”列值=“失败”时,我想突出显示表格行

I can get it to work without the If Statement in there, but that just hightlights the whole table which is not very helpful to me. 我可以在没有If语句的情况下正常工作,但这只是突出了整个表,对我不是很有帮助。

Example of Table row 表行示例

<tr role="row" class="odd">
    <td class=" nowrap">Customer1_File</td>
    <td class=" nowrap">2014-10-22</td>
    <td>2014-10-22</td>
    <td>00:25:26</td>
    <td>Fail</td>
    <td>Client_name</td>
    <td>Job_Code_Details</td>
</tr>

This is what I was using before, but it doesn't work since the table is loaded after this is ran: 这是我以前使用的方式,但是由于在运行该表之后将其加载,因此它不起作用:

<script type="text/javascript">
    var i = 0;
    var x = document.getElementsByClassName("chk");

    while (i <= x.length) {
        document.getElementsByClassName("chk")[i].className = "res";
        x = document.getElementsByClassName("chk");
    }; 

</script>

If I do it this way: 如果我这样做:

"fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
     $(nRow).children().each(function (index, td) {
         $(this).addClass('res');
     });
}

It highlights my whole table. 它突出了我的整个桌子。

I am pretty new to JQuery/Javascript ( as in this is my first project, I took it over from someone else and trytingo to piece this thing together and make some improvements. ) 我对JQuery / Javascript非常陌生(因为这是我的第一个项目,我从别人那里接管了它,然后尝试将它们拼凑在一起并进行一些改进。)

So my question is, what I am I doing wrong here? 所以我的问题是,我在这里做错了什么? How Can I highlight the row of a table based of the cell value? 如何根据单元格值突出显示表格的行?

You have a typo in the first column definition, but I suspect that's only in your example code above rather than your real code, otherwise you would have noticed. 您在第一列定义中有一个错字,但是我怀疑这只是在您上面的示例代码中,而不是您的真实代码中,否则您会注意到。

Try this for your row callback: 为您的行回调尝试以下操作:

"fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
    if (aData[4] == "Fail") {
        $(nRow).addClass('res');
    }
}

I can see you are using dataTables 1.10.x . 我可以看到您正在使用dataTables 1.10.x In this version, it is important to declare the CSS "correct" (so it works with the built in CSS being injected) like this : 在此版本中,声明CSS的“正确”非常重要(这样才能与注入的内置CSS一起使用),如下所示:

table.dataTable tr.highlight {
    background-color: lime; 
}

and then declare the fnRowCallBack like this (example) : 然后像这样声明fnRowCallBack(示例):

var table = $('#example').DataTable({
    fnRowCallback: function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
       if (aData[3] == "Fail") {
           $(nRow).addClass('highlight');
       }
    }    
});

see demo -> http://jsfiddle.net/wqbd6qeL/ ...1.10.x on a paginated table. 请参阅分页表上的演示-> http://jsfiddle.net/wqbd6qeL/ ... 1.10.x。


Edit : I see it is almost identical to @John-NotANumber's answer, except for the CSS. 编辑 :我看到它几乎与@ John-NotANumber的答案相同,除了CSS。

Okay so the thing that I was doing wrong was that I was using JSON and trying to access it as an array. 好吧,我做错了的事情是我正在使用JSON并尝试将其作为数组访问。

$("#etlTable").DataTable({
    "dom": '<"top"iflp<"clear">>rt<"bottom"iflp<"clear">>',
    "iDisplayLength": 100,
    "ordering": false,
    "autowidth": true,
    "columns": [{ "data": "file_name","class": "nowrap" }, 
            { "data": "start_time", "class": "nowrap" },
            { "data": "end_time"},
            { "data": "duration"},
            { "data": "outcome", "class": "chk"}, 
            { "data": "client_name" },
            { "data": "details" }
           ],
    "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
        if (aData[4] == "Fail") {
            $(nRow).children().each(function (index, td) {
            $(this).addClass('res');
            });
        }
    }
});

because it is an array, and they have an alias, I had to do this instead: 因为它是一个数组,并且有一个别名,所以我不得不这样做:

"fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
     if (aData['outcome'] == "Fail") {
             $(nRow).addClass('highlight');
             $(nRow).css('background-color', '#FFFF00');
     }

     console.log(aData['outcome']);

} }

Notice this part here: aData['outcome'] 在这里注意这一部分:aData ['outcome']

to find this I had to add this: console.log(aData['outcome']); 为了找到这个,我必须添加这个:console.log(aData ['outcome']);

It now works brilliantly. 现在,它运行出色。

http://jsfiddle.net/t4rLk1an/2/ http://jsfiddle.net/t4rLk1an/2/

alter the table like this: 像这样修改表:

<tr role="row" class="odd">
    <td class=" nowrap">Customer1_File</td>
    <td class=" nowrap">2014-10-22</td>
    <td>2014-10-22</td>
    <td>00:25:26</td>
    <td class="correct">Fail</td>
    <td>Client_name</td>
    <td>Job_Code_Details</td>
</tr>

and jQuery like: 和jQuery一样:

$(document).ready(function(){
    $('.correct').each(function() {          
    if ($(this).html() == 'Fail') {
    $(this).closest('tr').removeClass("chk");
    $(this).closest('tr').addClass("res");
  } 
});
}

);

I'm not sure about your class, as there is no css. 我不确定您的课程,因为没有CSS。 To try you can change it to 要尝试,您可以将其更改为

$(document).ready(function(){
    $('.correct').each(function() {          
    if ($(this).html() == 'Fail') {
    $(this).closest('tr').css("background","red");

  } 
});
}

);

Maybe this: 也许这样:

$("tr").each(function () {
    if($(this).find('td').eq(4) == "Fail") {
        $(this).removeClass('chk');
        $(this).addClass('res');
    }
});

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

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