根据内容更改jquery数据表的单元格背景
我是数据表新手 - http://datatables.net/ - 。我很难找到一个示例,如何根据单元格的位置和内容更改单元格的背景颜色。
像这样的东西对我有用,除了所选行的突出显示现在是 背景颜色已更改的单元格中的“颜色过度”。如果我可以在 fnRowCallback 中获取行的类名,那么我就可以处理它。
$(document).ready(function() {
// Add a click handler to the rows - this could be used as a callback
$("#example tbody").click(function(event) {
$(oTable.fnSettings().aoData).each(function() {
$(this.nTr).removeClass('row_selected');
});
(event.target.parentNode).addClass('row_selected');
});
oTable = $('#example').dataTable({
"fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
$(nRow).children().each(function(index, td) {
if (index == 6) {
if ($(td).html() === "pending") {
$(td).css("background-color", "#078DC6");
} else if ($(td).html() === "rendering") {
$(td).css("background-color", "#FFDE00");
} else if ($(td).html() === "success") {
$(td).css("background-color", "#06B33A");
} else if ($(td).html() === "failure") {
$(td).css("background-color", "#FF3229");
} else {
$(td).css("background-color", "#FF3229");
}
}
});
return nRow;
},
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "scripts/server_processing.php",
"sPaginationType": "full_numbers",
});
});
I am new to datatables - http://datatables.net/ - . I am in trouble to find an example how I could change the background color of a cell based on its position and content.
Something like this worked for me except that the highlighting of the selected row is now
'overcolored' in the cells which have changed background color. If I could get the class name of the row in the fnRowCallback then I could handle it.
$(document).ready(function() {
// Add a click handler to the rows - this could be used as a callback
$("#example tbody").click(function(event) {
$(oTable.fnSettings().aoData).each(function() {
$(this.nTr).removeClass('row_selected');
});
(event.target.parentNode).addClass('row_selected');
});
oTable = $('#example').dataTable({
"fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
$(nRow).children().each(function(index, td) {
if (index == 6) {
if ($(td).html() === "pending") {
$(td).css("background-color", "#078DC6");
} else if ($(td).html() === "rendering") {
$(td).css("background-color", "#FFDE00");
} else if ($(td).html() === "success") {
$(td).css("background-color", "#06B33A");
} else if ($(td).html() === "failure") {
$(td).css("background-color", "#FF3229");
} else {
$(td).css("background-color", "#FF3229");
}
}
});
return nRow;
},
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "scripts/server_processing.php",
"sPaginationType": "full_numbers",
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我知道这个问题已经得到解答,但我想我会分享如何能够通过以下方式将各种“条件格式”应用到各种单元格:
首先,我在数据表初始化期间向每列添加了一个类:
然后,我创建了一个每次重绘都会调用的 addFormat() 函数(我必须这样做,因为我有一个实时更新的实时表):
在 addFormat 函数中,我基本上通过 CSS 类定义了所有格式规则。我使用 jQuery :contains 扩展 来应用正则表达式等非常具体的规则。这些规则的选择器将是 td 以及我在数据表初始化期间分配给列的任何类:
这对我来说非常有效。
I know the question was already answered, but I thought I would share how I was able to apply "conditional formatting" of sorts to various cells, in the following way:
First, I added a class to each column during my datatables initialization:
Then, I created an addFormat() function that was called each redraw (I had to do it this way because I have a live table that updates in realtime):
In the addFormat function, I essentially defined all of my formatting rules, via CSS classes. I used a jQuery :contains extension in order to apply very specific rules with regular expressions and such. My selectors for these rules would be
td
and whatever class I had assigned to a column during datatables initialization:That worked very well for me.
您可以根据单元格值动态更改单元格的背景颜色
You can change background color of cell dynamically respective of cell value by
我还没有测试过这个,但是这样的东西应该会给你一个想法:
I haven't tested this but something like this should give you an idea:
以下是如何使用 DataTables 1.10+ 语法通过
createdCell
有条件地设置单元格样式。Here's how to conditionally style the cells via
createdCell
, using DataTables 1.10+ syntax.这对我有用。 data[3] 表示我们正在查看的行的第 4 列(请记住,javascript 中的数组是从零开始的,即它们从零开始编号)。但是当我们想要更改单元格背景时,我们使用 .eq(3) 语法。
This worked for me. data[3] means column 4 of the row we're looking at (remember, arrays in javascript are zero-based, that is they start numbering at zero). But when we want to change the cell background, we use the .eq(3) syntax.