Assuming you mean that you do not want to have to write 50 separate “if” statements as in @aleksandra_budnik’s JSFiddle, it’s really not hard to factor the code neatly to make it handle lots of cases. For example:
var colors = {
yellow: 10,
red: 20,
orange: 30,
...
};
var color = changes[0][3];
output.innerHTML = colors[color];
That’s it! And if your color names/values or whatever are in a database you can just look it up there instead of needing the literal “colors” values enumeration.
So, for example, let’s assume you have database rows which have a name
column for the color names and a value
column for the corresponding number-value. Then instead of hard-coding you can dynamically create the colors
object I’m using with something like:
var colors = {};
foreach row in data-table do
colors[ row[name] ] = row[value];
end
And furthermore, to get the array of color names you need to pass as source:
to the Handsontable
constructor, you can just use Object.keys(colors)
.
Of course, assuming you will do this population at Handsontable creation-time, you will need to use colors
as a “global variable” so that it is preserved for use later in the afterChange
callback. You could save it as a (user-defined/extra) property on your Handsontable instance so that it is easily accessible in the right place.