How to search and replace a word in datatable rollCallback
How to search and replace a word in datatable rollCallback
Ashwani_rai01
Posts: 1Questions: 1Answers: 0
I am using txt file to show the data into datatable, I edit the data on another screen and then I edit the file also, and the data which is edited, I concatenate a word "updated" with the original data in the file(for instance: 234updated), Now I want to search a data which contains word updated and replace it with blank and also highlight it with the color yellow. Here is my code
$('#arrTableCustomer').DataTable({
ajax: function(data, callback) {
$.ajax({
url: "../Arr_Data_Files/<?php echo $clientName ?>_Arr2.txt",
dataType: 'json',
success: function(data) {
var array = [];
var array2 = [];
var array3 = [];
jQuery.each(data, function(index, value) {
for (i = 0; i < lengthArray2; i++) {
var array = [];
array.push(value[i][0]);
for (j = 1; j <= total; j++) {
array.push(value[i][j]);
}
array2.push(array);
}
});
array3.push({
"data": array2
});
var obj = Object.assign({}, array3[0]);
callback(obj);
}
});
},
deferRender: true,
// serverSide: true,
sScrollX: "100%",
ordering: false,
scrollY: 300,
scrollCollapse: true,
scroller: true,
fixedColumns: {
left: 1
},
dom: '<"html5buttons"B>lTfgitp',
buttons: [{
extend: 'csv',
title: 'Arr By Customer'
},
{
extend: 'excel',
title: 'Arr By Customer'
},
],
fixedHeader: {
//header: true,
footer: false
},
processing: true,
language: {
'loadingRecords': '',
'processing': 'Loading Data...'
},
"rowCallback": function(row, data, index, type) {
let DataLength = data.length;
for (let i = 1; i < DataLength; i++) {
$(row).find('td:eq(' + 0 + ')').css('text-align', 'left').html('<a href="customerScreen.php?custname=' + encodeURIComponent(data[0]) + '" target="_blank">' + data[0] + '</a>');
if (data[i] == "updated") {
$(row).find('td:eq(' + i + ')').css('background-color', 'yellow');
}
const formatter = new Intl.NumberFormat('en-US', {});
if (Number.parseFloat(data[i])) {
$(row).find('td:eq(' + i + ')').css({
'text-align': 'center',
'font-size': '14px'
}).html(formatter.format(Math.round(data[i])));
} else {
$(row).find('td:eq(' + i + ')').css({
'text-align': 'center',
'font-size': '14px'
}).html(data[i]);
}
if (data[i] > 0 && data[i] > data[i - 1]) {
//$(row).find('td:eq('+i+')').html(Math.round(data[i]))
$(row).find('td:eq(' + i + ')').css('background-color', '#d9ffb3');
}
if (data[i] > 0 && data[i] < data[i - 1]) {
$(row).find('td:eq(' + i + ')').css('background-color', '#ffb3b3');
}
if (data[i] < data[i - 1]) {
$(row).find('td:eq(' + i + ')').css('background-color', '#ffb3b3');
}
if (data[i] > 0 && data[i - 1] == data[0]) {
$(row).find('td:eq(' + i + ')').css('background-color', '#d9ffb3');
}
}
},
"autoWidth": false,
"footerCallback": function(row, data, start, end, display) {
var api = this.api();
nb_cols = api.columns().nodes().length;
var j = 1;
while (j < nb_cols) {
var pageTotal = api
.column(j, {
page: 'all'
})
.data()
.reduce(function(a, b) {
return Number(a) + Number(b);
}, 0);
const formatter = new Intl.NumberFormat('en-US', {});
$(api.column(j).footer()).css({
'text-align': 'center'
}).html('$' + formatter.format(Math.round(pageTotal)));
j++;
}
}
});
Answers
If you replace the word
Updated
with an empty string then the next timerowCallback
runs the highlighting will be gone as the word you are checking for is gone. One option might be to usecolumns.render
to remove the word for the filter, sort and display operations. This should leave the word in the data that can be used byrowCallback
. See the orthogonal data docs for more details. See this example:https://live.datatables.net/miledagu/1/edit
Kevin