jquery datatables-按列名获取td
我想将一个类添加到特定行中的td。我不想对数字指数大惊小怪,而是想通过它的列名访问td。换句话说,我想这样做:jquery datatables-按列名获取td,jquery,datatables,Jquery,Datatables,我想将一个类添加到特定行中的td。我不想对数字指数大惊小怪,而是想通过它的列名访问td。换句话说,我想这样做: for each row in the table where the column "role" contains "some text" do add the "red" class to the td in the "email" column. 我已经找到了一种方法,但我并不十分满意;我觉得太复
for each row in the table where the column "role" contains "some text"
do add the "red" class to the td in the "email" column.
我已经找到了一种方法,但我并不十分满意;我觉得太复杂了。有人有更简单的建议吗
在下面的示例中,对于“角色”列中包含“某些文本”的每一行,我将“red”类添加到“email”列中的td中:
更新 我找到了一个更整洁的方法。实际上,这种解决方案的大部分都可以在官方文档中找到:
此有一个内置回调:。这可能不是您在代码中想要的,您正在使用按钮单击来应用类,但这就是您使用
fnRowCallback
在表呈现时应用类的方式:
tab = $("#myTable").dataTable({
"data": dataset,
"columns": [
{ "data": "uid", name: "uid" },
{ "data": "name", name: "name"},
{ "data": "role", name: "role"},
{ "data": "email", name: "email"}
],
"fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
if(aData[2] == 'some text'){
$('td:eq(3)', nRow).addClass('red');
}
},
...
感谢markpsmith,但它不符合“按其列名称访问td”的要求。您的示例仅使用硬编码的数字索引。
ta.rows().every(function () {
if ($.inArray(this.data().role , ["some text", "some other text,"] ) > -1) {
$(ta.column("email:name").nodes()[this.index()]).addClass("red");
}
})
tab = $("#myTable").dataTable({
"data": dataset,
"columns": [
{ "data": "uid", name: "uid" },
{ "data": "name", name: "name"},
{ "data": "role", name: "role"},
{ "data": "email", name: "email"}
],
"fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
if(aData[2] == 'some text'){
$('td:eq(3)', nRow).addClass('red');
}
},
...