基于单元格值的jQuery数据表行颜色更改
我想根据单元格值更改行背景颜色和文本颜色。我的基于单元格值的jQuery数据表行颜色更改,jquery,datatables,Jquery,Datatables,我想根据单元格值更改行背景颜色和文本颜色。我的 我的Html代码如下 <button id="refersh">Refresh</button> <button id="AddRow">Add New Row</button> <table id="stdTable" class="table table-bordered table-striped" width="100%"> <thead> &l
我的Html代码如下
<button id="refersh">Refresh</button>
<button id="AddRow">Add New Row</button>
<table id="stdTable" class="table table-bordered table-striped" width="100%">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
<th>Date of birth</th>
<th>Edit/View</th>
</tr>
</thead>
</table>
刷新
添加新行
身份证件
名称
年龄
出生日期
编辑/查看
数据表版本为V1.10.12。
数据加载方法是ajax。如下更改脚本。请查看“fnRowCallback”部分
var数据集=[
[
Id=“001”,
Name=“nn”,
Age=“Age”,
出生日期=“125”
],
[
Id=“001”,
Name=“nn”,
Age=“Age”,
出生日期=“125”
]
];
$('#stdTable')。数据表({
处理:对,
服务器端:错,
顺序:对,
是的,
搜索:是的,
栏目:[
{标题:“Id”},
{标题:“姓名”},
{标题:“年龄”},
{标题:“出生日期”},
{标题:“查看数据”}
],
栏目:[
{数据:“Id”},
{数据:“名称”},
{数据:“年龄”},
{数据:“出生日期”},
{
数据:空,
defaultContent:“查看IdDelete”
}
],
数据:数据集,
“columnDefs”:[
{
“目标”:0,
“可见”:错误
}
],
“fnRowCallback”:函数(nRow、aData、iDisplayIndex、iDisplayIndexFull){
如果(数据年龄=“20”){
//单元格背景色
$(nRow).find('td:eq(1)').css('background-color','#ffc2c2');
}
否则如果(aData.Age==“10”){
//行背景色
$('td',nRow).css('background-color','Orange');
}
否则如果(aData.Age==“25”){
//单元格文本颜色
$('td',nRow).css('color','red');
}
}
});
});
使用此createdRow函数:
"createdRow": function( row, data, dataIndex ) {
if ( data["LAYOUT"] == "N" ) {
$( row ).css( "background-color", "Orange" );
$( row ).addClass( "highlight" );
}
}
假设只使用css—请参阅:(还包括一个js解决方案作为答案,但可能只有jquery有更好的副本)
"createdRow": function( row, data, dataIndex ) {
if ( data["LAYOUT"] == "N" ) {
$( row ).css( "background-color", "Orange" );
$( row ).addClass( "highlight" );
}
}