Here is one approach, which iterates over the following items:
- the HTML table nodes in a specific column
- the equivalent DataTable cell values for the same column
The code uses the cell values from the underlying DataTable object to see when a value changes. It then uses that information to change the related HTML table node value (what you see displayed in the HTML).
So, whereas the underlying DataTable values are never changed, the HTML values are adjusted.
Also, we use a selector modifier
, which instructs the tbl.column()
function how to handle sorted and filtered data. Basically, we only operate on the data which is visible to the user.
function processColumnNodes(tbl) {
// see https://datatables.net/reference/type/selector-modifier
var selector_modifier = { order: 'current', page: 'current', search: 'applied' }
var previous = '';
var officeNodes = tbl.column(1, selector_modifier).nodes();
var officeData = tbl.column(1, selector_modifier).data();
for (var i = 0; i < officeData.length; i++) {
var current = officeData[i];
if (current === previous) {
officeNodes[i].textContent = '';
} else {
officeNodes[i].textContent = current;
}
previous = current;
}
}
In the above example, I am accessing column index 1 (the 2nd column in the table - so, slightly different from your question).
This function can be used as follows, with two DataTables events:
initComplete
- for the initial drawing of the table
- subsequent
draw
events
var table = $('#example').DataTable( {
data: dataSet.data,
"order": [[ 1, 'asc' ]], // just as a test for the initial draw
columns: [
{ title: "Name", data: "name" },
{ title: "Office", data: "office" }, // values will be hidden
{ title: "Office2", data: "office" }, // values will not be hidden, for verification
{ title: "Position", data: "position" },
{ title: "Start date", data: "start_date" },
{ title: "Salary", data: "salary" }
],
"initComplete": function(settings, json) {
// in case the initial sort order leads to
// cells needing to be altered:
processColumnNodes( $('#example').DataTable() );
}
} );
table.on( 'draw', function () {
processColumnNodes( $('#example').DataTable() );
} );
function processColumnNodes(tbl) {
// see https://datatables.net/reference/type/selector-modifier
var selector_modifier = { order: 'current', page: 'current', search: 'applied' }
var previous = '';
var officeNodes = tbl.column(1, selector_modifier).nodes();
var officeData = tbl.column(1, selector_modifier).data();
for (var i = 0; i < officeData.length; i++) {
var current = officeData[i];
console.log( i + ' ' + current );
if (current === previous) {
officeNodes[i].textContent = '';
} else {
officeNodes[i].textContent = current;
}
previous = current;
}
}
An example of the result, where I have also included "Office 2", showing the original unchanged node data, for comparison:
Because we are hiding values in the HTML table nodes, functions such as sorting and filtering are not affected (the original values are still stored in DataTables, as originally loaded).
Clarification: For testing purposes I provided my data in a JavaScript variable:
var dataSet = {
"data": [
{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
},
...
{
"id": "12",
"name": "Quinn Flynn",
"position": "Support Lead",
"salary": "$342,000",
"start_date": "2013/03/03",
"office": "Edinburgh",
"extn": "9497"
}
]
};