jdl*_*lam 3 javascript css conditional-formatting colors pivottable.js
所以我正在使用 PivotTable.js,它对工作有很大帮助。
不过现在,我正在尝试让过滤器根据值更改单元格或单元格内字体的颜色。
例如,如果我的数据集中有一组日期
dates = ["N/A", "4/12/2016", "7/9/2024", "7/9/2024", "4/1/2013"]
Run Code Online (Sandbox Code Playgroud)
我想在 2016 年 6 月 1 日之前的任何日期更改颜色。
如果这有什么不同,我将我的数据作为变量“数据”在本地传递
$(function(){
var derivers = $.pivotUtilities.derivers;
var renderes = $.extend($.pivoUtilities.renderers, $.pivotUtilities.export_renderers);
$("#pivot_table").pivotUI(data, {
derivedAttributes: function(data){
// not sure how to access the css of the element from here
}
rows: [],
cols: ["Name", "Date", "Organization", "Cost"],
renderers: renderers,
rendererName: "Table"
});
});
Run Code Online (Sandbox Code Playgroud)
我试过进入衍生属性,但我尝试的一切都不起作用。
任何帮助或头脑风暴将不胜感激
所以……其实是我自己解决的哈哈……
PivotTable.js 的一大优点是选项和排序的灵活性。所以我使用了 onRefresh 属性并将这个函数提供给它
onRefresh: function() {
var $labels = $('.pvtRowLabel')
var today = new Date();
var d = today.getDate();
var m = today.getMonth()+1;
var y = today.getFullYear();
var date;
var dateReg = /^\d{1,2}[\/]\d{1,2}[\/]\d{4}$/;
// Goes through each cell with the class '.pvtRowLabel'
for (var i=0; i<$labels.length; i++) {
if ($labels[i].innerHTML.match(dateReg)) {
date = $labels[i].innerHTML.split('/');
if (Number(date[2]) == y) {
if (Number(date[0]) == m) {
if (Number(date[1]) <= d) {
$('.pvtRowLabel').eq(i).addClass('expired');
}
} else if (Number(date[0]) < m) {
$('.pvtRowLabel').eq(i).addClass('expired');
}
} else if (Number(date[2]) < y) {
$('.pvtRowLabel').eq(i).addClass('expired');
}
}
};
},
Run Code Online (Sandbox Code Playgroud)
之后,只需使用 css 选择器指定您要使用的颜色
.expired { background-color: #F08080 !important; }
Run Code Online (Sandbox Code Playgroud)
我的解决方案的问题在于它增加了浏览器的压力,因为它在每次刷新表时检查 DOM 并添加类。我不确定在首次渲染时是否有办法完成此操作,因此这些单元格在生成时总是会被标记为已过期。