javascript - Call fnRowCallback on table redraw in datatable - Stack Overflow

I am doing some post processing in datatable in fnRowCallback. But they are not being called when the t

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)

      $(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
                }
    });

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)

      $(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
                }
    });
Share Improve this question asked Feb 25, 2014 at 17:21 NEONEO 2,0118 gold badges36 silver badges55 bronze badges 6
  • Where is your code that redraws the table? – Justin C Commented Feb 25, 2014 at 17:26
  • I don't manually redraw, but the table is redrawn automatically when pagination is used if I am correct. I just want to hook the code I use in rowcallback whenever the table is redrawn. – NEO Commented Feb 25, 2014 at 17:27
  • fnRowCallback should be called on each row every redraw, maybe try manually redrawing the table and seeing if fnRowCallback is called. – Justin C Commented Feb 25, 2014 at 17:32
  • But how do I manually call redraw if datatable itself calls redraw during pagination or display number of rows change? – NEO Commented Feb 25, 2014 at 17:35
  • I meant call oTable.fnDraw() somewhere outside of the callbacks, just to make sure that fnRowCallback gets fired on all redraws, not just the initial draw. – Justin C Commented Feb 25, 2014 at 17:37
 |  Show 1 more ment

1 Answer 1

Reset to default 2

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

Like so:

$("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):

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

Look at this working Plunker

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744753890a4591763.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信