简体   繁体   English

如何在 Jquery 数据表中自定义搜索框过滤器

[英]how customize search box filter in Jquery datatables

I am customizing the search box filter in my datatable by adding some conditions.我通过添加一些条件来自定义我的数据表中的搜索框过滤器。

-If the user enters more than 3 characters and hit Enter it will execute the search with that value. - 如果用户输入超过 3 个字符并按 Enter,它将使用该值执行搜索。

-Then, If the user clears the search box input and doesn't type any characters in 4 seconds it will execute search with empty value. - 然后,如果用户清除搜索框输入并且在 4 秒内没有输入任何字符,它将执行空值搜索。

All works fine but still missing one condition: some users start typing sth in the search box then they clear it because they cancel that search... therefore the search with empty value is executed and the result would be the same as the current view.一切正常,但仍然缺少一个条件:一些用户开始在搜索框中输入 sth,然后他们将其清除,因为他们取消了该搜索......因此执行空值搜索,结果将与当前视图相同。

some complain about it as the result contains very large data and take some minutes to run the query on server and they have to wait each time.有些人抱怨它,因为结果包含非常大的数据,需要几分钟才能在服务器上运行查询,而且他们每次都必须等待。

I want to amend the second condition: If all records are displayed in the table even if you type sth then clear the search box it will not draw it again as the result would be the same.我想修改第二个条件:如果所有记录都显示在表格中,即使你输入 sth 然后清除搜索框它不会再次绘制它,因为结果是一样的。

I had an idea to check liveTable.page.info();我有一个想法来检查liveTable.page.info(); then compare recordsDisplay with recordsTotal if it's equal it means the table is displaying the whole data.. but I noticed that recordsDisplay still save the previous draw value so it wasn't helpful for me.然后将recordsDisplayrecordsTotal进行比较,如果相等,则表示该表正在显示整个数据。但我注意到recordsDisplay仍然保存以前的绘制值,因此对我没有帮助。

Any suggestions please?请问有什么建议吗? Thank you very much.非常感谢。

 $(document).ready(function() { var liveTable = $('#example').DataTable({ }); var searchDelay = null; $(".dataTables_filter input").unbind().bind("keyup", function(e) { if(this.value.trim().length >= 3 && e.keyCode == 13) { liveTable.search(this.value.trim()).draw(); } if(this.value.trim() == "" && (e.key === "Backspace" || e.key === "Delete")) { clearTimeout(searchDelay); searchDelay = setTimeout(function() { if ($('div.dataTables_filter input').val().trim() == "") { //before this draw i need to add my new condition if table is currently showing all records then no need to draw again liveTable.search("").draw(); } }, 4000); } return; }); });
 <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" /> <script src="https://code.jquery.com/jquery-3.5.1.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> <table id="example" class="table table-bordered" style="width:100%"> <thead> <tr> <th>data</th> </tr> </thead> <tbody> <tr> <td>1</td> </tr> <tr> <td>2</td> </tr> <tr> <td>3</td> </tr> </tbody> </table>

so i found a solution but i believe there is a cleaner way to do it.所以我找到了一个解决方案,但我相信有一种更清洁的方法可以做到这一点。

    if($('#liveTable_info').text().includes("filtered from")) { 

    liveTable.search("").draw(); }

when the table is showing all records so you have sth like this Showing 1 to xxx of xxx entries and when the table is filtered then the page info shows Showing 1 to xxx of xxx entries (filtered from xxx total entries)当表格显示所有记录,所以你有这样的东西Showing 1 to xxx of xxx entries并且当表格被过滤时,页面信息显示Showing 1 to xxx of xxx entries (filtered from xxx total entries)

so i checked the displayed info text if it contains the word filtered which means that my table is not showing all records then i have to draw it again.所以我检查了显示的信息文本是否包含filtered的单词,这意味着我的表没有显示所有记录,那么我必须再次绘制它。

I hope this will be helpful for others我希望这对其他人有帮助

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

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