无法一次删除数据表中的多行

时间:2019-08-28 07:33:34

标签: jquery html datatables

我无法删除使用复选框选择的多行。当我选择这些行并单击删除按钮时,所选的第一行将被删除,而其他行则不会被删除。

我想一次删除多行。我是数据表的新手。我该怎么办?

$(document).ready(function() {
  var t = $('#table').DataTable();
  $('#add').on('click', () => {
    $("#popup").dialog({
      width: 400,
      modal: true,
      resizeable: false,
      buttons: {
        "Submit": function() {
          t.row.add([
            '<input type="checkbox" class="select">',
            $('#name').val(),
            $("#age").val(),
            '<button type="button"  class="edit" >Edit</button>'
          ]).draw(false);
          $(this).dialog("close");
        }
      }
    });
  })
  
  $("#delete").on("click", function() {
    //$('table tr').has('input:checked').remove();
    t.row($('table tr').has('input:checked'))
      .remove()
      .draw();
  })
  
  $('#deleteall').on('click', function() {
    //t.$('tbody tr').remove();
    t.clear().draw();
  })
  
  $('tbody').on('click', ".edit", (event) => {
    $("#popup").dialog({
      width: 400,
      modal: true,
      resizeable: false,
      buttons: {
        "Submit": function() {
          var name = '<tr><td><input type="checkbox" class="select"></td><td>' +
            $('#name').val() + '</td><td>' +
            $("#age").val() +
            '</td><td>' +
            '<button type="button"  class="edit" >Edit</button>' +
            '</td></tr>';
          t.row($(event.currentTarget).parents('tr'))
            .remove()
            .draw();
          t.row.add([
            '<input type="checkbox" class="select">',
            $('#name').val(),
            $("#age").val(),
            '<button type="button"  class="edit" >Edit</button>'
          ]).draw();
          //console.log($('tr'));
          $('.add-input').val('');
          $(this).dialog("close");
        }
      }
    })
  })
})
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript" src="tab-mod.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" />
<script type="text/javascript" src="http://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<div class="table-wrap">
  <table id="table" border="1">
    <thead>
      <tr>
        <th>Select</th>
        <th>Name</th>
        <th>Age</th>
        <th>Edit</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><input type="checkbox" class="select"></td>
        <td>Sakthi</td>
        <td>20</td>
        <td><button type="button" class="edit">Edit</button></td>
      </tr>
      <tr>
        <td><input type="checkbox" class="select"></td>
        <td>Prabhu</td>
        <td>21</td>
        <td><button type="button" class="edit">Edit</button></td>
      </tr>
      <tr>
        <td><input type="checkbox" class="select"></td>
        <td>Sakthi</td>
        <td>20</td>
        <td><button type="button" class="edit">Edit</button></td>
      </tr>

    </tbody>
  </table>
</div>
<div class="op">
  <button type="button" class="mod" id="add">Add</button>
  <button type="button" class="mod" id="delete">Delete</button>
  <button type="button" class="mod" id="deleteall">Delete All</button>

</div>
<div class="popup" id="popup" style="display:none;">
  <input type="text" placeholder="Name" class="add-input" id="name">
  <input type="number" placeholder="Age" class="add-input" id="age">
</div>

1 个答案:

答案 0 :(得分:3)

问题是因为table.row()一次只能检索一行。为它提供多行将意味着仅选择第一个。

要解决此问题,只需使用rows(),因为它接受多行作为参数:

$("#delete").on("click", function() {
  t.rows($('table tr').has('input:checked')).remove().draw();
})

$(document).ready(function() {
  var t = $('#table').DataTable();
  $('#add').on('click', () => {
    $("#popup").dialog({
      width: 400,
      modal: true,
      resizeable: false,
      buttons: {
        "Submit": function() {
          t.row.add([
            '<input type="checkbox" class="select">',
            $('#name').val(),
            $("#age").val(),
            '<button type="button"  class="edit" >Edit</button>'
          ]).draw(false);
          $(this).dialog("close");
        }
      }
    });
  })

  $("#delete").on("click", function() {
    t.rows($('table tr').has('input:checked')).remove().draw();
  })

  $('#deleteall').on('click', function() {
    //t.$('tbody tr').remove();
    t.clear().draw();
  })

  $('tbody').on('click', ".edit", (event) => {
    $("#popup").dialog({
      width: 400,
      modal: true,
      resizeable: false,
      buttons: {
        "Submit": function() {
          var name = '<tr><td><input type="checkbox" class="select"></td><td>' +
            $('#name').val() + '</td><td>' +
            $("#age").val() +
            '</td><td>' +
            '<button type="button"  class="edit" >Edit</button>' +
            '</td></tr>';
          t.row($(event.currentTarget).parents('tr'))
            .remove()
            .draw();
          t.row.add([
            '<input type="checkbox" class="select">',
            $('#name').val(),
            $("#age").val(),
            '<button type="button"  class="edit" >Edit</button>'
          ]).draw();
          //console.log($('tr'));
          $('.add-input').val('');
          $(this).dialog("close");
        }
      }
    })
  })
})
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript" src="tab-mod.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" />
<script type="text/javascript" src="http://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<div class="table-wrap">
  <table id="table" border="1">
    <thead>
      <tr>
        <th>Select</th>
        <th>Name</th>
        <th>Age</th>
        <th>Edit</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><input type="checkbox" class="select"></td>
        <td>Sakthi</td>
        <td>20</td>
        <td><button type="button" class="edit">Edit</button></td>
      </tr>
      <tr>
        <td><input type="checkbox" class="select"></td>
        <td>Prabhu</td>
        <td>21</td>
        <td><button type="button" class="edit">Edit</button></td>
      </tr>
      <tr>
        <td><input type="checkbox" class="select"></td>
        <td>Sakthi</td>
        <td>20</td>
        <td><button type="button" class="edit">Edit</button></td>
      </tr>
    </tbody>
  </table>
</div>
<div class="op">
  <button type="button" class="mod" id="add">Add</button>
  <button type="button" class="mod" id="delete">Delete</button>
  <button type="button" class="mod" id="deleteall">Delete All</button>
</div>
<div class="popup" id="popup" style="display:none;">
  <input type="text" placeholder="Name" class="add-input" id="name">
  <input type="number" placeholder="Age" class="add-input" id="age">
</div>