访问Datatables中另一列中的数据

时间:2017-05-26 17:42:27

标签: javascript jquery datatables

我正在使用Datatables,而我正在尝试访问另一列中的数据。我不清楚我是否应该使用columns.data,或者我是否应该通过使用获取列索引来采用另一种方法?

目的

  • 在第二个渲染功能中,我希望第一个makeSlug(data)引用"data": "district",第二个保持不变并引用"data": "school"

scripts.js中

"columns": [
{ "data": "district",
    "render": function (data, type, row, meta) {
        return '<a href="/schools/' + makeSlug(data) + '">' + data + '</a>';
    }
},
{ "data": "school",
    "render": function (data, type, row, meta) {
        return '<a href="/schools/' + makeSlug(data) + '/' + makeSlug(data) + '">' + data + '</a>';
    }
},
{ "data": "subject"},
{ "data": "rate"},
{ "data": "test_takers"}
],

1 个答案:

答案 0 :(得分:10)

第三个参数row是一个包含该行的完整数据集的数组。使用row['district']访问district媒体资源。

例如:

{ 
   "data": "school",
   "render": function (data, type, row, meta) {
        return '<a href="/schools/' + makeSlug(row['district']) + '/' + makeSlug(data) + '">' + data + '</a>';
    }
}
相关问题