Server side pagination

Server side pagination

calacala Posts: 52Questions: 15Answers: 0
edited November 2015 in Free community support

Hi!

I've created my function in order to have the server able to filter my table.

the ajax response is something like:

{
    "recordsTotal":17,
    "recordsFiltered":17,
    "data":[
        {
            "DT_RowId":"row_17",
            "adati":{
                "protocollo":"17",
                "prenotato_da":"",
                "anno_eccl":"2006",
                "data_imm":"2006-02-27",
                "sotto_cod":"pTP",
                "data_doc":"2006-02-27",
                "rif":"0",
                "num_ord":"1234",
                "tipo_doc":"lettera",
                "mittente":"io",
                "destinatario":"me",
                "oggetto":" DICHIARAZIONE",
                "da_leggere":"NO",
                "per_mod":"NO"
                },
            "afiles":""
        },
        {
            "DT_RowId":"row_16",
            "adati":{
                "protocollo":"16",
                "prenotato_da":"",
                "anno_eccl":"2006",
                "data_imm":"2006-02-27",
                "sotto_cod":"pTP",
                "data_doc":"2006-02-27",
                "rif":"0",
                "num_ord":"1234",
                "tipo_doc":"lettera",
                "mittente":"tu",
                "destinatario":"WESTMINSTER COLLEGE CAMBRIDGE",
                "oggetto":"LETTERA DI PRESENTAZ",
                "da_leggere":"NO",
                "per_mod":"NO"
            },
            "afiles":[{"id":"15"},{"id":"17"}]
        },
    "options":[],
    "files":{
        "afiles":{
            "16":{
                "id":"16",
                "filename":"20060227082305-0.pdf",
                "old_filename":null,
                "web_path":"\/wp-content\/uploads\/2006\/02\/20060227082305-0.pdf",
                "hash_name":null
            },
        }
    }
}

so I think that the structure is correct.

Now, I have 2 paths (if the number of rows is exceeding the value of pagination, for example 10):

1) get the number of row equal to the value of pagination

2) get all the rows that satisfy the filter

In the first case the table is correctly showed, but the paginator doesn't show that there are more pages to show.

In the second case I have a strange behaviour: all the rows are showed in the table (more than 10) but the paginator correctly shows that there are x more pages and also the infos are correct ("Showing 1 to 10 of 16 elements"). In this case also start, end, page, pages and all the other page.info() are correct.

I cannot understand how to inform the table that there are more pages in the first case, or limit the row in the second.

Thanks for the help.

Answers

  • ibdfariaibdfaria Posts: 15Questions: 4Answers: 0
    edited November 2015

    You seem to be using wordpress, which is what I am using. It took me a while to figure out what should be sent with the response too. My response settings:

    $data = array(
    "draw"=>intval($_GET['draw']),
    "recordsTotal"=>intval($total_count),
    "recordsFiltered"=>intval($records_filtered),
    "data"=>$add_posts);

    Where:

    $_GET['draw'] is passed from the ajax call

    //All posts available for queried post_type

    $total_count = wp_count_posts($post_type)->publish;

    //Posts found for the specific query

    $records_filtered = $query->found_posts;

    $add_posts = array with results

  • calacala Posts: 52Questions: 15Answers: 0
    edited November 2015

    thanks @ibdfaria for your answer!

    Yes I'm using WP, but I don't use the datatable to show a list of posts. I use a completely separated database table with different data.

    However I will try to use your idea modifying it for my purpose!

  • calacala Posts: 52Questions: 15Answers: 0

    as a workaround I've added a ajax.dataSrc function:

    dataSrc: function ( json ) {
        var schede = [];
        if (vista=='filter_date'){
            current_page=table.page.info().page;
            var start_value= current_page*pagination_value;
            for (i=start_value;((i<start_value+pagination_value)&&(i!=parseInt(json.recordsFiltered)));i++){
                schede.push(json.data[i]);
            }
            return schede;
        }
        return json.data;
    }
    

    so the json is modified before the table is draw.

    Now I want to resolve another little bug:
    when the table isn't filtered, the individual column searching and the global search are correctly working, when I filter with my serverside function, they have no effect on the table.
    Someone stumbled into this kind of behavior?

    Thank you!

This discussion has been closed.