mercredi 7 décembre 2016

Load/Save sorting & filter options on Datatables using Laravel

I need to be able to save the state of pagination, sorting & search inputs... ultimately utilizing Laravel and an API instead of a database. I'd rather not use yajra/laravel-datatables as I've already created the complex datatable I need.

Here's an example of what I have:

<script type="text/javascript">
    $(document).ready(function () {
        var table = $('#stackoverflow-datatable').DataTable({
            "stateSave": true,
            "stateSaveCallback": function (settings, data) {
                $.ajax({
                    "url": "/api/save_state",
                    "data": data,
                    "dataType": "json",
                    "success": function (response) {}
                });
            },
            "stateLoadCallback": function (settings) {
                var o;
                $.ajax({
                    "url": "/api/load_state",
                    "async": false,
                    "dataType": "json",
                    "success": function (json) { o = json; }
                });
                return o;
            }
        });
    });
</script>

The urls: "/api/load_state" point to a Laravel route and the routes point to specific methods on the Controller. I see that it's calling the methods perfectly however I don't know what it's sending me or how I change the JS to tell it to send what I need. Then once I have an on Object (or something) how to send it back and apply iy when the user goes back to the page.

Thanks in advance for your help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire