Filter by td attribute added via columnDef
Filter by td attribute added via columnDef
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
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
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.
Are you referring to the
filter()
API versus thesearch()
API? If so then this snippet from thefilter()
API docs applies: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
I had the column set to searchable: false facepalm