0

Using setCellCssStyles inside a for..in loop

Capable to color a specific cell in SlickGrid using setCellCssStyles with an explicit index number of the row such as:

        grid.setCellCssStyles("key_name", {
          0: {
              col_name: "css_class",
            },

          })

But when switch to a counter of the for..in loop it doesn't work:

        grid.setCellCssStyles("key_name", {
          i: {
              col_name: "css_class",
            },

          })

Tried to re render the grid (grid.render()) with/without settimeout after setCellCssStyles , the typeOf(i) is Number

Any ideas?

Thanks (:

Dani
  • 333
  • 3
  • 20

2 Answers2

0

This is because your "keyname" is the same when you set the css styles with the same hash. If you use the same "keyname" and different hashes, it overwrites the previous one and that's why it doesn't work in the for loop. Try adding the loop variable to the "keyname" and see if it works.

addybist
  • 461
  • 1
  • 4
  • 13
-1

This is a javascript issue, not slickgrid. The { i: value } object initialiser is a convenient shorthand for explicit initialisation, it's actually equivalent to { "i": value }.

See: Using a variable for a key in a JavaScript object literal

You should use something like

var temp = {};
for (var i = 0; i < 5; i++) {
  temp[i] = value;
}

It's worth also noting that the object keys must always be text, but in a lot of case the number-to-text conversion is handled by Javscript.
If you think you're initialising an array, you better go read up on the difference between Javascript arrays and objects.

Ben McIntyre
  • 1,972
  • 17
  • 28