Friday, August 10, 2018

Custom JavaScript Date Sorter Function For Bootstrap Table in ASP.NET MVC

While working on a project that implements Bootstrap Table by Wenzhixin, one column returns a date value with the format of MMM-YYYY and this column is sortable. A solution I've come up is to use the flexibility of Moment.js. Here's to date sorter function for the column.
function DateSorter(a, b) {
    var dateA = new Date(moment(a).format('MMM-YYYY'));
    var dateB = new Date(moment(b).format('MMM-YYYY'));
    if (dateA < dateB)
        return -1;
    if (dateA > dateB)
        return 1;
    return -0;
Then set the data-sorter property of the table column with the function name.
<th data-field="BillingMonth" data-sortable="true" data-sorter="DateSorter">Billing Month</th>


Post a Comment