adding a new row to server side datatable without ajax call

adding a new row to server side datatable without ajax call

fdalexfdalex Posts: 4Questions: 0Answers: 0

Hi, here is my problem :
I have a datatable and I want to add a new row, data of this row comes from a websocket and I would like to add this like as the first line of my datatable to say like "this line is new" dynamically. The problem is, when I write mydatatable.row.add([...]); I need to do a .draw() to update the display, but then it's doing an ajax call and remove the edit I've done.

I've read some post saying that row.add is not really what I should do with server side enabled, but ... is there a way to do this ?
Any way to do a .draw() but skip the ajax call and return an edited version of the actual data ? (maybe override the .draw() function with a parameter value, for example, .draw('noajax'), something like that ? So it would redraw the table based on the actual data )

Many thanks

Replies

  • colincolin Posts: 15,163Questions: 1Answers: 2,588

    Hi @fdalex ,

    As you say, you need the data there after the draw, so one option would be to use a function for ajax.dataSrc (see the last example on that page) and add that row into the returned data.

    Cheers,

    Colin

  • fdalexfdalex Posts: 4Questions: 0Answers: 0

    Thanks you, I will try this

  • fdalexfdalex Posts: 4Questions: 0Answers: 0
    edited November 2019

    hi ! I managed to do what I wanted with ajax.dataSrc, thank you @colin !

    $('#example').dataTable( {
      "ajax": {
        "url": "data.json",
        "dataSrc": function ( json ) {
              var dataClone = [...json.data];    
              dataClone.push(adding new line from webocket);
              return dataClone;
        }
      }
    } );
    
This discussion has been closed.