Filter and row index (PRO)

Tags: #<Tag:0x00007f34494efa28>

Hi,

is there possibility to get physical row index of a changed cell when filtering is used?

Eg. http://handsontable.github.io/ngHandsontable/demo/index.html#/PRO-filtering-external-inputs
with modified settings object as:

  this.settings = {
    rowHeaders: true,
    colHeaders: true,
    dropdownMenu: true,
    filters: true,
    afterChange: function(changes, source){
      console.log(changes);
    }
  };

Then if you filter by first column (A) with is_equal_to = 5, you will see only one row (Macy Kowalski) as expected, then when you modify something afterChange will output change at row with index 0 (which also makes sense since there is only one row visible) - but how to get information about exact physical row that was changed (and update corresponding row/value in the database after all)?

Hi @danijel.dukaric

afterChange hook is not triggered by filtering (demo: http://jsfiddle.net/vte6ooz8/)
…but in my opinion it should be. I’ll mark this idea as an improvement suggestion.

Thanks @aleksandra_budnik

Maybe it is not necessary to trigger afterChange on filtering since there is no actual data change - simple row indices mapping (like on sorting) would be enough.

User could simply check something like hot.isFiltered and then map logical to physical index (as it is case with sorting,
if hot.sortingEnabled then hot.sortIndex(...) etc etc

Hi @danijel.dukaric,

Looking into our API I saw that you can check if some column was filtered by executing hot.getPlugin('filters').formulaCollection.isEmpty(). Or if you want to check if filter was applied to specific column you can check this by executing hot.getPlugin('filters').formulaCollection.hasFormulas(3).

After checking if column was filtered you can transform visible row index into physical by hot.runHooks('modifyRow', {visual row index HERE});.

Thank you very much,

this was very helpful and I think it solves my problem :slight_smile:

The trick of using modifyRow seems to work for both filtered rows and sorted rows - in fact this is the most reliable method I have found for converting a row from afterChange (logical) to dataset row (physical):

function HOTLogicalRowsToPhysical(logical) {
  let physical;
  hot.addHookOnce('modifyRow', (rowName) => { physical = rowName; });
  hot.runHooks('modifyRow', logical);
  return physical;
}

Hi @aleksandra_budnik,
I have exactly this type of problem, I have to trigger some events in afterChange but I am no able to trigger them if filters are present.
I’m working on version 7.4.2 (for the only reason that updateSetting doesn’t reset filters, from 8.x.x that reset appens) and this behavior still persist.
Any workaround to suggest? like a custom hook? but i don’t know how to create them…
Any suggestions are welcome!
Thank you in advance

Hi @marcari

Can you tell me a bit more about the updateSettings() resetting your filters? I’ve made a simple demo here https://jsfiddle.net/xL6gw0ms/ but it works as expected

Mmh something strange happened

I was preparing a demo to show you and to do so I disabled a portion of my code into ‘beforechange’ and now the ‘afterchange’ always trigger.

It’s weird because the code I disabled has nothing to do with filters.

To be clear: if I keep the code and I have no filters, it works every single time as expected and ‘afterchange’ trigger; if filters are present, ‘afterchange’ won’t be called

Still, the value set into the cell is lost when i confirm the edit; I guess those two problems are connected.

I need to analyze the situation, I will keep you updated

In the meantime, thank you very much @aleksandra_budnik