serverSide ajax function 'data' param sending '[object Object]' in the request

serverSide ajax function 'data' param sending '[object Object]' in the request

DennyJoDennyJo Posts: 2Questions: 1Answers: 0

Description of problem: (We are changing from using clientSide to serverSide processing)
Using serverSide true, I can retrieve the DataSource using Ajax. The following requirements are what the table needs to include, but does not currently work:
- Searching/Filtering
- Sorting by column
- Displaying Total row count, with current page count (e.g '0-10 of 20 results')

For this, the Ajax 'function' (https://datatables.net/reference/option/ajax) is required to pass the Datatables settings into my ajax resource (In this case, Java REST resource).

DataTable({
            serverSide: true,
            select: 'single',
            lengthMenu: [[10, 50, 100, 500], [10, 50, 100, 500]],
            ajax: function (data, callback, settings){
                callback(
                    {"data":[]}
                );
            },
// render/language parts ommitted, just showing the ajax: param
});

'data' in the function() contains the current Datatables info as stated in the docs (such as length, search, etc) but not all values are being sent to the request as expected.

I can filter page size and select a specific page using serverSide with 'length' and 'start', as this gets passed through correctly, but other options get passed in as '[object Object]' as below:

order=[object Object]&start=10&length=10&search=[object Object]

Is there a way that serverSide handles the data differently, or perhaps even a way to retrieve the current config (search/order) so I can stringify the object array and append it onto the ajax request? (I have tried https://datatables.net/reference/api/order() and stringified the value, appending on to the request, but the updated order is not passed through for future renders/changing page)

The Previous/Next buttons are also not working when switching to serverSide, is there a specific doc page for handling these buttons (as the numbered pages send the correct data to the request)

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 54,881Questions: 1Answers: 8,601 Site admin

    I don't see how you are actually passing the data parameter to the server there, but you probably need to do JSON.stringify(data). If you can show me how are are using the data object, I will hopefully be able to help a little more.

    Allan

  • DennyJoDennyJo Posts: 2Questions: 1Answers: 0

    Hi Allan,
    Figured it out over the weekend.
    Yes, 'data' (or any specific config i required' needed to be stringified - that solved my [object Object] problem.
    The dataSource was returning 'totalRecords' not 'recordsTotal'. Changing this fixed my paging problem.

    I'm still unable to sort columns. To clarify, if serverSide is enabled, we have to manually order our data using the provided order (column and direction)?

  • allanallan Posts: 54,881Questions: 1Answers: 8,601 Site admin
    edited August 9 Answer ✓

    we have to manually order our data using the provided order (column and direction)?

    Correct - the server needs to do all the sorting, filtering and paging. That is basically the whole point of server-side processing.

    Allan

Sign In or Register to comment.