【问题标题】:How can I restore removed rows in the Bootstrap DataTable?如何恢复 Bootstrap DataTable 中已删除的行?
【发布时间】:2016-12-08 21:44:57
【问题描述】:

我正在使用引导数据表并制作了一个自定义过滤器。我想按值过滤所有动物。例如:

如果我选择“猫”,则只显示具有“猫”类的行。

到目前为止它正在工作,但是当我选择另一种动物时,我无法弄清楚如何再次重置已删除的行。有一次我的行不见了,我就无法挽回了。

var table = $('.data-table').DataTable({
 "dom": "<'row'<'col-sm-4'l><'col-sm-4'f><'col-sm-4 customFilterHolder'>>" +
 "<'row'<'col-sm-12'tr>>" +
 "<'row'<'col-sm-5'i><'col-sm-7'p>>",
 "scrollX": true,    
  initComplete: function(){
      $(".customFilterHolder").html('<div style="float:right;min-width:200px"><div style="float:right;"><select name="select" class="select form-control select2" style="width: 100%;"><option value="all">All</option><option value="dog">Dog</option><option value="cat">Cat</option><option value="horse">Horse</option></select></div><div style="float:right;margin:4px 10px 10px">Filter</div></div>');      

      $('.select').on('change',function(){
          table.rows(':not(.' + $(this).val() + ')').remove().draw(true);
      });    
   }
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://almsaeedstudio.com/themes/AdminLTE/plugins/datatables/dataTables.bootstrap.css">

<script src="https://almsaeedstudio.com/themes/AdminLTE/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script src="https://almsaeedstudio.com/themes/AdminLTE/plugins/datatables/dataTables.bootstrap.min.js"></script>

<table class="data-table table table-bordered table-striped">
  <thead>
    <tr>
      <th>Name</th>
      <th>Animal</th>
      <th>City</th>

    </tr>
  </thead>
  <tbody>
    <tr class="all cat">
      <td>Tiger Nixon</td>
      <td>Cat</td>
      <td>Edinburgh</td>
      
    </tr>
    <tr class="all cat">
      <td>Garrett Winters</td>
      <td>Cat</td>
      <td>Tokyo</td>
      
    </tr>
    <tr class="all dog">
      <td>Ashton Cox</td>
      <td>Dog</td>
      <td>San Francisco</td>
      
    </tr>
      <tr class="all horse">
      <td>Lesley Manning</td>
      <td>Horse</td>
      <td>Washington</td>
      
    </tr>
  </tbody>
</table>

【问题讨论】:

    标签: jquery twitter-bootstrap datatables


    【解决方案1】:

    正如docs 明确指出的那样,remove() 实际上删除了行。如果您希望能够恢复已删除的行,则必须自己实现该功能。这是一个非常简单的插件示例,它维护已删除的&lt;tr&gt; 列表:

    var removedRows = {};
    $.fn.dataTable.Api.register('row().hide()', function(index) {
      if (index && removedRows[index]) {
        var row = this.table().row.add( removedRows[index].html);
        delete removedRows[index]
    
        //here the reinserted row can be manipulated
        row.nodes().to$().css('color', 'green')
      } else {
        removedRows[this.index()] = { html: this.nodes().to$()[0] };
        this.remove()
      }
      return this
    });
    

    现在table.row(&lt;selector&gt;).hide() 将删除一行。
    table.row().hide(deletedIndex) 将按原始索引恢复已删除的行

    所有删除的行都可以恢复

    Object.keys(removedRows).forEach(function(index) {
      table.row().hide(index).draw();
    }) 
    

    演示 -> http://jsfiddle.net/4uvrgbx3/

    【讨论】:

      【解决方案2】:

      我找到了另一个适合我的解决方案:

      table.columns(1).search($(this).val()).draw();
      

      【讨论】:

      • 唯一的问题是,我无法返回“全部”
      猜你喜欢
      • 1970-01-01
      • 2015-12-18
      • 1970-01-01
      • 2016-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多