【问题标题】:How can I use a select2 selectbox with dataTables?如何将 select2 选择框与 dataTables 一起使用?
【发布时间】:2017-07-05 13:48:51
【问题描述】:

我想在我的引导数据表中使用一个 select2 选择框。我需要使用full select2 js 但它不起作用,我想知道为什么。

$(document).ready(function() {
    var table = $('#example').DataTable( {
        scrollY:        "300px",
        scrollX:        true,
        scrollCollapse: true,
        paging:         false,
        fixedColumns:   {
            leftColumns: 1,
            rightColumns: 1
        }
    } );
} );

$("#e1").select2();
td, th{background-color:white;}
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>

<link href="http://cdnjs.cloudflare.com/ajax/libs/select2/3.2/select2.css" rel="stylesheet"/>

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/fixedcolumns/3.2.2/js/dataTables.fixedColumns.min.js"></script>

<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>


<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.full.js"></script>


<table id="example" class="stripe row-border order-column" width="100%" cellspacing="0">
        <thead>
            <tr>
                <th>First name</th>
                <th>Last name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
                <th>Extn.</th>
                <th>E-mail</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><select  id="e1">
        <option value="AL">Alabama</option>
        <option value="Am">Amalapuram</option>
        <option value="An">Anakapalli</option>
        <option value="Ak">Akkayapalem</option>
        <option value="WY">Wyoming</option>
    </select></td>
                <td>Satou</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>33</td>
                <td>2008/11/28</td>
                <td>$162,700</td>
                <td>5407</td>
                <td>a.satou@datatables.net</td>
            </tr>
        </tbody>
    </table>

【问题讨论】:

    标签: jquery datatables jquery-select2


    【解决方案1】:
    1. 您正在使用 CSS 3.2 版本以及 4.0.3 版本的库!由于版本 4 是完全重写的,因此任何 UI 都会失败。查找匹配的版本here

    2. 你需要实例化 select2 dataTables 被初始化之后。您可以在 drawCallback 回调中执行此操作。

    3. 不要使用id's 来引用select2's。我猜你会在多个页面上有很多 select2,所以给他们一个像dt-select2 这样的虚拟类,这样你就可以在一次调用中初始化所有可见的 select2。但无论如何都要保留id 以供事件处理程序中参考。

    例子:

    $('#example').DataTable({
      ...
      drawCallback: function() {
         $('.dt-select2').select2();
      }
    })  
    

    demo

    【讨论】:

    • 嘿@peace_love,“工具栏”是什么意思?
    • @peace_love 您必须针对特定元素,尝试使用$('.dataTables_length select').select2() -> jsfiddle.net/349kL0pu
    【解决方案2】:

    请参阅此示例,在 filter the data 的标头上包含 select 选项:

    演示here

    $('#example').DataTable({
      initComplete: function() {
        this.api().columns('.fName').every(function() {
          var column = this;
          var select = $('<select class="f"><option value="">First Name</option></select>')
            .appendTo($(column.header()).empty())
            .on('change', function() {
              var val = $.fn.dataTable.util.escapeRegex(
                $(this).val()
              );
    
              column
                .search(val ? '^' + val + '$' : '', true, false)
                .draw();
            });
    
          column.data().unique().sort().each(function(d, j) {
            select.append('<option value="' + d + '">' + d + '</option>')
          });
        });
        this.api().columns('.lName').every(function() {
          var column = this;
          var select = $('<select class="f"><option value="">Last Name</option></select>')
            .appendTo($(column.header()).empty())
            .on('change', function() {
              var val = $.fn.dataTable.util.escapeRegex(
                $(this).val()
              );
    
              column
                .search(val ? '^' + val + '$' : '', true, false)
                .draw();
            });
    
          column.data().unique().sort().each(function(d, j) {
            select.append('<option value="' + d + '">' + d + '</option>')
          });
        });
      }
    
    
    })
    $(document).ready(function($) {
      $('.f').select2();
    });
    

    【讨论】:

      猜你喜欢
      • 2021-06-07
      • 2014-08-14
      • 1970-01-01
      • 2016-01-30
      • 2015-12-13
      • 2018-04-22
      • 2016-11-23
      • 2013-02-15
      • 1970-01-01
      相关资源
      最近更新 更多