[]
While FlexGrid supports styling cells using the custom CSS classes or attributes using the formatItem event, it also allows to set different styles on a large number of rows and style cells in any range using the ‘wijmo.grid.style’ module. This module is a lightweight extension which does not require writing multiple lines of code to style a cell or cell ranges. It provides the FlexGridStyle class which contains methods to specify the style to be applied on the cell ranges and the gridpanel to which the cell range belongs.
The FlexGridStyle class provides different methods to easily add and remove styles from cells. To use these methods, you need to add ‘wijmo.grid.style’ module. Let us learn about these methods in detail in the following sections.
The FlexGridStyle class provides the applyStyle method to apply either inline or class-based CSS style on a cell range. This method allows the users to specify the style to be applied on a particular cell range.
Syntax:
applyStyle(rng: CellRange, style: Partial<CSSStyleDeclaration>, panel?: GridPanel, inline?: boolean): void;
Parameter Name | Description |
---|---|
cellRange(required) | Specifies the range of cells to which the style should be applied |
style (required) | Specifies the CSS style object to be applied on the cell range. The passed style object should conform to the CSSStyleDeclaration type. |
gridPanel (optional) | Specifies the panel to which the cell range belongs - (Cells/TopLeft/ColumHeader/RowHeader). By default it is 'Cells'. |
inline (optional) | Specifies whether to apply the style inline on the cell element or add a new CSS class. Works only when an object is passed to the style parameter. By default its value is true. |
Code Example:
In this example, the applyStyle method applies class-based CSS on a specified cell range. Here, the inline parameter is set as ‘false’, which converts the style object to a CSS class and adds it to the document. However, by default, the value of the inline parameter is true for the applyStyle method.
let cssObject = { "background-color": "yellow !important", "font-size" : '10px' };
flexGridStyle.applyStyle(new CellRange(3,1,3,2), cssObject, theGrid.cells, false);
In FlexGrid, you can toggle between adding or removing a particular CSS class on grid cells using the toggleClass method of the FlexGridStyle class. This method allows you to toggle the specified CSS class name on the provided cell range.
Syntax:
toggleClass(rng: CellRange, className: string, panel?: GridPanel, addOrRemove?: boolean)
Parameter Name | Description |
---|---|
rng (required) | Specifies range of cells to which CSS class should be applied |
className (required) | Specifies the CSS class name to be applied on the provided range |
panel (optional) | Specifies the panel to which the cell range belongs (Cells/TopLeft/ColumHeader/RowHeader). By default it is Cells. |
addOrRemove (optional) | Indicates whether the class should be added or removed by specifying the Boolean value. If no value is provided, then toggle the class. |
Code Example:
//Add the class name
gridStyle.toggleClass(new wjGrid.CellRange(0,0,2,1), 'myStyle1', theGrid.cells, true);
//Remove the class name
gridStyle.toggleClass(new wjGrid.CellRange(0,0,2,1), 'myStyle1', theGrid.cells, false);
//Toggle the class name
gridStyle.toggleClass(new wjGrid.CellRange(0,0,2,1), 'myStyle1');
The FlexGridStyle class provides the following methods to remove styles:
removeStyle – It removes a particular CSS style from a specified cell range as shown in the following code.
Syntax:
removeStyle(rng: CellRange, style: string | Partial<CSSStyleDeclaration>, panel?: GridPanel)
Parameter Name | Description |
---|---|
cellRange (required) | Specifies the range of cells from which style should be removed |
style (required) | Specifies a CSS style object to be removed from the cell range. The provided style object should conform to the CSSStyleDeclaration type. If the style object was previously applied with inline set to false, the associated class name will be removed; otherwise, the inline style will be cleared. |
gridPanel (optional) | Specifies the panel to which the cell range belongs (Cells/TopLeft/ColumHeader/RowHeader). By default it is 'Cells'. |
Code Example:
flexGridStyle.removeStyle(new CellRange(10,1,11,2), "testClassGreen");
clearAllStyles – It clears all applied styles from a specified cell range, or the entire grid as shown in the following code.
Syntax:
clearAllStyles(): void; //To clear all styles
clearAllStyles(rng: CellRange, panel?: GridPanel): void; //To clear the styles in the cell range
Parameter Name | Description |
---|---|
cellRange (optional) | Specifies the range of cells from which styles should be removed |
gridPanel (optional) | Specifies the panel to which the cell range belongs (Cells/TopLeft/ColumHeader/RowHeader). By default it is 'Cells'. |
Code Example:
//To clear all styles
flexGridStyle.clearAllStyles();
//To clear styles in the cell range
flexGridStyle.clearAllStyles(new CellRange(10,1,11,2))
When CSS styles are added using the applyStyle method and ‘background-color' is specified in the style, then it overrides FlexGrid’s selection style, and the cell seems unselected.
When the ‘inline’ parameter is set to ‘false’ in the style applied using the applyStyle method, it overrides any style applied on the same cell using the toggleClass method.
Submit and View Feedback For