简体   繁体   中英

How to add new row in a DataTable

I have DataTable in my js file.

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

Using this I can able to add the new row in the " $('#student_detail').DataTable({}); " datatable

I have to fetch some datas from the DB and i did get it in the DataTable. But after that i cant able to add new row in the dataTable just like before one.

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

When i try to add a new row I see that "Processing" but i cant able to add the new empty row. What am i doing wrong. Can anyone help.

The Result from the server side looks like this

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

Can anyone help me with this. Everything seems to be fine. but why cant i able to add a new?

I think you may need to render the table after the row is added. It looks like you are getting the data, but not 'redrawing' the table so to speak.

I used the jQuery template library to do this. You would need to write something similar to this:

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

This could be wrong, but maybe it will give you a place to start? I am relatively new to JQuery and JS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM