Changing of lengthMenu does not change the default to the first entry?

Changing of lengthMenu does not change the default to the first entry?

mihomesmihomes Posts: 165Questions: 23Answers: 0
$.extend( true, DataTable.defaults, {
    lengthMenu: [
        [25, 50, 100, 250],
        [25+' entries', 50+' entries', 100+' entries', 250+' entries'] // change per page values here
    ],
    renderer: "bootstrap"
});

I would like to change the default paging to 25... removing the '10' from my paging list. When doing this though it still defaults back to 10 for every table on page load/draw. Am I missing something or is this a bug?

'serverSide' is true and I am using 'ajax' to retrieve results. This is a table and code which has no issues so I can rule that out. My expectation here was the default paging would change from 10 to 25 because 10 no longer exists (the docs say it defaults to the first entry). While my paging dropdown has changed the initial draw has not. If I use the dropdown to choose 50 it redraws with 50... then I can change back to 25 and it will draw 25. It is just the initial draw on page load which is wrong and always 10.

Answers

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949
    edited November 2020

    Your setting of the Datatables defaults doesn't seem correct. The default settings is documented here. I modified your code to use the default syntax as documented and it works:
    http://live.datatables.net/yojetose/1/edit

    Also there is not an option renderer so renderer: "bootstrap" has no effect.

    Also you will might want to use the pageLength option to 25 to match the length menu default.

    Kevin

  • mihomesmihomes Posts: 165Questions: 23Answers: 0
    edited November 2020

    The format was taken directly from dataTables.bootstrap4.js included in the release. It has been like that for many versions now.

    Even using your version of this doesn't change the initial paging on any of my tables.

    If I look at the request sent to my php page from datatables it continues to send length "10" regardless of what I set.

  • mihomesmihomes Posts: 165Questions: 23Answers: 0
    edited November 2020

    Adding pageLength: 25, to my defaults solves the issue. With that said, according to the docs the default is the first entry in lengthMenu so it would appear that is not the case.

    "Note that the pageLength property will be automatically set to the first value given in this array, unless pageLength is also provided."

    https://datatables.net/reference/option/lengthMenu

    From what I am seeing this does not happen as I need to specify pageLength to make this work.

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949
    edited November 2020

    The format was taken directly from dataTables.bootstrap4.js included in the release

    That code may work within dataTables.bootstrap4.js. I've not seen it before in the docs. What options did you have that are working? Or are you directly updating the dataTables.bootstrap4.js file?

    Even using your version of this doesn't change the initial paging on any of my tables.

    Do you have a lengthMenu option configured in your Datatables init code? If so that will override the default settings.

    If I look at the request sent to my php page from datatables it continues to send length "10" regardless of what I set.

    As I mentioned the pageLength option will set the initial table page length.

    If you still need help we will need to see what you are doing. At minimum post your Datatable JS code including the default settings and the init code. Better post a link to your page or a test case, you can update my example, showing the issue.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • mihomesmihomes Posts: 165Questions: 23Answers: 0

    I use dataTables.bootstrap4.js and modify it slightly for my own needs. It is part of datatables itself - not a third-party or something I made on my own. https://github.com/DataTables/DataTables/blob/master/media/js/dataTables.bootstrap4.js

    $.extend( true, DataTable.defaults, {
        lengthMenu: [
            [25, 50, 100, 250],
            [25+' entries', 50+' entries', 100+' entries', 250+' entries'] // change per page values here
        ],
        pageLength: 25, //required to set default from lengthMenu above
        renderer: "bootstrap"
    });
    

    works with the addition of pageLength: 25,. Without it the default is always 10 even though it is not part of lengthMenu.

    No, I am not using pageLength in my init code for any of the tables in question. As you mentioned that would override anything set prior.

    I should not need to set pageLength: 25 from what the docs say. Just editing lengthMenu should set the default pageLength to the first entry provided for this setting.

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949

    I use dataTables.bootstrap4.js and modify it slightly for my own needs

    Making changes to these files is generally not recommended especially if you can do the same with documented configuration options. If you upgrade the files you will need to move your changes. Plus since they aren't documented the behavior may change with different versions. The recommended way to implement default settings is documented here. But you are free to do what you want with the files :smile:

    Sorry I misunderstood your first post. Thought the problem was with the length menu not showing correctly. You are correct that the docs say the initial page length should be the first (default) in the length menu list. My example shows that this is not working as documented when using default settings:
    http://live.datatables.net/yojetose/1/edit

    However it does work when applied directly to the Datatable:
    http://live.datatables.net/delapoku/1/edit

    @allan or @colin will need to take a look at the default settings.

    Kevin

This discussion has been closed.