我正在尝试将服务器数据拉入Jade,然后使用数据运行函数以确定要自动选择的类。这用于表格以自动着色单元格,具体取决于单元格的值。
通过(日期差异)运行返回数据的函数:
function inDays(date1,date2) {
var date1=date1.split('/');
var date2=date2.split('/');
var d1=new Date(date1[1]+'/'+date1[0]+'/'+date1[2]);
console.log(d1);
var d2=new Date(date2[1]+'/'+date2[0]+'/'+date2[2]);
console.log(d2);
var t2=d2.getTime();
var t1=d1.getTime();
var days=parseInt((t2-t1)/(24*3600*1000));
return days
}
期望的用法:
td(class!='<%- #{inDays(<%= dateCompareAgainst %>, <%= date %>)} < 5 ? "green" : "orange"') <%= date %>
然而,这不起作用。
当我提出以下内容时:
td(class='#{inDays(<%= dateCompareAgainst %>, <%= date %>)}') <%= date %>
它按预期显示了类中的两个日期。我对如何正确地做到这一点感到茫然,任何帮助都会受到赞赏。
答案 0 :(得分:0)
我已经设法通过将它放在JS文件本身来解决这个问题。我发现它有点hacky但它有效。我使用骨干利用骨干的模板和渲染功能。我为每个单元格类型添加了类,以便可以使用以下内容。
这是我根据两个单元格的日期差异自动更改颜色的方法:
render: function() {
return this.$el.html(this.template(this.model.attributes)),
this.$el.find(".initialsent").each(function(a, b) {
if (b.innerText) {
var c = b.innerText,
d = app.formatDate(c),
e = $(this).closest("tr").children("td.opened").text(),
f = app.formatDate(e);
$(this).closest("tr").children("td.initialsent").addClass(app.inDays(f, d) < 3 ? "green" : app.inDays(f, d) < 5 ? "orange" : "red")
}
else {
var g = new Date,
e = $(this).closest("tr").children("td.opened").text(),
f = app.formatDate(e);
$(this).closest("tr").children("td.initialsent").addClass(app.inDays(f, g) < 3 ? "green" : app.inDays(f, g) < 5 ? "orange" : "red")
}
}),
this
}