Server side processing with Ajax pagination question

Server side processing with Ajax pagination question

dannywolfdannywolf Posts: 2Questions: 1Answers: 0

dynamic search data for datatable with ajax source data:

HTML form:

$('.form-filter form').on('submit', function(e) {
    e.preventDefault();
    const start = $('.form-filter [name=start]').val();
    const end = $('.form-filter [name=end]').val();
    const type = $('.form-filter [name=type]').val();
    const status = $('.form-filter [name=status]').val();

    if (this.checkValidity() !== false) {
        action_handle('fire_search');
        var datetime = timezone(start, end);
        paymentTable(datetime[0], datetime[1], type, status);
    }
});

datatable function:

function paymentTable(from, to, type, status) {
    const paymentTable = $('#table').DataTable({
        ajax: {
            type : "POST",
            url: "/api/somethinng/history",
            data: {"start": from, "end": to, "type": type, "status": status},
            dataSrc: function(json) {
                if(json == "no data") {
                    return [];
                } else {
                    return json.data;
                }

            }
        },
        responsive: {
            details: {
                renderer: $.fn.dataTable.Responsive.renderer.tableAll({
                    tableClass: 'ui display nowrap table-sm table-bordered'
                })
            }
        },
        processing: true,
        serverSide: true,
        deferRender: true,
        destroy: true,
        order: [[0,"desc"]],
        }
    });

    paymentTable.draw();
}

Using PHP to generate the data from DB:

public function api_history() {
    $raw = $this->balance_model->data_source([
        'userid' => $_SESSION['token'], 
        'type' => (int)$this->input->post('type'), 
        'status' => (int)$this->input->post('status'), 
        'fromdate' => $from,
        'todate'=>$to,
        'self' => true,
        'pageindex' => 1,
        'rowperpage' => 1000
    ]);

    if( $raw['code'] == 1 && $raw['data'] != [] ):
        asort($raw['data']);
        $data = [];
        foreach( $raw['data'] as $ph ):
            $row = [];
            $row[] = $ph['date'];
            $row[] = $ph['id'];
            $row[] = $ph['amount'];
            $data[] = $row;
        endforeach;
        echo json_encode([
            'data' => $data
            'draw' => (int)$_POST['draw'],
            'recordsTotal' => $raw['totalRecord'],
            'recordsFiltered' => $raw['totalRecord']
        ]);
    else:
        echo json_encode(['no data']);
    endif;
}

The question is:
how can I pass the pagination number to the datable jscript and letting php to define the pageindex dynamically?
I tried to pass the value into the function, it will totally reload the ajax table for me and stay back at page number 1 instead of page number 2.

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    I'm not following, sorry, but for server side processing the protocol is discussed here, and all interaction must follow that protocol.

    Cheers,

    Colin

  • dannywolfdannywolf Posts: 2Questions: 1Answers: 0

    Hi, thanks for the replied ya.
    At the "Using PHP to generate the data from DB", Im requesting the data via api using pageindex and rowperpage.

    Im following datatable server side processing protocol as well, but it not as work.

    How can I pass the pagination number to the datatable function and request the following pageindex from the api?

  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin

    how can I pass the pagination number to the datable jscript and letting php to define the pageindex dynamically?

    That's not how DataTables' server-side processing works. It will tell the server what page of data it is requesting (using the protocol Colin linked to) and the server should respond with suitable data.

    It sounds like you are trying to do your own custom pagination rather than using DataTables'.

    but it not as work.

    In what way? It is working here.

    Allan

This discussion has been closed.