Implementing custom action on button (search and replace)

Implementing custom action on button (search and replace)

smsnheck@web.desmsnheck@web.de Posts: 3Questions: 1Answers: 0

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

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin
    Answer ✓

    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

  • smsnheck@web.desmsnheck@web.de Posts: 3Questions: 1Answers: 0

    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

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    I'd be interested to see how you did it - yes please. What was the problem in the end?

    Allan

  • smsnheck@web.desmsnheck@web.de Posts: 3Questions: 1Answers: 0

    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.

    buttons: 
    [
        {
            text: "Search and replace",
            action: function (e, dt, node, config) {
                table.rows().deselect();
                var searchTerm = new RegExp($('#search').val());
                var replaceTerm = $('#replace').val();
                table.column(1).nodes().each(function (node, index, dt) {
                    var value = table.cell(node).data();
                        if (searchTerm !== '' && searchTerm.test(value)) {
                            if (replaceTerm !== '') {
                                table.row(index).select();
                            }
                    }
                });
                editor.edit(table.rows( {selected: true} ).indexes(), false);
                var rows = editor.field('record').multiGet();
                $.each(rows, function(id, val) {
                    var row = table.row(id);
                    editor.field('record').multiSet(id, val.replace(searchTerm, replaceTerm));
                });
                editor.submit();
                table.rows().deselect();
            },
              editor: editor
        }
    ]
    
This discussion has been closed.