I've built a column filter that filters a column to find rows dated "yesterday", "today", or "tomorrow", based on a select element. Having done that, I now see that it would also be useful to be able to filter for dates older than yesterday and newer than tomorrow. However, I don't see any simple way to extend what I've built to accommodate an open-ended date range as that would require.
I have the following code to define the select options:
// Set up comparison dates for filtering the dueDate column
var today = new Date();
var yesterday = moment(today).add(-1,'days');
yesterday = yesterday._d.toISOString().substring(0,10);
var tomorrow = moment(today).add(1,'days');
tomorrow = tomorrow._d.toISOString().substring(0,10);
today = today.toISOString().substring(0,10);
And the following code implements the column filter (for columns with className="dFilter"):
// Populate the dueDate column filter
this.api().columns('.dFilter').every( function () {
var column = this;
var that = this;
var select = $('<select><option value="">All</option>
<option value="'+yesterday+'">Yesterday</option>
<option value="'+today+'">Today</option>
<option value="'+tomorrow+'">Tomorrow</option></select>')
.appendTo( $(column.header()) )
.on( 'change', function() {
that
.search ($(this).val() )
.draw();
} );
column
.cache( 'search' )
.sort()
.unique()
.each( function() {
} );
} );
This works great for what it does, and is quite similar to other column filters I use, but it looks like a dead-end with regard to being extensible in the way I want. Is there a way to extend this scheme, or do I need to abandon this and take a completely different approach?
Thanks,
Tom