How to edit rows from a filtered API instance

How to edit rows from a filtered API instance

YOMYOM Posts: 53Questions: 22Answers: 1

Hi, I have a requirement for users to be able to select multiple rows and click a button to modify a specific column for each selected row- but only if a particular condition is met within the row.

To do this I'm simply filtering the selected rows with a custom validation function. The filtering is working as filteredRows contains only the rows that should be edited.

Here's an example that shows the logic I'm using currently.

setSelectedColumn(datatable, value) {
    const rowSelector = datatable.rows({ selected: true });
    const filteredRows = rowSelector.filter((row) =>
      this.customValidationFunction(row)
    );

    editor.edit(filteredRows, false).val("status", value).submit();
  }

The problem is that filteredRows doesn't appear to be the correct type for editor.edit() to process it.

I've tried getting the nodes/indexes of the filteredRows from the filteredRows object, but when I do filteredRows.rows() it returns all the rows for the current page- not just the filtered rows. I'm unsure how to select only the filteredRows from the result set itself in such a way that I can get their indexes/nodes.

How can I get the indexes/nodes from filteredRows so that editor.edit() can process it?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,261Questions: 26Answers: 4,934
    Answer ✓

    The filter() API returns just the data from the rows matching the condition. This won't work for edit() API. Instead use the row-selector as a function to match the rows you want to edit.

    var filteredRows = datatable
        .rows( function ( idx, data, node ) {
            return data.first_name.charAt(0) === 'A' ?
                true : false;
        }, 
        { selected: true }
     );
    

    To return the selected rows that match the condition. Replace the function with your customValidationFunction() function.

    Kevin

  • YOMYOM Posts: 53Questions: 22Answers: 1

    I had no idea you could pass in a function as a row-selector. That is extremely useful. Thanks so much!

Sign In or Register to comment.