Using DataTables, how to specify an element inside a <td> to be searched

微笑、不失礼 提交于 2019-12-02 02:00:43

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;
         }
      }
   }]
});

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>

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!