Aviator
Aviator

Reputation: 613

check if date is today`s date in kendo grid column

I am doing displaying dates in a column, if it is today`s date, there should be css class applied, if not, just date should be displayed. I tried to solve it this way:

  template: "#if(Date == new Date()) {#<div class='todayClass'>#= kendo.toString(kendo.parseDate(Date, 'yyyy-MM-dd'), 'dd-MM-yyyy') #</div>#} else{#= kendo.toString(kendo.parseDate(Date, 'yyyy-MM-dd'), 'dd-MM-yyyy') #}#",

but I get an error: "Date is not a constructor", does anyone know how to solve it? Thanks

Upvotes: 2

Views: 1680

Answers (1)

chiapa
chiapa

Reputation: 4412

You can create a function for the dataBound event that iterates through the grid's rows and check for that specific field.

function checkDates() {
    var currentDate = new Date();
    currentDate = currentDate.setHours(0, 0, 0, 0); // eliminate the time from the date
    dataView = this.dataSource.view();
    for (var i = 0; i < dataView.length; i++) {
        // check if the fields match and apply a class to the row if so
        var mydate = dataView[i].Date.setHours(0, 0, 0, 0); // eliminate the time from the date
        if (mydate == currentDate) { // compare dates
            var uid = dataView[i].uid;
            $("#grid tbody").find("tr[data-uid=" + uid + "]").addClass("yourClass");
        }
    }
}

You may need to make the date formats match, but that another issue (not hard to solve)

EDIT

I have addressed the date formatting for you, this way it certainly works, as you can confirm in this fiddle

Upvotes: 3

Related Questions