使用 DataTables,如何指定 <td> 中的元素待查 [英] Using DataTables, how to specify an element inside a &lt;td&gt; to be searched

查看:28
本文介绍了使用 DataTables,如何指定 <td> 中的元素待查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jquery DataTables,我有一个包含单元格的表格,每个 <td> 包含一个 和一个隐藏的 <select>,现在我想要的只是过滤 内的文本,而不是 的全部内容还包含隐藏的 元素被css隐藏

The code is generated on server, but the resulting table is something like this: Please notice that: <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 代码定位第一列和第二列,根据您的需要进行调整.

You can use the code below to only search <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 属性来指定用于过滤的值,然后不需要额外的初始化代码.请参阅下面的示例:

Alternatively, you can use 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>

请参阅手册example 有关 data- 属性的更多信息.

See manual or example for more information on data- attributes.

这篇关于使用 DataTables,如何指定 <td> 中的元素待查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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