我已经在表的初始化阶段阅读了使用fnCreatedRow的知识,但是该解决方案对我不起作用,因为我需要在table.row.add阶段添加ID,因为我要插入的ID来自ajax在添加行之前进行的成功调用。

基本上-当用户单击我的addRow按钮时,我向服务器发送请求,并首先在数据库中创建新行,然后返回该新创建行的唯一lineID。然后,我想将该lineID用作此表第一个单元格上的id元素,以便以后用户保存页面时可以引用它。

这是我的功能:

$('#addRow').on( 'click', function () {

  var q         = <?php echo json_encode($q); ?>;
  var d         = $('#d').html();

  $.ajax({
    type: 'POST',
    url: 'addRow.php',
    data: { q:q, d:d },
      success:function(data){
        newRowId = data;
      }
  });

  lastRow++;
  table.row.add([
    lastRow, /* add id = newRowID to this cell */
    "",
    "",
    "",
    "",
    ""
  ] ).draw( false );


});


更新资料

我找到了一种添加ID的方法,但是我只能在ROW本身而不是在单元格内获取它:

$.ajax({
    type: 'POST',
    url: 'addRow.php',
    data: { q:q, d:d },
      success:function(data){
        success       = JSON.parse(data);
        uCount        = success[0];
        uLineID       = success[1];

        lastRow++;
        var newRow = table
            .row.add([
              lastRow,
              "",
              "",
              "",
              "",
              ""
            ] )
            .draw( )
            .node( );

        $( newRow )
            .eq(0).attr( 'id', uLineID); /* I tried this */
            .attr('id', uLineID);        /* and this, separately */
      }
  });

最佳答案

使用下面的代码为新添加的行中的第一个td元素设置ID属性。

var newRow = table.row.add([
   lastRow,
   "",
   "",
   "",
   "",
   ""
]).draw( false ).node();

$('td:eq(0)', newRow).attr('id', uLineID);


有关更多信息,请参见row.add()

08-18 03:59
查看更多