如何生成 jQuery DataTables rowId 客户端?
How to generate jQuery DataTables rowId client side?
jQuery 数据表 reference shows an example of setting the rowId
option to a column from the data source (server side). This setting is used for the "select" extension and retaining row selection on Ajax reload。
有什么方法可以生成行标识符值 1) 客户端,或 2) 作为来自数据源的多个列的组合?
示例数据源:
{
"data": [
{
"aid": 5421,
"bid": 4502,
"name": "John Smith"
}
}
代码:
$("#datatable").DataTable({
select: true,
//rowId: "aid" each row ID is the value of the "aid" column
// e.g., <tr id="5421">
//rowId: 0 each row ID is the value of the 0-indexed column
// e.g., <tr id="5421"> (same as above)
rowId: [0, 1] // How? row ID combined value of 2+ columns
// e.g. <tr id="5421-4502">
rowId: "random" // How? random generated client-side ID
// e.g., <tr id="id34e04">
});
显然无法直接执行此操作。作为解决方法,您可以使用 ajax.dataSrc
option and/or the rowId
选项:
// Example using dataSrc option to manipulate data:
$("#example").dataTable({
ajax: {
url: "data.json",
dataSrc: function (json) {
for (var i = 0, ien = json.data.length; i < ien; i++) {
json.data[i][0] = '<a href="' + json.data[i][0] + '">View Message</a>';
}
}
}
});
这对我有用:
$("#datatable").DataTable({
...
'createdRow': function(nRow, aData, iDataIndex) {
$(nRow).attr('id', 'row' + iDataIndex); // or if you prefer 'row' + aData.aid + aData.bid
},
...
});
我从 question that seems to be a duplicate of this one. The createdRow
callback is documented here 更新了它。
jQuery 数据表 reference shows an example of setting the rowId
option to a column from the data source (server side). This setting is used for the "select" extension and retaining row selection on Ajax reload。
有什么方法可以生成行标识符值 1) 客户端,或 2) 作为来自数据源的多个列的组合?
示例数据源:
{
"data": [
{
"aid": 5421,
"bid": 4502,
"name": "John Smith"
}
}
代码:
$("#datatable").DataTable({
select: true,
//rowId: "aid" each row ID is the value of the "aid" column
// e.g., <tr id="5421">
//rowId: 0 each row ID is the value of the 0-indexed column
// e.g., <tr id="5421"> (same as above)
rowId: [0, 1] // How? row ID combined value of 2+ columns
// e.g. <tr id="5421-4502">
rowId: "random" // How? random generated client-side ID
// e.g., <tr id="id34e04">
});
显然无法直接执行此操作。作为解决方法,您可以使用 ajax.dataSrc
option and/or the rowId
选项:
// Example using dataSrc option to manipulate data:
$("#example").dataTable({
ajax: {
url: "data.json",
dataSrc: function (json) {
for (var i = 0, ien = json.data.length; i < ien; i++) {
json.data[i][0] = '<a href="' + json.data[i][0] + '">View Message</a>';
}
}
}
});
这对我有用:
$("#datatable").DataTable({
...
'createdRow': function(nRow, aData, iDataIndex) {
$(nRow).attr('id', 'row' + iDataIndex); // or if you prefer 'row' + aData.aid + aData.bid
},
...
});
我从 question that seems to be a duplicate of this one. The createdRow
callback is documented here 更新了它。