2

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

Example

<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 correctly. However, I don't know what it's sending me or how I change the JS to tell it to give me what I need. Once I have an on Object (or something), send it back and apply it when the user goes back to the page.

1 Answer 1

1

By the description, looks like you have everything working, you just need a way to debug data you are receive, so you can use Log to do that:

Route::get('/api/load_state', function() {
    \Log::info(app('request')->all());
});

And tail the log

tail -f storage/logs/laravel.log

So if you hit

http://domain.dev/api/load_state?what=1    

You should see

enter image description here

To respond a json object back to the client, you can do

Route::get('/api/load_state', function(Request $request) {
    return response()->json(
        User::find($request->get('user_id'))
    );
});
Sign up to request clarification or add additional context in comments.

5 Comments

Ah, yes! Thank you so much! So essentially I can do the same thing with the router pointed to this method... public function saveState(Request $request) { Log::info($request->all()); } And save the data but once I have the stored data available how do I use "load_state" and it's Laravel method to pull data into the datatable and sync up everything?
This works great, THANK YOU! I json_encode()/json_decoded the object so I could get it into the database. The one thing the object doesn't seem to include, however, are my custom filters which are really important for loading/saving. Any ideas on that? I just assumed they'd be part of that object.
Please show more of your code so we can understand what you are doing and how you are doing it now.
This is a pure Datatables JS/HTML question and perhaps this belongs in an entirely new post. But if you have a field outside of your table like $('#dateStart'). How do you add this custom filter to the datatable in a way so that it's part of the "search" in the object that can be saved/loaded? My JS above has not changed so there's no other code to show you.
I added the new question if you know the answer, Cheers. stackoverflow.com/questions/41040855/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.