Filter by td attribute added via columnDef

Filter by td attribute added via columnDef

loeffelloeffel Posts: 3Questions: 1Answers: 0

I am using something like this https://jsfiddle.net/an5kbx0z/ to filter my entries by a custom data attribute. Using this with the data attribute added directly in my template works flawlessly. However after changing DataTables to ajax mode and adding the data attribute via columnDefs -> createdCell the filter is no longer working.

How can I filter by data attributes added via columnDefs?

This is the columnDefs section of my script:

columnDefs: [
{
targets: [1],
createdCell: function (td, cellData, rowData, row, col) {
if(cellData === 'Info') {
$(td).attr('data-doctype', 'infoItem')
}
if(cellData === Document') {
$(td).attr('data-doctype', docItem')
}
}
}
]

Answers

  • kthorngrenkthorngren Posts: 21,166Questions: 26Answers: 4,921

    Not sure I understand the question. Your example is filtering by the data in the column not the HTML5 attribute. This doc discusses how to use the HTML5 attribute for filtering and what to name them. My understanding is the HTML5 attributes work only with DOM sourced tables as Dataatables looks at that info on initialization to build the searching and sorting information. Please see this thread for more info:
    https://datatables.net/forums/discussion/comment/145323/#Comment_145323

    Kevin

  • loeffelloeffel Posts: 3Questions: 1Answers: 0

    You are right about the fiddle (it is not mine, I never tested if it was actually filtering by data attribute or column value).

    The limitation to DOM sourced tables would be an explanation although I don't understand why filtering doesn't work while searching does work just fine in my ajax tables.

  • kthorngrenkthorngren Posts: 21,166Questions: 26Answers: 4,921

    I don't understand why filtering doesn't work while searching does work just fine in my ajax tables.

    Are you referring to the filter() API versus the search() API? If so then this snippet from the filter() API docs applies:

    This method should not be confused with search() which is used to search for records in the DataTable - i.e. the filter method does not change the rows that are displayed in the DataTable.

    If not then please provide more specifics of what you are doing. A link to your page or a test case would be best so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • loeffelloeffel Posts: 3Questions: 1Answers: 0
    edited January 2020

    I had the column set to searchable: false facepalm

This discussion has been closed.