javascript 在数据表中的表重绘上调用 fnRowCallback

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22021563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:13:46  来源:igfitidea点击:

Call fnRowCallback on table redraw in datatable

javascriptjquerydatatablesjquery-datatables

提问by NEO

I am doing some post processing in datatable in fnRowCallback. But they are not being called when the table is redrawn. (i.e, when some event like changing the number of displayed rows are called from UI, the table is redrawn)

我正在 fnRowCallback 中的数据表中进行一些后期处理。但是在重新绘制表格时不会调用它们。(即,当从 UI 调用诸如更改显示行数之类的事件时,将重新绘制表格)

      $(document).ready(function () {
            var oTable = $('#data').dataTable({
                "bJQueryUI": true,
                "bProcessing": true,
                "bServerSide": true,
                "bSort": false,
                "sAjaxSource": "query.php",
                "sPaginationType": "full_numbers",
                "aoColumns": [
                    null,
                    null,
                    null,
                ],
                "fnRowCallback": function (nRow, aData, iDisplayIndex) {
                    $(nRow).attr("id", aData[4]);
                    return nRow;
                },
               "fnDrawCallback": function( oSettings ) {
                // How do I call fnRowCallback here? 
               // losing post processing because it is not being called after a redraw
                }
    });

回答by mainguy

I think your attempt to look up the actual row via jquery in $(nRow)does not work. nRowcontains the whole row. You should just is as namespace for the jquery selector (second parameter) to restrict it to this particular row.

我认为您尝试通过 jquery in 查找实际行$(nRow)不起作用。nRow包含整行。您应该只是作为 jquery 选择器(第二个参数)的命名空间以将其限制为该特定行。

Like so:

像这样:

$("selector",nRow).jqueryaction()

$("selector",nRow).jqueryaction()

This works for me:

这对我有用:

Html:

网址:

  <tr>
    <td>a</td>
    <td class="boldmetight">b</td>
  </tr>
  <tr>
    <td class="boldmetight">c</td>
    <td>d</td>
  </tr>.. etc

And the table definition with a rowcallback that bolds every cell with a specific class (just for example):

以及带有 rowcallback 的表定义,它用特定的类加粗每个单元格(仅作为示例):

  var otable = $("#datatable").dataTable({
    "fnRowCallback": function(nRow, aData, iDisplayIndex) {
      $('.boldmetight', nRow).html('<b>' +  $('.boldmetight', nRow).text() + '</b>');
    }
  });

Look at this working Plunker

看看这个工作的Plunker