Assigning CSS Style to Slickgrid Cells

2020-04-16 04:36发布

问题:

Slickgrid is continuosly evolving so some of the answers to similar issues are not applicable (e.g., they mention it was not possible to assign a CSS to a specific cell). Apparently now this is supported and would appreciate if someone could provide an example using the CSS API at https://github.com/mleibman/SlickGrid/wiki/API-Reference:

grid.setCellCssStyles("birthday_highlight", {
   0: {
        birthday: "highlight", 
        age: "highlight" 
       },

   9: {
         birthday: "highlight",
         age: "highlight"
       }
})

I have a very simple grid with A-Z columns and 1-100 rows: how can I assign a yellow background if the content of the cell is a number?

Thanks

回答1:

This can be done with by implementing getItemMetadata(row) (see https://github.com/mleibman/SlickGrid/wiki/Providing-data-to-the-grid), but you'd have to iterate over all cells, so that's not very efficient. Perhaps a custom formatter that looks at the actual value is a better fit. With that, you won't be able to specify a CSS class for the cell itself, but you'd be able to style the cell content however you like, so technically you could make a DIV that fills the whole cell and set background on that.