stateLoadCallback doesn't apply a saved stated from remote

stateLoadCallback doesn't apply a saved stated from remote

ricardooliveiraricardooliveira Posts: 3Questions: 1Answers: 0

Hello there I'm using statesavecallback and stateloadcallback to store an user specific preferences with visibility and ordering in datatables

'stateLoadCallback': function (settings) {

        var data;
        // Send an Ajax request to the server to get the data. Note that
        // this is a synchronous request since the data is expected back from the
        // function
        $.ajax ({
            'url': contextPath+'/app/loadTable?page='+thisPage,
            'async': false,
            'dataType': 'json',
            'success': function (json) {
                data = json;
                loadTable = true;
            }
        });

        return data;
    },
'stateSaveCallback': function (settings, data) {
    // Send an Ajax request to the server with the state object
    data['pagina'] = thisPage;
    if (!requestSent && loadTable) {
        requestSent = true;
        $.ajax ({
            'url': contextPath+'/app/saveTable.json',
            'data': data,
            'dataType': 'json',
            'type': 'POST',
            'success': function () {},
            'complete': function () { requestSent=false;}
        });
    }
}

However even though the request in loadstate is doing fine, it doesn't apply to the table, am I missing something?

Answers

  • ricardooliveiraricardooliveira Posts: 3Questions: 1Answers: 0

    Any help?

  • ricardooliveiraricardooliveira Posts: 3Questions: 1Answers: 0
    var table = $('.table').DataTable({
        stateSave: true,
        dom: 'Bfrtip',
        'order': [[ 1, 'asc' ]],
        'dom': 'CT<'clear'>lfrtip',
        'tableTools': {
            'aButtons': [
                {
                    'sExtends':    'collection',
                    'sButtonText': '',
                    'aButtons':    [
                        {
                            'sExtends': 'copy',
                            'oSelectorOpts': { filter: 'applied', order: 'current', page: 'all' },
                        },
                        {
                            'sExtends': 'xls',
                            'oSelectorOpts': { filter: 'applied', order: 'current' },
                            'mColumns': 'visible'
                        },
                        {
                            'sExtends': 'pdf',
                            'oSelectorOpts': { filter: 'applied', order: 'current' },
                            'mColumns': 'visible'
                        },
                        {
                            'sExtends': 'csv',
                            'oSelectorOpts': { filter: 'applied', order: 'current' },
                            'mColumns': 'visible'
                        },
                        {
                            'sExtends': 'print',
                            'oSelectorOpts': { filter: 'applied', order: 'current' },
                        }
                    ]
                }
            ],
            'sSwfPath': contextPath + '/assets/plugins/datatables/extensions/TableTools/swf/copy_csv_xls_pdf.swf'
        },
        'responsive': false,
        'orderCellsTop': true,
        'scrollX': true,
        'scrollCollapse': true,
        'iDisplayLength': -1,
        'paging': false,
        'autoWidth': true,
        'columnDefs': [
            {
                'targets': [0],
                'visible': false,
                'searchable': false
            }
        ],
        'colVis': {
            'exclude': [ 0 ],
            'order': 'alfa'
        }
        ,'stateSaveCallback': function (settings, data) {
            // Send an Ajax request to the server with the state object
            // console.log(JSON.stringify(data));
            if (!requestSent && loadTable) {
                requestSent = true;
                $.ajax ({
                    contentType: 'application/json;charset=UTF-8',
                    url: contextPath + '/app/saveTable?page='+thisPage,
                    data: JSON.stringify(data),
                    dataType: 'json',
                    method: 'POST',
                    'success': function () {},
                    'complete': function () {
                        requestSent=false;
                        delete data.pagina;
                    }
                });
            }
        },
        'stateLoadCallback': function (settings) {
            var o;
            // Send an Ajax request to the server to get the data. Note that
            // this is a synchronous request since the data is expected back from the
            // function
            $.ajax ({
                'url': 'app/loadTable.json?page='+thisPage,
                'async': false,
                'dataType': 'json',
                'success': function (json) {
                    o = json;
                    loadTable = true;
                    console.log(json);
                    settings.oLoadedState = o;
                    settings.oSavedState = o;
                    table.state.apply();
                    table.draw();
                    console.log(settings);
                }
            });
    
            return o;
        }
    });
    

    I tried using settings to set the loadestate and savedstate, but to no avail, here is the whole datatable initialization code

  • kornixkornix Posts: 2Questions: 1Answers: 0

    Hi I'm in the same situation.
    alert(JSON.stringify(o));
    show me clean valid JSON - However it isn't applied to the table though it is implemented in the constructor - I searched around but found no answer - wondering why this feature is available if it won't work.

  • danbenedekdanbenedek Posts: 5Questions: 3Answers: 0

    I have the same problem. Does this feature work?

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    Could anyone with this issue give me a link to a page showing the issue so I can debug it please?

    There was a bug in 1.10.13 which would have caused this, but it is resolved in 1.10.15.

    Allan

This discussion has been closed.