How to send current page number in Ajax request

前端 未结 2 882
遇见更好的自我
遇见更好的自我 2021-01-11 17:27

I am using jQuery DataTable to display huge amount of data in a table. I am getting data page wise on Ajax request like this:

var pageNo = 1;
$(\'#propertyTa         


        
相关标签:
2条回答
  • 2021-01-11 17:42

    "Untested" Edit: Added "retrieve": true in options to retrieve the table instance (v1.10), checked if table exists. Changed fnSettings to setting() for datatable v1.10.

    You can try to read from the datatable settings and then from settings read _iDisplayStart and _iDisplayLength , then calculate current page index as follows:

    var currentPageIndex  = 1;
    //Is datatable already initialized? 
    if ( $.fn.DataTable.isDataTable( '#propertyTable' ) ) {
    //Get the current settings and calculate current page index
        var oSettings = $('#propertyTable').dataTable({"retrieve": true}).settings();
        currentPageIndex = Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength) + 1;
    
    
    }
    

    then in ajax call pass currentPageIndex:

     "ajax": "${contextPath}/admin/getNextPageData/"+currentPageIndex +"/"+10,
    
    0 讨论(0)
  • 2021-01-11 17:47

    DataTables already sends parameters start and length in the request that you can use to calculate page number, see Server-side processing.

    If you still need to have the URL structure with the page number, you can use the code below:

    "ajax": {
       "data": function(){
          var info = $('#propertyTable').DataTable().page.info();
    
          $('#propertyTable').DataTable().ajax.url(
              "${contextPath}/admin/getNextPageData/"+(info.page + 1)+"/"+10
          );
       }
    },
    
    0 讨论(0)
提交回复
热议问题