检查 kendo 网格列中的日期是否为今天的日期

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

我在列中显示日期,如果是今天的日期,应该应用 css class,如果不是,则只显示日期。我试过这样解决:

  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') #}#",

但是我得到一个错误:"Date is not a constructor",有人知道如何解决吗?谢谢

您可以为遍历网格行的 dataBound 事件创建一个函数,并检查该特定字段。

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");
        }
    }
}

您可能需要使日期格式匹配,但这是另一个问题(不难解决)

编辑

我已经为您解决了日期格式问题,这种方式确实有效,您可以在此 fiddle

中确认