[英]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:编辑:为了确保排序按预期工作,我执行了以下操作:
true
parameter of the clone method to prevent event handlers of Datatable being clonedtrue
参数以防止克隆 Datatable 的事件处理程序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.