Jquery数据表在表中添加新行按钮作为行

时间:2017-02-27 23:41:40

标签: jquery datatables

我正在使用:jquery.dataTables.js来自:https://datatables.net

如何将此按钮添加新行作为一行放在表格内?现在不在桌子旁边

jsfiddle:http://jsfiddle.net/5L2qy092/4/

HTML:

<table id="example" class="display" width="100%" cellspacing="0">
  <thead>
    <tr>
      <th>order</th>
      <th>name</th>
      <th>country</th>
      <th>delete</th>
    </tr>
  </thead>
</table>

<button id="addRow">Add New Row</button>
<table id="newRow">
  <tbody>
    <tr>
      <td>Line 2
        <input type="hidden" value="2" /> </td>
      <td>DVap
        <input type="hidden" value="DVap" /> </td>
      <td>
        <input type="text" value="22" /> </td>
      <td>
        <i class="fa fa-minus-square" aria-hidden="true"></i>  </td>
    </tr>
  </tbody>
</table>

jquery的:

 $(document).ready(function() {
    var dt = $('#example').dataTable();
    dt.fnDestroy();
  });

  $(document).ready(function() {
    //var url = 'http://www.json-generator.com/api/json/get/ckDfqBChNK?indent=2';
    var url = 'http://www.json-generator.com/api/json/get/bQzyuEGndu?indent=2';
    var table = $('#example').DataTable({
      ajax: url,
      rowReorder: {
        dataSrc: 'order',
      },
      columns: [{
        data: 'order'
      }, {
        data: 'place'
      }, {
        data: 'name'
      }, {
        data: 'delete'
      }],
      "initComplete": function(oSettings) {
        $(this).on('click', "i.fa.fa-minus-square", function(e) {
          table.row( $(this).closest('tr') ).remove().draw();
        });
      }
    });

    // add row
    $('#addRow').click(function() {
      //t.row.add( [1,2,3] ).draw();
      var rowHtml = $("#newRow").find("tr")[0].outerHTML
      console.log(rowHtml);
      table.row.add($(rowHtml)).draw();
    });
  });

2 个答案:

答案 0 :(得分:1)

我对小提琴进行了一些更改,看看这是否是你想要的结果。

 $(document).ready(function() {
//var url = 'http://www.json-generator.com/api/json/get/ckDfqBChNK?indent=2';
var url = 'http://www.json-generator.com/api/json/get/bQzyuEGndu?indent=2';
var table = $('#example').DataTable({
  ajax: url,
  rowReorder: {
    dataSrc: 'order'
  },
  columns: [{
    data: 'order'
  }, {
    data: 'place'
  }, {
    data: 'name'
  }, {
    data: 'delete'
  }],

   "fnDrawCallback": function() {
    try {addRow();} catch(e) {}
},

  "initComplete": function(oSettings) {

    $(this).on('click', "i.fa.fa-minus-square", function(e) {

      table.row( $(this).closest('tr') ).remove().draw();

      addRow();

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

      var rowHtml = $("#newRow").find("tr")[0].outerHTML

      table.row.add($(rowHtml)).draw();

      addRow();

    });

   addRow();

  }

});


function addRow() {

   table.rows().eq(0).each( function ( i ) {
   var row = table.row( i );
   row.child.remove();
   });

    var line = table.row( $('#example').find('tr:last') );  

    line.child('<button id="addRow">Add New Row</button>').show(); 

   }

 });

http://jsfiddle.net/Leon_Klaj/9grm4wdc/

答案 1 :(得分:1)

如果您希望buttonnew rowtable,则可以使用我的第一个答案。

但是,在这种情况下,我们将不得不处理排序,删除等。

我认为最好是模拟&#39;一排新的。像这样:

&#13;
&#13;
$(document).ready(function() {
    var dt = $('#example').dataTable();
    dt.fnDestroy();
  });

  $(document).ready(function() {
    //var url = 'http://www.json-generator.com/api/json/get/ckDfqBChNK?indent=2';
    var url = 'http://www.json-generator.com/api/json/get/bQzyuEGndu?indent=2';
    var table = $('#example').DataTable({
      ajax: url,
      rowReorder: {
        dataSrc: 'order'
      },
      columns: [{
        data: 'order'
      }, {
        data: 'place'
      }, {
        data: 'name'
      }, {
        data: 'delete'
      }],
      
      "initComplete": function(oSettings) {
      
        $(this).on('click', "i.fa.fa-minus-square", function(e) {
          
          table.row( $(this).closest('tr') ).remove().draw();
           
        });
     
      
      }
      
    });
    
    $('#example').css('border-bottom', 'none');
    $('<div class="addRow"><button id="addRow">Add New Row</button></div>').insertAfter('#example');
    $('#addRow').on('click', function() {
        
          var rowHtml = $("#newRow").find("tr")[0].outerHTML

          table.row.add($(rowHtml)).draw();
          
          table.page( 'last' ).draw( 'page' );
        
        });
   
  });
&#13;
table#newRow {
  display: none
}

div.addRow {
    line-height: 45px;
    background-color: #fff;
    padding-left: 10px;
    border-bottom: 1px solid;
    border-top: 1px solid #e5e5e5;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>

<script src="https://cdn.datatables.net/rowreorder/1.2.0/js/dataTables.rowReorder.min.js"></script>

<link href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.css" rel="stylesheet"/>

<link href="https://cdn.datatables.net/rowreorder/1.2.0/css/rowReorder.dataTables.min.css" rel="stylesheet"/>

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>


<table id="example" class="display" width="100%" cellspacing="0">
  <thead>
    <tr>
      <th>order</th>
      <th>name</th>
      <th>country</th>
      <th>delete</th>
    </tr>
  </thead>
</table>

<table id="newRow">
  <tbody>
    <tr>
      <td>Line 2
        <input type="hidden" value="2" /> </td>
      <td>DVap
        <input type="hidden" value="DVap" /> </td>
      <td>
        <input type="text" value="22" /> </td>
      <td>
        <i class="fa fa-minus-square" aria-hidden="true"></i>  </td>
    </tr>
  </tbody>
</table>
&#13;
&#13;
&#13;