【问题标题】:how to add a button inside a dropdown select using jQuery如何使用jQuery在下拉选择中添加按钮
【发布时间】:2020-07-20 16:00:16
【问题描述】:

我正在使用jQuery DataTables。在表尾,我在每一列添加了bootstrap-select 来过滤数据。

我想在选择下拉菜单中添加一个按钮“清除过滤器”,如下所示:

按钮的位置无关紧要,可以在搜索框下方,也可以在末尾...

所以包装搜索框的div 有一个.bs-searchbox 类,所以我所做的是:在当前列中找到具有该类的div,然后在其中附加我的按钮。

var button = column.find('.bs-searchbox');
$('<button type="button" class="btn btn-sm btn-light">Clear filter</button>').appendTo(button); 

jQuery 无法将 .find 识别为函数。请在下面找到我的代码的详细说明。

你能告诉我我做错了什么吗?非常感谢。

$(document).ready(function() {

  var table = $('#example').DataTable({
    searching: false,
    info: false,
    paging: false,

    initComplete: function() {
      // loop through each colum in my datatable          
      this.api().columns().every(function() {

        var column = this;
        //append bootstrap selectpicker (multiple) on footer of current colum 
        var select = $('<select class="form-control show-tick" data-container="body" data-header="Select option(s)" data-actions-box="true" data-live-search="true" title="All" data-selected-text-format="count > 0" multiple></select>')
          .appendTo($(column.footer()).empty());

        //get unique values of each column and append as options
        column.data().unique().sort().each(function(d, j) {
          select.append('<option value="' + d + '">' + d + '</option>');
        });

        //add button 'clear filter' inside the select after search box
        /*
        var button = column.find('.bs-searchbox');
         $('<button type="button" class="btn btn-sm btn-light">Clear filter</button>').appendTo(button); 
        */

      });

      //apply bootstrap selectpicker
      $("select").selectpicker();

    }
  });
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css" rel="stylesheet">
<link href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap4.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.17/css/bootstrap-select.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.17/js/bootstrap-select.min.js"></script>

<table id="example" class="table table-bordered table-hover nowrap" style="width:100%">
  <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Ashton Cox</td>
      <td>Junior Technical Author</td>
      <td>San Francisco</td>
    </tr>
    <tr>
      <td>Cedric Kelly</td>
      <td>Senior Javascript Developer</td>
      <td>Edinburgh</td>
    </tr>
    <tr>
      <td>Airi Satou</td>
      <td>Accountant</td>
      <td>Tokyo</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
    </tr>
  </tfoot>
</table>

【问题讨论】:

    标签: javascript html jquery datatables bootstrap-selectpicker


    【解决方案1】:

    尝试在$("select").selectpicker();之后添加按钮

    见下面的代码。

    $(document).ready(function() {
    
      var table = $('#example').DataTable({
        searching: false,
        info: false,
        paging: false,
    
        initComplete: function() {
          // loop through each colum in my datatable          
          this.api().columns().every(function() {
    
            var column = this;
            //append bootstrap selectpicker (multiple) on footer of current colum 
            var select = $('<select class="form-control show-tick" data-container="body" data-header="Select option(s)" data-actions-box="true" data-live-search="true" title="All" data-selected-text-format="count > 0" multiple></select>')
              .appendTo($(column.footer()).empty());
    
            //get unique values of each column and append as options
            column.data().unique().sort().each(function(d, j) {
              select.append('<option value="' + d + '">' + d + '</option>');
            });
    
          });
    
          //apply bootstrap selectpicker
          $("select").selectpicker();
          
          var button = $('.bs-searchbox');
             $('<button type="button" class="btn btn-sm btn-light">Clear filter</button>').appendTo(button); 
            
    
        }
      });
    });
    

    【讨论】:

    • 非常感谢。正是我需要的
    猜你喜欢
    • 2021-09-23
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 2011-06-29
    相关资源
    最近更新 更多