为什么jQuery数据表<stdint.h>
不能使用类名? <{1}}回调永远不会被解雇。
字符串 - 类名称将匹配列
的TH
另外,如果我改为目标索引my_global_method
,我会收到错误。此外,它大约发射22次。不应该开火6次?索引为0的每个单元格为1?
columnDefs
答案 0 :(得分:1)
columns.class
属性,请改用columns.className。targets:[0]
时收到错误,因为您的columnDefs.render回调不会返回任何数据。除非您需要按设计引用类名,否则使用targets:0
或targets:[0]
是引用列的首选方法。您的更正代码是:
var dataSet = [
['Trident','Internet Explorer 4.0','Win 95+','4','X'],
['Trident','Internet Explorer 5.0','Win 95+','5','C'],
['Trident','Internet Explorer 5.5','Win 95+','5.5','A'],
['Trident','Internet Explorer 6','Win 98+','6','A'],
['Trident','Internet Explorer 7','Win XP SP2+','7','A'],
['Trident','AOL browser (AOL desktop)','Win XP','6','A']
];
$('#example').dataTable( {
columnDefs: [
{
render: function ( data, type, row, meta ) {
console.log(type, data, row);
return data;
},
targets: 0
}
],
columns: [
{ "title": "Engine", "className": "foo" },
{ "title": "Browser" },
{ "title": "Platform" },
{ "title": "Version", "className": "center" },
{ "title": "Grade", "className": "center" }
],
data: dataSet
});
请参阅this JSFiddle进行演示。
优化代码
如果您不需要按类名引用列,则可以进一步优化代码。可以在columnDefs.render
属性中定义columns
回调。我还在render
回调中添加了类型检测,以演示如何使用它。
请参阅下面的代码。
var dataSet = [
['Trident','Internet Explorer 4.0','Win 95+','4','X'],
['Trident','Internet Explorer 5.0','Win 95+','5','C'],
['Trident','Internet Explorer 5.5','Win 95+','5.5','A'],
['Trident','Internet Explorer 6','Win 98+','6','A'],
['Trident','Internet Explorer 7','Win XP SP2+','7','A'],
['Trident','AOL browser (AOL desktop)','Win XP','6','A']
];
$('#example').dataTable( {
columns: [
{
"title": "Engine",
"className": "foo",
"render": function ( data, type, row, meta ) {
// If data is being displayed
if(type === "display"){
return "<b>" + data + "</b>";
// Otherwise, if data is not being displayed
} else {
return data;
}
},
},
{ "title": "Browser" },
{ "title": "Platform" },
{ "title": "Version", "className": "center" },
{ "title": "Grade", "className": "center" }
],
data: dataSet
});
请参阅this JSFiddle进行演示。