I am using JQuery Tablesorter 2.7.* for a JavaScript table I have. I am also using the Tablesorter Filter widget, in order to have several of my columns have drop-down filtering options. I have a new challenge: I am required to add columns to the table dynamically. I have seen this question: Adding columns dynamically to a table managed with jQuery's tablesorter – which recommends that I remove and recreate the table with each addition/removal of a column. This is fair enough, however this causes a conflict with the Filter Widget.
In order to apply the Filter functions, the JavaScript references the column by index only, as so:
widgetOptions: {
filter_reset: '.reset',
filter_functions: {
2: true,
3: true,
}
}
This code would cause columns with index 2 and 3 to have the default select Filter. The problem occurs when new columns are added dynamically – the index values of these columns are likely to change.
This brings my question; is there any way to apply widget options to columns named specifically by name? If not, is there some solution I can implement that would achieve the functionality where I can add/remove columns dynamically, without disturbing the filter functions?
Edit: I am also using filter functions.
Update #2: As of tablesorter v2.17.0 a class name* or ID can be used to target a column within the
filter_functionsoption:* Note: the class name can not include a selector that uses any kind of indexing, e.g.
"th:eq()",":gt()",":lt()",":first",":last",":even"or":odd",":first-child",":last-child",":nth-child()",":nth-last-child()", etc.In the docs, it shows an alternative to using the
filter_functionsoption:So, just add a
filter-selectclass name to the column instead.Update: Since other filter functions are being used, you can define these functions outside of the initialization code (demo)