简体   繁体   English

jQuery 数据表列搜索

[英]jQuery Datatable column search

I am using jQuery Datatable and I am using it column search feature.我正在使用 jQuery 数据表,并且正在使用它的列搜索功能。

I have added it successfully in the header but it's coming after thead titles I need it before thead titles while by using table eq(0) I am able to place it before titles but sorting is coming with it.我已经在头添加成功,但它的到来之后thead冠军之前我需要它thead冠军,同时通过使用表eq(0)我能头衔之前将其排序,但与它的到来。

 $('#myApprovalTable thead tr').clone(true).appendTo('#myApprovalTable thead'); $('#myApprovalTable thead tr:eq(1) th').each(function(i) { var title = $(this).text(); //$(this).html('<input type="text" placeholder="Search" />'); if (title == 'Actions') { $(this).hide(); } $(this).html('<input type="text" placeholder="' + title + '" />'); $('input', this).on('keyup change', function() { if (myApproval.column(i).search() !== this.value) { myApproval .column(i) .search(this.value) .draw(); } }); }); var myApproval = $('#myApprovalTable').DataTable({ orderCellsTop: true, dom: 'Bfrtip', buttons: [{ text: 'Reset Filter', action: function(e, dt, node, config) { $('#myApprovalTable input').val('').change(); } }], language: { search: "_INPUT_", searchPlaceholder: "Search..." } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/js/jquery.dataTables.min.js"></script> <table id="myApprovalTable" class="display table-responsive" style="width:100%"> <thead> <tr> <th>title1</th> <th>title2</th> <th>title3</th> <th>title4</th> <th>title5</th> <th>title6</th> <th>title7</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> </tr> <tr> <td>2</td> <td>2</td> <td>2</td> <td>2</td> <td>2</td> <td>2</td> <td>2</td> </tr> </tbody> </table>

Changing your jQuery selector so it selects the first row did the trick.更改您的 jQuery 选择器使其选择第一行就可以了。

$('#myApprovalTable thead tr:eq(1) th')

to

$('#myApprovalTable thead tr:eq(0) th')

EDIT: To make sure sorting works as intended I did the following:编辑:为了确保排序按预期工作,我执行了以下操作:

  • I switched the order of your code.我改变了你的代码顺序。 First the Datatable is initialized before adding the search logic.首先在添加搜索逻辑之前初始化数据表。
  • Also I removed the true parameter of the clone method to prevent event handlers of Datatable being cloned我还删除了 clone 方法的true参数以防止克隆 Datatable 的事件处理程序
  • Last I prepended the cloned elements with prependTo instead of appendTo() so the cloned table heading is being changed instead of the original one.最后,我使用prependTo而不是appendTo()将克隆的元素prependTo前面,因此克隆的表标题被更改而不是原始的。

 var myApproval = $('#myApprovalTable').DataTable({ orderCellsTop: true, dom: 'Bfrtip', buttons: [{ text: 'Reset Filter', action: function(e, dt, node, config) { $('#myApprovalTable input').val('').change(); } }], language: { search: "_INPUT_", searchPlaceholder: "Search..." } }); $('#myApprovalTable thead tr').clone().prependTo('#myApprovalTable thead'); $('#myApprovalTable thead tr:eq(0) th').each(function(i) { var title = $(this).text(); //$(this).html('<input type="text" placeholder="Search" />'); if (title == 'Actions') { $(this).hide(); } $(this).html('<input type="text" placeholder="' + title + '" />'); $('input', this).on('keyup change', function() { if (myApproval.column(i).search() !== this.value) { myApproval .column(i) .search(this.value) .draw(); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/js/jquery.dataTables.min.js"></script> <table id="myApprovalTable" class="display table-responsive" style="width:100%"> <thead> <tr> <th>title1</th> <th>title2</th> <th>title3</th> <th>title4</th> <th>title5</th> <th>title6</th> <th>title7</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> </tr> <tr> <td>2</td> <td>2</td> <td>2</td> <td>2</td> <td>2</td> <td>2</td> <td>2</td> </tr> </tbody> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM