使用 DataTables,如何指定 <td> 中的元素待查 [英] Using DataTables, how to specify an element inside a <td> to be searched
问题描述
我正在使用 jquery DataTables,我有一个包含单元格的表格,每个 I'm using jquery DataTables, and I have a table with cells, each 我正在使用基本的 DataTables 配置: I'm using basic DataTables configuration: 我已经在这个网站、Datatables 网站、谷歌搜索上尝试了几天,但找不到答案,所以请帮忙提前谢谢 I've been trying for a couple of days now on this site, Datatables site , googling, but couldn't find an answer, so please help Thanks in advance 代码是在服务器上生成的,但是结果表是这样的:请注意: The code is generated on server, but the resulting table is something like this:
Please notice that: 您可以使用以下代码仅在特定列的单元格内搜索 You can use the code below to only search 或者,您可以使用 Alternatively, you can use 请参阅手册或example 有关 See manual or example for more information on 这篇关于使用 DataTables,如何指定 <td> 中的元素待查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!<td>
包含一个 和一个隐藏的
<select>
,现在我想要的只是过滤 内的文本,而不是
的全部内容还包含隐藏的 元素.
<td>
contains a <span>
and a hidden <select>
, now all I want is to filter on just the text inside the <span>
not the whole content of the <td>
which also contains the hidden <select>
element.$(document).ready( function () {
$('#table_id').DataTable();
} );
元素被css隐藏
<select>
element is hidden with css<tr>
<td>
<span>Text</span>
<select>
<option>option1</option>
<option>option2</option>
....
</select>
</td>
<td>
<span>Text</span>
<select>
<option>option1</option>
<option>option2</option>
....
</select>
</td>
</tr>
...
推荐答案
.请注意,我使用
"targets": [0, 1]
仅根据您的 HTML 代码定位第一列和第二列,根据您的需要进行调整.<span>
inside cells in specific columns. Please note that I've used "targets": [0, 1]
to target first and second column only based on your HTML code, adjust according to your needs.$('#table_id').DataTable({
"columnDefs": [{
"targets": [0, 1],
"render": function ( data, type, full, meta ) {
if(type === 'filter'){
return $('#table_id').DataTable().cell(meta.row, meta.col).nodes().to$().find('span').text();
} else {
return data;
}
}
}]
});
<td>
元素上的 data-search
属性来指定用于过滤的值,然后不需要额外的初始化代码.请参阅下面的示例:data-search
attribute on <td>
element to specify value used for filtering, then no additional initialization code is required. See below for an example:<tr>
<td data-search="Text">
<span>Text</span>
<select>
<option>option1</option>
<option>option2</option>
....
</select>
</td>
<td data-search="Text">
<span>Text</span>
<select>
<option>option1</option>
<option>option2</option>
....
</select>
</td>
</tr>
data-
属性的更多信息.data-
attributes.
登录
关闭