Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery dataTables - Checking and adding new row if not exist using API .any()

I am trying to add new row in datatables, and by using the API .any() to check if the id is already exist in the rows and if it exist I will not add new row to my datatable, and here is the result form my request from databse see http://pastie.org/10196001 , but I am having trouble in checking.

socket.on('displayupdate',function(data){
     var dataarray = JSON.parse(data);
     dataarray.forEach(function(d){
         if ( table.row.DT_RowId(d.DT_RowId).any() ) { // TypeError: table.row.DT_RowId is not a function
            console.log('already exist cannot be added');
         }else{
            table.row.add(d).draw();
         }
     });
 });

Thank you in advance.

like image 412
jemz Avatar asked Jul 06 '26 20:07

jemz


1 Answers

You get the error, of course, because DT_RowId not is a function in the API. But DT_RowId is in fact the one and only property that get some special treatment from dataTables :

By assigning the ID you want to apply to each row using the property DT_RowId of the data source object for each row, DataTables will automatically add it for you.

So why not check rows() for that automatically injected id along with any()?

socket.on('displayupdate',function(data){
   var DT_RowId,
       dataarray = JSON.parse(data); 
   dataarray.forEach(function(d){
       DT_RowId = d.DT_RowId;
       if (table.rows('[id='+DT_RowId+']').any()) {
          console.log('already exist cannot be added');
       } else {
          table.row.add(d).draw();
       }
   });
});

simplified demo -> http://jsfiddle.net/f1yyuz1c/

like image 144
davidkonrad Avatar answered Jul 11 '26 13:07

davidkonrad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!