jspreadsheet / ce

Jspreadsheet is a lightweight vanilla javascript plugin to create amazing web-based interactive tables and spreadsheets compatible with other spreadsheet software.
https://bossanova.uk/jspreadsheet/v4
MIT License
6.77k stars 828 forks source link

onchange "too much recursion" error #1422

Closed kpagcha closed 3 years ago

kpagcha commented 3 years ago

I am building a symmetric table:

+---------+---------+---------+---------+---------+
|         | Venue 1 | Venue 2 | Venue 3 | Venue 4 |
+---------+---------+---------+---------+---------+
| Venue 1 |       0 |       3 |       7 |      10 |
| Venue 2 |       3 |       0 |       1 |       6 |
| Venue 3 |       7 |       1 |       0 |       4 |
| Venue 4 |      10 |       6 |       4 |       0 |
+---------+---------+---------+---------+---------+

And I'm trying to implement the following behavior: when the value of a cell is changed, the mirror cell should be automatically updated. So I plugged in the following function to the onchange listener:

function (instance, cell, x, y, value) {
  const name = jexcel.getColumnNameFromId([y, x]);
  jexcel.current.setValue(name, value);
}

Problem: changing the other cell's value also triggers the listener and we end up in a recursion loop.

I wouldn't think changing another cell's value when changing one is such a weird ask, so what am I doing improperly here?

GBonnaire commented 3 years ago

You need to defined the var ignoreEvents to true for not recall onchange

onchange: function (instance, cell, x, y, value) {
     jexcel.current.ignoreEvents = true;
     const name = jexcel.getColumnNameFromId([y, x]);
     jexcel.current.setValue(name, value);
     jexcel.current.ignoreEvents = false;
}
kpagcha commented 3 years ago

Also what's the listener for when a single cell is selected and when it loses focus?

For selected I could use onselection although it doesn't discriminate between dragging and simply clicking a cell (would be forced to use borders).

But how about losing a selection? ("blur")

GBonnaire commented 3 years ago

maybe i don't understand you question.

For detect blur you have event onblur

For make a blur : jspreadsheet.current.resetSelection(true);