gpt4 book ai didi

javascript - 如何仅搜索 jquery 数据表中的文本?

转载 作者:行者123 更新时间:2023-12-02 18:41:45 35 4
gpt4 key购买 nike

我按照这个例子:http://www.datatables.net/development/filtering

但是它不起作用。如果您输入“a”作为搜索键盘,所有行仍显示在表中,因为它搜索 html 标记 <a> 。我错过了什么?

<!doctype html>
<html>
<head>
<link rel="stylesheet" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>

<script>
$.fn.dataTableExt.ofnSearch['string'] = function ( sData )
{
return sData.replace(/\n/g," ").replace( /<.*?>/g, "" );
};
$(document).ready
(
function()
{
$('#search').dataTable
(
{
aoColumns:[
{'sType': 'string'}
]
}
);
}
);
</script>


</head>
<body>

<table id="search">
<thead>
<tr>
<th>search</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="www.google.com">b</a></td>
</tr>
<tr>
<td><a href="www.google.com">c</a></td>
</tr>
<tr>
<td><a href="www.google.com">d</a></td>
</tr>
<tr>
<td><a href="www.google.com">e</a></td>
</tr>
<tr>
<td><a href="www.google.com">f</a></td>
</tr>
<tr>
<td><a href="www.google.com">g</a></td>
</tr>
</tbody>
</table>
</body>
</html>

最佳答案

从源代码中得到这个

* Note that as of v1.9, it is typically preferable to use <i>mData</i> to prepare data for
* the different uses that DataTables can put the data to. Specifically <i>mData</i> when
* used as a function will give you a 'type' (sorting, filtering etc) that you can use to
* prepare the data as required for the different types. As such, this method is deprecated.
* @type object
* @default {}
* @deprecated
*
* @example
* $.fn.dataTableExt.ofnSearch['title-numeric'] = function ( sData ) {
* return sData.replace(/\n/g," ").replace( /<.*?>/g, "" );
* }
*/
"ofnSearch": {},

所以我想出了这个

var filterFunc = function ( sData )
{
return sData.replace(/\n/g," ").replace( /<.*?>/g, "" );
};
$(document).ready
(

function()
{
$('#search').dataTable
(
{
aoColumns:[
{
'mData': function(source, type, val){
if (type === 'set') {
source.value = val;
source.value_display = val;
source.value_filter = val=="" ? "" : filterFunc(val);
return;
}
else if (type === 'display') {
return source.value_display;
}
else if (type === 'filter') {
return source.value_filter;
}
// 'sort', 'type' and undefined all just use the integer
return source.value;
}

}
]
}
);
}
);

这是 fiddle http://jsfiddle.net/pJG9f/1/

关于javascript - 如何仅搜索 jquery 数据表中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16783202/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com