'How to create a counter column in a jQuery DataTable?
I have a datatable that only displays records that are in the workflow step = "Waiting", which is the first step in the workflow.
I need to create a column that displays data similar to the following:
If record IDs are [95, 87, 65, 34, 12], then this new column would display [5, 4, 3, 2, 1].
If the datatable is sorted by this new column in ascending order, then the values would be => record IDs [12, 34, 65, 87, 95] new column [1, 2, 3, 4, 5].
If record 95 has its workflow step updated to != "Waiting", then the values would be => record IDs [12, 34, 65, 87] new column [1, 2, 3, 4].
I tried the following, but the values/numbers are not sorted as desired:
{
render: function (data, type, full, meta) {
return meta.row + 1;
}
}
Solution 1:[1]
What if you tried something like this:
//Create an array for your records. Add zero to avoid index 0
var recordIds = [12, 34, 65, 87, 95];
var recordIdArray = $.merge([0], recordIds)
//sort it by record ID in case it's not in order
recordIds.sort();
//assign index position as New Column value
$.each(recordIdArray, function (index, value) {
//create table row example
if (index > 0) {
var tableRow = "<tr><td>"+index+"</td><td>"+value+"</td></tr>";
console.log("NewCol: " + index + " || Record ID: " + value);
}
//append data to your dataTable here. Example: $("#datatable tbody").append(tableRow);
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Petee |
