Server side processing removing all data from JSON that isn't used in any column

Server side processing removing all data from JSON that isn't used in any column

nessness Posts: 2Questions: 1Answers: 0
edited February 2022 in DataTables 1.10

When using serverSide: true all fields that aren't used by one of the columns are automatically removed from the JSON that's stored under table.data() and cannot be accessed for further processing.

For example I have some custom rendering logic, that adds a star depending on another value's boolean.

table = $('#snowflake').DataTable({
  ajax: {
      url: "/api/entries/",
  },
  processing: true,
  serverSide: true,
  searchDelay: 1500,
  rowId: 'id',
  order: [[0, 'asc']],
  columns: [
    {
      data: 'order',
      className: 'reorder',
    }, {
      data: "name",
      name: "name",
      render: function(data, type, row) {
        if (row.enabled) {  // row.enabled will be undefined
          return data + ' *';
        }
        return data;
      }
    },
  ],
});

JSON returned from server:

[{
    "id": 1,
    "order": 1,
    "name": "Hello world",
    "enabled": true
}, ...]

However because enabled does not have a definition in columns it's not accessible by the name render method, as it's automatically discarded/set to undefined by DataTables.
Any idea how to keep all data returned from the server – just like it would, if we were using the normal, local non-serverSide mode?

PS: More on a sidenote; is it possible to call table.draw() without fetching the data from the API again? I just want to update column visibility.

Answers

  • kthorngrenkthorngren Posts: 21,391Questions: 26Answers: 4,961

    However because enabled does not have a definition in columns it's not accessible by the name render method, as it's automatically discarded/set to undefined by DataTables.

    What you described works here:
    http://live.datatables.net/pitawoho/1/edit

    Maybe all the rows don't have the property enabled.

    is it possible to call table.draw() without fetching the data from the API again?

    Calling draw() will fetch data from the server script and doesn't have an option to not fetch fro the server with server side processing is enabed.

    I just want to update column visibility.

    You shouldn't need to call draw() when updating column visibility. This is also in the example I posted.

    If this doesn't help then please post a link to your page or a test case replicating the issues so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • nessness Posts: 2Questions: 1Answers: 0
    edited February 2022

    What you described works here:
    http://live.datatables.net/pitawoho/1/edit

    Thanks. You're right. I realized my backend is causing the filtering out as described when the "long serverSide-url" is active depending on the columns[X][data]=... setting. Sorry for the hassle.

    http://127.0.0.1:8000/api/entries/?draw=1&columns[0][data]=order&columns[0][name]=&columns[0][searchable]=true&columns[0][orderable]=true&columns[0][search][value]=&columns[0][search][regex]=false&columns[1][data]=object.name&columns[1][name]=object__name&columns[1][searchable]=true&columns[1][orderable]=true&columns[1][search][value]=&columns[1][search][regex]=false&columns[2][data]=object.contact&columns[2][name]=object__contact&columns[2][searchable]=true&columns[2][orderable]=true&columns[2][search][value]=&columns[2][search][regex]=false&columns[3][data]=object.phone&columns[3][name]=object__phone&columns[3][searchable]=true&columns[3][orderable]=true&columns[3][search][value]=&columns[3][search][regex]=false&[...]&format=datatables&_=1644779135588
    

    Regarding the visibility – this is how I'm doing it. Hiding the column works, but showing it again doesn't:

    column = table.column(index);
    column.visible(false);  // works
    column.visible(true);  // table columns are unchanged, until I call table.draw()
    
  • kthorngrenkthorngren Posts: 21,391Questions: 26Answers: 4,961

    I updated the example to show that showing the column doesn't need draw():
    http://live.datatables.net/pitawoho/2/edit

    Again please provide a test case showing the issue so we can help debug.

    Kevin

Sign In or Register to comment.