Page not statesaved

Page not statesaved

redaxeredaxe Posts: 4Questions: 1Answers: 0

I am new to Datatables and trying to get it set up. I am liking it so far.

One problem I have is that my table page number isn't being preserved when I move away from the page and back again.

Other settings, such as the search and pageLength fields, are being preserved.

It has me baffled. I am about to back out my config and go back to basics to try and work it out.

Any suggestions on what it could be?

My config:

    $('#mytable').DataTable({
        columns: [
            {
                data: null,
                orderable: false,
                targets: 0,
                render: DataTable.render.select(),
            },
            { data: 0 },
            { data: 1 },
            { data: 2 },
            { data: 3 },
            { data: 4 },
            { data: 5 }
        ],
        Xselect: {
            style: 'multi+shift',
            selector: 'td:first-child'
        },
        ajax: {
            url: '/ajax/myajax',
            dataSrc: ''
        },
        layout: {
            topStart: 'paging',
            topEnd: 'search',
            bottomStart: 'info',
            bottomEnd: {
                buttons: ['pageLength', 'copy', 'csv', 'excel', 'pdf', 'print']
            },            
        },
        columnDefs: [
            {
                targets: 6,
                render: function (data, type, row, meta) {
                    if (type === 'display') {
                        data = '<a href="/edit/' + encodeURIComponent(data) + '">Edit</a>';
                    }
                    return data;
                }
            }],
        stateSave: true,
        initComplete: function () {
            this.api()
                .columns([1, 3, 5])
                .every(function (d) {
                    var column = this;
                    var theadname = $("#mytable th").eq([d]).text();

                    var select = $('<select class="form-control m-2"><option value="">' + theadname + ' all</option></select>')
                        .appendTo($('#filter'))
                        .on('change', function () {
                            column
                                .search($(this).val(), { exact: true })
                                .draw();
                        });

                    column
                        .data()
                        .unique()
                        .sort()
                        .each(function (d, j) {
                            
                            if (column.search() === d) {
                                select.append('<option value="' + d + '" selected="selected">' + d + '</option>')
                            } else {
                                select.append('<option value="' + d + '">' + d + '</option>')
                            }
                        });
                });
        }
    });

This question has accepted answers - jump to:

Answers

  • redaxeredaxe Posts: 4Questions: 1Answers: 0
    edited July 27

    Just an update - I removed all config options but left the ajax data source and the stateSave option and it still didn't preserve the pagination. Does that mean the feature isn't support for Ajax loads or am I just doing the page load wrong? (or am I not understanding how stateSave works).

  • redaxeredaxe Posts: 4Questions: 1Answers: 0

    https://datatables.net/extensions/select/examples/initialisation/stateSave

    This ajax example doesn't stateSave the pagination either - bug or you-must-do-it-yourself feature perhaps?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Apologies, that appears to be a bug. Really suprised that wasn't caught by the unit tests! I'll look into it at the start of the week.

    Allan

  • redaxeredaxe Posts: 4Questions: 1Answers: 0
    edited July 27

    Excellent news! I will press on with coding other things ...

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Fix committed here. It will be in the nightly builds soon and then the next release (which will probably be towards the end of next week).

    Added a test to make sure it doesn't happen again as well. Thanks for letting me know about that!

    Allan

Sign In or Register to comment.