使用多列 JQuery 过滤和复选框选择与多选选项一起。

huangapple go评论76阅读模式
英文:

Useing multi-column JQuery filter and Checkbox selection with multi-select option together

问题

我尝试同时使用两个 JQuery 数据表功能。
1) 每行前面的复选框,带有多选选项。
https://datatables.net/extensions/select/examples/initialisation/checkbox.html。和
2) 多列过滤和搜索,如下例所示。
https://datatables.net/examples/api/multi_filter.html

我将这两个代码段合并在一起,但它不起作用。可能出了什么问题?

```$(document).ready(function () {
    // 设置 - 在每个页脚单元格中添加文本输入
    $('#example tfoot th').each(function () {
        var title = $(this).text();
        $(this).html('<input type="text" placeholder="Search ' + title + '" />');
    });
 
    // 数据表
    var table = $('#example').DataTable({
        initComplete: function () {
            // 应用搜索
            this.api()
                .columns()
                .every(function () {
                    var that = this;
 
                    $('input', this.footer()).on('keyup change clear', function () {
                        if (that.search() !== this.value) {
                            that.search(this.value).draw();
                        }
                    });
                });
        },
         
        $(document).ready(function() {
    $('#example').DataTable( {
        columnDefs: [ {
            orderable: false,
            className: 'select-checkbox',
            targets:   0
        } ],
        select: {
            style:    'os',
            selector: 'td:first-child'
        },
        order: [[ 1, 'asc' ]]
    } );
} );
    });
});
英文:

I am trying to use two features from JQuery datatable together.

  1. checkbox infront of each row with multi-select option
    https://datatables.net/extensions/select/examples/initialisation/checkbox.html. and
  2. multi-column filter and search as in below example
    https://datatables.net/examples/api/multi_filter.html

I added both the codes together but it is not working. What could be wrong here?

    // Setup - add a text input to each footer cell
    $('#example tfoot th').each(function () {
        var title = $(this).text();
        $(this).html('<input type="text" placeholder="Search ' + title + '" />');
    });
 
    // DataTable
    var table = $('#example').DataTable({
        initComplete: function () {
            // Apply the search
            this.api()
                .columns()
                .every(function () {
                    var that = this;
 
                    $('input', this.footer()).on('keyup change clear', function () {
                        if (that.search() !== this.value) {
                            that.search(this.value).draw();
                        }
                    });
                });
        },
         
        $(document).ready(function() {
    $('#example').DataTable( {
        columnDefs: [ {
            orderable: false,
            className: 'select-checkbox',
            targets:   0
        } ],
        select: {
            style:    'os',
            selector: 'td:first-child'
        },
        order: [[ 1, 'asc' ]]
    } );
} );
    });
});

</details>


# 答案1
**得分**: 1

问题出在你将这两个功能组合在一起的方式上。根据你的 JavaScript 代码,它会作为一个独立的功能,因为你初始化了数据表两次,即 `$(&#39;#example&#39;)..`。

***演示代码***:

```js
$(document).ready(function() {
  // 排除第一列,因为它是复选框列
  $(&#39;#example tfoot th:not(:first)&#39;).each(function() {
    var title = $(this).text();
    $(this).html(&#39;&lt;input type=&quot;text&quot; placeholder=&quot;搜索 &#39; + title + &#39;&quot; /&gt;&#39;);
  });
  $(&#39;#example&#39;).DataTable({
    columnDefs: [{
      orderable: false,
      className: &#39;select-checkbox&#39;,
      targets: 0
    }],
    select: {
      style: &#39;os&#39;,
      selector: &#39;td:first-child&#39;
    },
    order: [
      [1, &#39;asc&#39;]
    ],
    initComplete: function() { // 只在这里添加搜索部分

      // 应用搜索
      this.api()
        .columns()
        .every(function() {
          var that = this;

          $(&#39;input&#39;, this.footer()).on(&#39;keyup change clear&#39;, function() {
            if (that.search() !== this.value) {
              that.search(this.value).draw();
            }
          });
        });
    }
  });
});
tfoot input {
  width: 100%;
  padding: 3px;
  box-sizing: border-box;
}
<link rel=&quot;stylesheet&quot; href=&quot;https://cdn.datatables.net/1.13.2/css/jquery.dataTables.min.css&quot; />
<link rel=&quot;stylesheet&quot; href=&quot;https://cdn.datatables.net/select/1.6.0/css/select.dataTables.min.css&quot; />
<script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;></script>
<script src=&quot;https://cdn.datatables.net/1.13.2/js/jquery.dataTables.min.js&quot;></script>
<script src=&quot;https://cdn.datatables.net/select/1.6.0/js/dataTables.select.min.js&quot;></script>
<table id=&quot;example&quot; class=&quot;display&quot; style=&quot;width:100%&quot;>
  <thead>
    <tr>
      <th></th>
      <th>姓名</th>
      <th>职位</th>
      <th>办公室</th>
      <th>年龄</th>
      <th>薪水</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td>
      <td>Tiger Nixon</td>
      <td>系统架构师</td>
      <td>爱丁堡</td>
      <td>61</td>
      <td>$320,800</td>
    </tr>
    <tr>
      <td></td>
      <td>Garrett Winters</td>
      <td>会计</td>
      <td>东京</td>
      <td>63</td>
      <td>$170,750</td>
    </tr>
    <tr>
      <td></td>
      <td>Ashton Cox</td>
      <td>初级技术作者</td>
      <td>旧金山</td>
      <td>66</td>
      <td>$86,000</td>
    </tr>
    <tr>
      <td></td>
      <td>Ashton XYX</td>
      <td>初级作者</td>
      <td>美国</td>
      <td>60</td>
      <td>$87,000</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th></th>
      <th>姓名</th>
      <th>职位</th>
      <th>年龄</th>
      <th>入职日期</th>
      <th>薪水</th>
    </tr>
  </tfoot>
</table>
英文:

Issue is in the way you have combine both the feature together.Currently according to your js code it acts as a separate feature because you are initializing your datatable twice i.e: $(&#39;#example&#39;)..

Demo Code :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function() {
//excluding the first column its for checkbox.. so
$(&#39;#example tfoot th:not(:first)&#39;).each(function() {
var title = $(this).text();
$(this).html(&#39;&lt;input type=&quot;text&quot; placeholder=&quot;Search &#39; + title + &#39;&quot; /&gt;&#39;);
});
$(&#39;#example&#39;).DataTable({
columnDefs: [{
orderable: false,
className: &#39;select-checkbox&#39;,
targets: 0
}],
select: {
style: &#39;os&#39;,
selector: &#39;td:first-child&#39;
},
order: [
[1, &#39;asc&#39;]
],
initComplete: function() { //add here only the search part
// Apply the search
this.api()
.columns()
.every(function() {
var that = this;
$(&#39;input&#39;, this.footer()).on(&#39;keyup change clear&#39;, function() {
if (that.search() !== this.value) {
that.search(this.value).draw();
}
});
});
}
});
});

<!-- language: lang-css -->

tfoot input {
width: 100%;
padding: 3px;
box-sizing: border-box;
}

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.datatables.net/1.13.2/css/jquery.dataTables.min.css&quot; /&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.datatables.net/select/1.6.0/css/select.dataTables.min.css&quot; /&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.datatables.net/1.13.2/js/jquery.dataTables.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.datatables.net/select/1.6.0/js/dataTables.select.min.js&quot;&gt;&lt;/script&gt;
&lt;table id=&quot;example&quot; class=&quot;display&quot; style=&quot;width:100%&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Position&lt;/th&gt;
&lt;th&gt;Office&lt;/th&gt;
&lt;th&gt;Age&lt;/th&gt;
&lt;th&gt;Salary&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;Tiger Nixon&lt;/td&gt;
&lt;td&gt;System Architect&lt;/td&gt;
&lt;td&gt;Edinburgh&lt;/td&gt;
&lt;td&gt;61&lt;/td&gt;
&lt;td&gt;$320,800&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;Garrett Winters&lt;/td&gt;
&lt;td&gt;Accountant&lt;/td&gt;
&lt;td&gt;Tokyo&lt;/td&gt;
&lt;td&gt;63&lt;/td&gt;
&lt;td&gt;$170,750&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;Ashton Cox&lt;/td&gt;
&lt;td&gt;Junior Technical Author&lt;/td&gt;
&lt;td&gt;San Francisco&lt;/td&gt;
&lt;td&gt;66&lt;/td&gt;
&lt;td&gt;$86,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;Ashton XYX&lt;/td&gt;
&lt;td&gt;JuniorAuthor&lt;/td&gt;
&lt;td&gt;US&lt;/td&gt;
&lt;td&gt;60&lt;/td&gt;
&lt;td&gt;$87,000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;tfoot&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Position&lt;/th&gt;
&lt;th&gt;Age&lt;/th&gt;
&lt;th&gt;Start date&lt;/th&gt;
&lt;th&gt;Salary&lt;/th&gt;
&lt;/tr&gt;
&lt;/tfoot&gt;
&lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月27日 12:10:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定