从数据表中搜索 [英] Searching from DataTable

查看:86
本文介绍了从数据表中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://datatables.net/reference/api/search()#Example

我使用上面的链接作为搜索示例,但是我的设置却大不相同.

Im using the link above as an example for my searching, but my setup is quite different.

<td>
    <div class="plCell_desktop">
        <input type="radio" class="" data-lnk_id="414107671" data-group="RUTH">
        <label for="414107671">RUTH</label>
    </div>
</td>

这是我桌子上的摘录.

数据的唯一可见位是"Ruth".
但是当我搜索说"76"时,结果还是会带回"Ruth".
原因很可能是这样的事实,我在表格单元格中有比"Ruth"更多的信息.

The only visable bit of data is "Ruth".
but when I search for say '76' it will still bring "Ruth" back as a result.
The reason is most likely the fact that I have a lot more info in the table cell than "Ruth".

好的,我的问题是.您可以强迫DataTables从单词的开头开始搜索吗?例如(输入"uth"不会带回"Ruth",但是"Ru"会,希望是有道理的).

Okay, so my question is. Can you force DataTables to search from the beginning of a word. e.g. ( enter "uth" will not bring back "Ruth", but "Ru" will, hope it makes sense ).

您可以使用DataTables进行一种"innerHTML.val()"搜索吗?

Can you do a kind of "innerHTML.val()" search with DataTables?

推荐答案

好的,我的问题是.您可以强迫DataTables从 一个词的开头.例如(输入"uth"不会带回"Ruth", 但"Ru"会说得通的).

Okay, so my question is. Can you force DataTables to search from the beginning of a word. e.g. ( enter "uth" will not bring back "Ruth", but "Ru" will, hope it makes sense ).

是的.创建一个自定义过滤器,该过滤器可以像这样执行过滤.默认的智能搜索"将被自定义过滤器否决,以后的任何过滤都将通过该过滤器:

Yes. Create a custom filter upfront that perform filtering like this. The default "smart search" will be overruled by the custom filter and any future filtering will go through that :

$.fn.dataTable.ext.search.push(function( settings, data, dataIndex ) {
   var term = $('.dataTables_filter input').val().toLowerCase()
   for (var i=0, l=data.length; i<l; i++) {
     if (data[i].toLowerCase().indexOf(term) == 0 ) return true
   }
   return false
})

演示-> http://jsfiddle.net/qxcjzuxa/

demo -> http://jsfiddle.net/qxcjzuxa/

您可能会注意到,使dataTables过滤总体上区分大小写非常容易(仅举一个例子).过滤器阵列是一种LIFO结构,您可以在其中相互叠置多个过滤器.如果出于某种原因将动态禁用单词开头"过滤,则只需通过$.fn.dataTable.ext.search.pop()删除过滤器.

As you may notice it would be very easy to make the dataTables filtering overall case sensitive (just an example). The filter array is a LIFO-structure where you can have multiple filters on top of each other. You remove a filter simply by $.fn.dataTable.ext.search.pop() if you for any reason will disable "beginning of word" filtering dynamically.

这篇关于从数据表中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆