How to add multiple rows in datatables jquery

后端 未结 4 1321
旧时难觅i
旧时难觅i 2021-01-01 22:21

I have used https://datatables.net/reference/api/rows.add%28%29 link working but the data showing the table as [object,object]. How to show the object to string

4条回答
  •  抹茶落季
    2021-01-01 22:43

    I came across this problem too - I found the documentation to be less than clear. Their example on https://datatables.net/reference/api/rows.add() does not work when I pass my own dynamically created array of objects.

    In order to get it working, you have to specify the columns' names when instantiating DataTables.

    In any case, the below is a working solution.

    var DataTable = $('#tableName').DataTable({
      iDisplayLength: 15,   // number of rows to display
      columns: [
        { data: 'id' },
        { data: 'name' },
        { data: 'car' },
      ]
    });
    
    // assume this is a dynamically created array of objects
    var persons = [
      {
        id: 1,
        name: 'John',
        car: 'Mercedes',
      }, 
      {
        id: 2,
        name: 'Dave',
        car: 'BMW',
      }, 
      {
        id: 3,
        name: 'Ken',
        car: 'Jeep',
      },  
    ];
    
    DataTable.rows.add(persons).draw();
    

提交回复
热议问题