Table length settings

Table length settings

FedericoVFedericoV Posts: 35Questions: 9Answers: 0

In my Datatables (AJAX data ) I need to get rid of change length menu and set the table to 15 rows.
Even if reading the documentation I cannot set the table to 15 row...it always shows 50 rows.
Below the code I'm trying...I've tested pageLength and length option...no success so far.
Thanks for any hint

     table.DataTable({
        dom: 'lrtip',
        responsive: true,
        autoWidth: false,
        searchDelay: 500,
        processing: true,
        serverSide: true,
        stateSave: true,
        lengthChange: false,
        //lengthMenu: [[15, 25, 50, -1], [15, 25, 50, "All"]],
        //pageLength: 15,
        length: 15,
        searchHighlight: true,
        ajax: {
            url: "<?php echo site_url('rmidev/domestic_list')?>",
            type: "POST",
            "data": function ( d ) {
                d.csrf_test_name = token_hash;
            },
            "dataSrc": function ( json ) {
                token_hash = json['csrf_test_name'];
                for(var i=0; i< json.data.length; i++){
                    json.data[i].mm = '<a href="' + controller_url + '/data/' + json.data[i].id + '">' + json.data[i].mm + '</a>';
                }
                //return return_data;
                return json.data;
            }
        },
        order: [[2, 'asc']],
        deferRender: true,
        columns: [
            {data: 'mm'},
            {data: 'ditta'},
            {data: 'tipo'},
            {data: 'motori'},
            {data: 'nc'},
        ],
        columnDefs: [
            {
                targets: [ 3, 4 ],
                orderable: false,
                searchable: false,
            },
        ],
        "language": {"url": "<?php echo site_url('assets/public/plugins/datatables/lang/dataTables.') . $actual_lang . '.lang'; ?>"},
        "pagingType": "numbers",
    });

This question has accepted answers - jump to:

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Perhaps your server-side script is imposing a 50-record condition.

  • FedericoVFedericoV Posts: 35Questions: 9Answers: 0

    Negative, the server-side script get the limit parameter from the AJAX call...so I should be able to set the limit from the code above.
    The weird thing is: if I enable the lengthMenu, select 50 rows...then disable the lengthMenu...the script keep receiving 50 rows...no way to set a d ifferent value, maybe I'm using a wrong config setting since neither pageLength or length works.

  • kthorngrenkthorngren Posts: 21,311Questions: 26Answers: 4,948
    edited April 2020 Answer ✓

    The option to set the initial page length is pageLength. You have it commented out above and the length: 15 you have is ignored because its not a valid option.

    If the number of rows displayed is 50 then the server script is returning 50 rows. There would be no way for the client side to display 50 rows if the server script returns only 15. Take a look at the browser's Network inspector to see the parameters sent and the number of rows returned in the XHR request. Are you using a Datatables provided server script?

    The server script is expected to follow this protocol:
    https://datatables.net/manual/server-side

    Kevin

  • FedericoVFedericoV Posts: 35Questions: 9Answers: 0

    Thanks for feedback Kevin.
    Network inspector confirm the correct value returned by server script ( at least for what I know and see )

    Believe me...the server script doesn't set the limit of returned rows...I'm totally sure about that.
    I have uncommented the pageLength option...it seems working fine but the I have commented the lengthMenu option when it was set to 15 page.

    My guess is that, if I have a cookie ( Datatable use it to keep option settings...correct?) settled for 50 or other values...the pageLength option doesn't work... I could be wrong...of course
    Thanks in any case for your help.
    The latest hypotheses are just to understand how Datatables works

  • kthorngrenkthorngren Posts: 21,311Questions: 26Answers: 4,948
    Answer ✓

    Here is a quick test case with StateSave:
    http://live.datatables.net/tumupawo/1/edit

    Your hypothesis looks to be correct. Change the page length and reload the page and the table shows the changed page length.

    You can use stateSaveParams to affect what config parameters are saved.

    Kevin

This discussion has been closed.