Show a row on the last page of a paginated jQuery datatable

折月煮酒 提交于 2019-12-04 04:48:09

问题


I want to make a hidden row visible when the last page of the datatable is clicked.How can I do this? I could not find the method to get the current page number and the last page number of Jquery Datatable pagination.

This is what I want to do,but I dont have any idea abt how to get current page and last page.

$( "#paginate_button" ).click(function() {     
if(.......){  //if clicked page number= last page
$( "#hidden_row" ).show();
else
$( "#hidden_row" ).hide();
});

回答1:


First, implement the wellknown [fnPagingInfo][1] "plugin". Eg add the following to your code :

$.fn.dataTableExt.oApi.fnPagingInfo = function ( oSettings ) {
  return {
    "iStart":         oSettings._iDisplayStart,
    "iEnd":           oSettings.fnDisplayEnd(),
    "iLength":        oSettings._iDisplayLength,
    "iTotal":         oSettings.fnRecordsTotal(),
    "iFilteredTotal": oSettings.fnRecordsDisplay(),
    "iPage":          Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength ),
    "iTotalPages":    Math.ceil( oSettings.fnRecordsDisplay() / oSettings._iDisplayLength )
  };
}

Then, apply the if condition in fnDrawCallback :

"fnDrawCallback": function () {
    var currentPage= this.fnPagingInfo().iPage;     
    if ((+currentPage + +1) == this.fnPagingInfo().iTotalPages) {
    $("#target_id").show();
    }else
    $("#target_id").hide();

    }
});



回答2:


I had a similar issue and that worked for me too. But, I had to put this way (the script must be loaded before the datatable):

$.extend( true, $.fn.dataTable.defaults, {

  "fnDrawCallback": function () {
      var currentPage= this.fnPagingInfo().iPage;     
      if ((+currentPage + +1) == this.fnPagingInfo().iTotalPages) {
        $("#target_id").show();
      }else{
        $("#target_id").hide();
      }
    }
  });
});

*I would put it as a comment to the other answer but I don't have enought reputation.



来源:https://stackoverflow.com/questions/21045248/show-a-row-on-the-last-page-of-a-paginated-jquery-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!