3

我正在尝试在数据表中添加新行,并通过使用 API .any() 检查行中是否已经存在 id,如果存在,我不会向我的数据表中添加新行,这是结果形式我来自数据库的请求见http://pastie.org/10196001,但我在检查时遇到了麻烦。

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();
         }
     });
 });

先感谢您。

4

1 回答 1

3

当然,您会得到错误,因为DT_RowIdnot 是 API 中的函数。但实际上是唯一一个从 dataTables 获得特殊处理的属性:DT_RowId

通过使用每行的数据源对象的属性 DT_RowId 为每行分配要应用的 ID,DataTables 将自动为您添加它。

那么为什么不检查rows()自动注入idany()呢?

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();
       }
   });
});

简化演示-> http://jsfiddle.net/f1yyuz1c/

于 2015-05-19T11:15:22.393 回答