简体   繁体   中英

Datatable send current page number

I've been try to send current page number to server by overwrite a variable called pgno

here is my code:

function fill_datatable(status='',pgno='') 
{
    var pgno = 0;
    table = $('.tb_scoin_available').DataTable({
        "processing": true,
        "serverSide": true,
        "ordering"  : false,
        "infoCallback": function( settings, start, end, max, total, pre ) {
            var api = this.api();
            var pageInfo = api.page.info();
            pgno = pageInfo.page+1;
            return pgno;
        },
        "ajax":{
            "url": base_url+'/adminPath/management_scoin/ajaxGetScoinAvailable',
            "type": "POST",
            "data":{ _token: csrf_token, status : status,pgno : pgno}
        },
        "columnDefs": [ { orderable: false} ],
        "columns": [
            { "data": "no" },
            { "data": "created_at" },
            { "data": "serial_scoin" },
            { "data": "unit_scoin" },
            { "data": "unit_scoin_desc" },
            { "data": "unit_scoin_sc" },
            { "data": "unit_scoin_idr" },
        ],

    }); 
}

when I try to alert on infoCallback: alert(pgno) the variable already overwrited, but when I try to dump the request on backend the pgno POST give me null result like this:

截屏

Anyone can help me out?

You can get the table page with the page() function, no need for the whole "page.info". It's better explained in Datatable's API: https://datatables.net/reference/api/page()

The method you're trying to access is only to get the information, not to set it. That is probably why it isn't working. Check their docs for better understanding of their API: https://datatables.net/reference/api/page.info()

EDIT:

You can get the current page through a simple calculation. Since you're using serverSide processing, you already have start and length . You just need to do start/length + 1 and you will get the current page number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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