How to make DataTables global search include data- attributes in tags?

How to make DataTables global search include data- attributes in tags?

userCLuserCL Posts: 1Questions: 1Answers: 0

I have two data lists in the <tr> tag as follows:
<tr data-tickets="{{ doc[6] }}" data-documentos="{{ doc[5] }}">
An example would be: "1,2,3,4,5,6,577" for data-*.

I need the global search of DataTables to also search within these values and display the row that contains a <tr> with that value (for example, 577). However, the search is only working on the table columns, and not on the data-* attributes.

Is it possible to modify the DataTables search so it can search within the <tr>, inside the data-* attributes?

Answers

  • kthorngrenkthorngren Posts: 22,032Questions: 26Answers: 5,082

    Datatables can use HTMl 5 data attributes for searching when they are applied to the column cells. See the docs for details. You could apply the attributes to one of the columns or create a new hidden column with the attributes.

    Can you populate doc[6] and doc[5] into table columns? If so you can use columns.visible to hide the columns and the gloabl search will still search them. This might be the best option.

    Another option might be to use search.fixed() as a function. The third parameter is the row index. You can use that index with row().node() to get the tr then extract the two data attributes. This might not be as efficient as the other two since the search data is built on the fly each search. The first to options the search data is calculated before searching at initialization unless there are data changes.

    Kevin

Sign In or Register to comment.