Implementing custom action on button (search and replace)
Implementing custom action on button (search and replace)
Hi,
I want to implement a custom action on a button (search and replace). I found already the multi-row (bulk edit) blog post and implementet my function like that.
I have the following javascript code in the Buttons section in my DataTable object:
{
text: "search and replace",
action: function (e, dt, node, config) {
var searchTerm = new RegExp($('#search').val());
var replaceTerm = $('#replace').val();
var rows = table.rows( {selected: true} ).indexes();
editor.edit(rows, false);
$.each(rows, function(i, rowIdx) {
var value = table.cell(node).data();
if (searchTerm !== '' && searchTerm.test(value)) {
if (replaceTerm !== '') {
editor.field('someField').multiSet(row.id(), row.data().someField.replace(searchTerm, replaceTerm));
}
}
});
editor.submit();
},
editor: editor
}
When I am searching and replacing for one term only it works fine. But if I want to search and replace multiple values on multiple rows it won't work.
The replace function works fine. console.log prints out the correct values. But these values aren't set inside the table row(s) and sent properly to the backend.
Any suggestions? Or is this impossible?
This question has an accepted answers - jump to answer
Answers
That's a really clever use of custom buttons and multi-row editing - nice one.
I don't immediately see any reason why that wouldn't work from the above code. Are you able to give me a link to a page showing the issue so I can take a look and trace it through please?
Thanks,
Allan
Thank you allan. Sorry for my late reply. But I managed to get this to work properly. I can provide you my example if you want to feature it in your examples. Just let me know
I'd be interested to see how you did it - yes please. What was the problem in the end?
Allan
I don't know what went wrong. I refactored a little bit and changed some parameters in function calls:
The idea (maybe there is a better solution) is to search first the values that match the regex pattern and select these rows. On the next step in the selected rows the search term is replaced by the replace term and will be submitted.