根据日期范围过滤更改 jquery 数据表行的 css
我正在尝试根据日期范围更改 jquery 数据表的背景颜色...我尝试使用 $.fn.afnFiltering.push(...) 进行日期范围过滤...但无法弄清楚如何更改过滤行的颜色...
这是我用来进行过滤的代码...我测试了这个并且日期范围过滤有效...
//row filtering by date
$.fn.dataTableExt.afnFiltering.push(
function (oSettings, aData, iDataIndex) {
var iFini = document.getElementById('dateStart').value;
var iFfin = document.getElementById('dateEnd').value;
if (iFini == "" && iFfin == "") {
return true;
}
else if (iFini != "" && iFfin == "") {
return true;
}
else if (iFini == "" && iFfin != "") {
return true;
}
else if (iFini != "" && iFfin != "") {
var sdate = new Date();
var edate = new Date();
var initialdt = iFini.split("/");
var enddt = iFfin.split("/");
var filterdt1 = aData[3].split(" ");
var filterdt = filterdt1[0].split("/");
var msg = "";
if ((Number.parseInvariant(filterdt[0]) + 1 >= Number.parseInvariant(initialdt[0]) + 1 && Number.parseInvariant(filterdt[0]) + 1 <= Number.parseInvariant(enddt[0]) + 1) && (Number.parseInvariant(filterdt[1]) >= Number.parseInvariant(initialdt[1]) && Number.parseInvariant(filterdt[1]) <= Number.parseInvariant(enddt[1])) && (Number.parseInvariant(filterdt[2]) >= Number.parseInvariant(initialdt[2]) && Number.parseInvariant(filterdt[2]) <= Number.parseInvariant(enddt[2]))) {
// msg = "initialdt: " + initialdt[0] + "/" + initialdt[1] + "/" + initialdt[2] + "====" + "enddt: " + enddt[0] + "/" + enddt[1] + "/" + enddt[2] + "====Filter:" + filterdt[0] + "/" + filterdt[1] + "/" + filterdt[2] + "/n";
//need to change the style here...not sure how to address
//the individual row
return true;
}
}
return true;
}
);
我还绑定了 fnDraw 函数,如下所示...
$("#dateStart").keyup(function () { $dTable.fnDraw(); });
$("#dateStart").change(function () { $dTable.fnDraw(); });
$("#dateEnd").keyup(function () { $dTable.fnDraw(); });
$("#dateEnd").change(function () { $dTable.fnDraw(); });
i am trying to change the background color of a jquery datatable based on a date range....i tried to use $.fn.afnFiltering.push(...) to do the date range filtering...but cant figure out how to change the color of the filtered rows....
heres the code i am using to do the filtering...i tested this and the date range filtering works...
//row filtering by date
$.fn.dataTableExt.afnFiltering.push(
function (oSettings, aData, iDataIndex) {
var iFini = document.getElementById('dateStart').value;
var iFfin = document.getElementById('dateEnd').value;
if (iFini == "" && iFfin == "") {
return true;
}
else if (iFini != "" && iFfin == "") {
return true;
}
else if (iFini == "" && iFfin != "") {
return true;
}
else if (iFini != "" && iFfin != "") {
var sdate = new Date();
var edate = new Date();
var initialdt = iFini.split("/");
var enddt = iFfin.split("/");
var filterdt1 = aData[3].split(" ");
var filterdt = filterdt1[0].split("/");
var msg = "";
if ((Number.parseInvariant(filterdt[0]) + 1 >= Number.parseInvariant(initialdt[0]) + 1 && Number.parseInvariant(filterdt[0]) + 1 <= Number.parseInvariant(enddt[0]) + 1) && (Number.parseInvariant(filterdt[1]) >= Number.parseInvariant(initialdt[1]) && Number.parseInvariant(filterdt[1]) <= Number.parseInvariant(enddt[1])) && (Number.parseInvariant(filterdt[2]) >= Number.parseInvariant(initialdt[2]) && Number.parseInvariant(filterdt[2]) <= Number.parseInvariant(enddt[2]))) {
// msg = "initialdt: " + initialdt[0] + "/" + initialdt[1] + "/" + initialdt[2] + "====" + "enddt: " + enddt[0] + "/" + enddt[1] + "/" + enddt[2] + "====Filter:" + filterdt[0] + "/" + filterdt[1] + "/" + filterdt[2] + "/n";
//need to change the style here...not sure how to address
//the individual row
return true;
}
}
return true;
}
);
i am also binding the fnDraw function as below...
$("#dateStart").keyup(function () { $dTable.fnDraw(); });
$("#dateStart").change(function () { $dTable.fnDraw(); });
$("#dateEnd").keyup(function () { $dTable.fnDraw(); });
$("#dateEnd").change(function () { $dTable.fnDraw(); });
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 iDataIndex 和 fnGetNodes 获取 tr 元素。
<代码>
var tr = oTable.fnGetNodes(iDataIndex);
$(tr).css('背景颜色', '#F00');
oTable 必须包含 dataTable 对象。
<代码>
var oTable = $("#myTable").dataTable({ ... });
You can get the tr element using iDataIndex and fnGetNodes.
var tr = oTable.fnGetNodes(iDataIndex);
$(tr).css('background-color', '#F00');
oTable must contain the dataTable object.
var oTable = $("#myTable").dataTable({ ... });