Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

javascript - Why do I have not necessary pagination buttons with jQuery DataTables?

I have a problem on a DataTables using pagination with server-side request.

It looks like the number of pagination buttons is not related to my results. I always have buttons for pages 1, 2, 3, 4, 5, no matter how many results I have.

My DataTables definition:

$('#tableau_reponses').DataTable({
        "ajax": {
            url: '/my_api/get_reponses_ajax',
            type: 'post',
            data: formData
        },
        "paging": true,
        "searching": false,
        "bProcessing": true,
        "serverSide": true,
        "ordering": false,
        "info": false,
        "pagingType": "numbers",
        "pageLength": 20,
        "lengthMenu": [10, 20, 50, 100],
        'order': [[1, 'asc']],
        'columnDefs': [
            {
                'targets': 0,
                'orderable': false,
                'className': 'dt-body-center',
                'render': function (data, type, full, meta){
                    return '<input type="checkbox" name="id[]" value="' + $('<div/>').text(data).html() + '">';
                }
            },
            {
                'targets': 1,
                'render': function (data) {
                    return '<td class="response_state_folder"></td>';
                }
            }
        ],
    });

On my test environment, I know I have 29 results. My pageLength is set to 20. So by default, on page 1, I have 20 results. When I select page 2, I have 9 results. So far so good. But when I select page 3, 4 or 5 (and I shouldn't be able to) I have 0 results.

Why do I have "3, 4, 5" buttons when, with 29 results, I should have only 2 pages? Of course if I have 142 results I need 8 pages...

I think I'm missing something pretty obvious here...

Thanks for your help!

===== EDIT =====

Following @Ashu's answer, I've managed to get "recordsFiltered" and "recordsTotal" in my response (respectively "20" and "29" in my example). But now, I have only one button (for page 1) where I'm expecting two.

question from:https://stackoverflow.com/questions/65670924/why-do-i-have-not-necessary-pagination-buttons-with-jquery-datatables

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your server response must return how many rows are there in the filtered response from the database, and how many rows are there in total so as the database so it can set the pagination values right.

Example response :

{
    "draw": 1,
    "recordsTotal": 57, // these both are important and must be updated in every response 
    "recordsFiltered": 20, // 
    "data": [
        [
            "Angelica",
            "Ramos",
            "System Architect",
            "London",
            "9th Oct 09",
            "$2,875"
        ],
    ]
}

If you need help on server-side code, You can edit or ask a new question.

More in the documentation here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...