Data Format for Server Side

Data Format for Server Side

Al GrantAl Grant Posts: 10Questions: 4Answers: 0

Hello,

I want to send paginated data to DataTables from my Spring based backend. So far I have managed to get the response to look like:

{
   "draw":1,
   "recordsTotal":57,
   "recordsFiltered":57,
   "data":[
      {
         "salesno":"P12345",
         "start_date":"01/09/2017",
         "address":"2 Some Road, Los Angeles",
         "Entry":false,
         "names":"PITT & ANISTON"
      },

Question: Do I need to change the inner { to [ - like below:

{
   "draw":1,
   "recordsTotal":57,
   "recordsFiltered":57,
   "data":[
      [
         "salesno":"P12345",
         "start_date":"01/09/2017",
         "address":"2 Some Road, Los Angeles",
         "Entry":false,
         "names":"PITT & ANISTON"
      ],

And if so what would the best way to do this be? Client or Server side? And if Client side does DataTables have a way to help with this situation?

Thanks

Al

This question has accepted answers - jump to:

Answers

  • Al GrantAl Grant Posts: 10Questions: 4Answers: 0

    And my client side code:

    $(document).ready(function () {
    
        $('#salesDashboardTable').DataTable({
            processing: 'true',
            serverSide: 'true',
            pageLength: 2,
            ajax: { url : '/getsales/paginated',
                data: "content"
            },
    
            "columnDefs": [
                { "data": "salesno", "render": function (data, type, row) { return '<a href=/sale/' + data + '>' + data + '</a>'; }, "targets": 0, },
                { "data" : "start_date", "targets" : 1 },
                { "data": "names", "targets" : 2 },
                { "data": "address", "targets" : 3 },
                { "data": "Entry", "render": function (data, type, row) {
                        return data === true ? '<div align = "center"><span class="glyphicon glyphicon-ok"></span></div>' :
                            '<div align = "center"><span class="glyphicon glyphicon-remove"></span></div>' ; }, "targets": 4 }
            ]
        });
    });
    
  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28
    Answer ✓

    I'm doing much the same as you with a Spring back end. My response data is as in your first example and it's working fine.

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Answer ✓

    Do I need to change the inner { to [ - like below:

    No - what you've shown isn't actually valid JSON. You could use an array, or you can continue to use an object - its up to you. This section of the manual has more information about both.

    Allan

  • Al GrantAl Grant Posts: 10Questions: 4Answers: 0

    Thank rduncecb - thats a good sanity check that I am on the right track.

    I had another look at it this morning when I wasn't so tired and its going now.

  • Al GrantAl Grant Posts: 10Questions: 4Answers: 0

    @allan thanks too. That link cleared a few things up.

  • Al GrantAl Grant Posts: 10Questions: 4Answers: 0

    @rduncecb in the request are you using the start property to work out which page and what data to send to front end?

  • Al GrantAl Grant Posts: 10Questions: 4Answers: 0

    Also does start in sent parameters indicate the record index out of total, or page index?

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    The first question to ask is if you actually need server-side processing? Are you using tens of thousands or more rows? If so fine, carry on with server-side processing - the manual for it is here.

    If not, then drop the serverSide option and just use client-side processing. It will save you needing to worry about the paging on the server-side.

    Allan

  • gkahanagkahana Posts: 3Questions: 1Answers: 0

    Hello allan.
    I need to format decimal to currency in server side datatable.
    My Data which I return in Page is { "mData": "TotalAmount" } I need to display the result in format of e.g 10,000,000.00 instead of e.g 232206368.75

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    @gkahana - I replied on your other thread.

This discussion has been closed.