【问题标题】:How to add new row in a DataTable如何在数据表中添加新行
【发布时间】:2014-08-19 14:41:42
【问题描述】:

我的 js 文件中有 DataTable。

function getStudentDetail(id) {
//id is set globally
  var oTableStudentDetails = $('#student_detail').DataTable({}); 
   $('#btn-add-row').on( 'click', function () {
    oTableStudentDetails.row.add([' ',' ',' ',' ',' ']).draw();
   } );
  }   

使用它,我可以在“ $('#student_detail').DataTable({}); ”数据表中添加新行

我必须从数据库中获取一些数据,并且我确实在 DataTable 中得到了它。但在那之后我不能像之前一样在数据表中添加新行。

function getStudentDetail(id) {
//id is set globally
var oTableStudentDetails = $('#student_detail').DataTable({
    "bProcessing":  true,
    "bServerSide":  true,
    "bDestroy": true,
    "bLengthChange": false,
    "dataType": "json",
    "ajax": {
        "url": url_prefix + "/getStudentDetail",
        "data": function(d) {
            d.idstudent = id;
         }
      }
   });

 $('#btn-add-row').on( 'click', function () {
    oTableStudentDetails.row.add([' ',' ',' ',' ',' ']).draw(); //this is not working  
                                                                  after using ajax 
                                                                  value in DataTable 
   } );
  }  

当我尝试添加新行时,我看到“正在处理”,但我无法添加新的空行。我究竟做错了什么。谁能帮忙。

服务器端的结果是这样的

 {"aaData": [["1", "roshi", "sssss", "xxxxxxxxxxx", "xxxxxxxxxxx"]], "iTotalRecords": 1, "sEcho": 99, "iTotalDisplayRecords": 1}

谁能帮我解决这个问题。一切似乎都很好。但是为什么我不能添加一个新的?

【问题讨论】:

  • @@usr:no.为了我的纪念我这样创造

标签: javascript jquery datatable


【解决方案1】:

我认为您可能需要在添加行后渲染表格。看起来您正在获取数据,但可以说不是“重绘”表格。

我使用 jQuery 模板库来执行此操作。你需要写类似这样的东西:

$('page').tmpl(studentTable).appendTo($(page).find('#yourTable tbody'));

这可能是错误的,但也许它会给你一个起点?我对 JQuery 和 JS 比较陌生。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    • 2017-12-11
    • 2020-05-13
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    相关资源
    最近更新 更多