Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit c977f18

Browse files
authored
Merge pull request #1349 from ghiscoding/docs/custom-formatters-native
docs: add Custom Formatter from Native DOM Element
2 parents 6bd4511 + 16a9dde commit c977f18

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

docs/column-functionalities/Formatters.md

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@
22

33
#### Index
44

5-
* [Built-in Formatter](Formatters.md#list-of-provided-formatters)
6-
* [Extra Params/Arguments](Formatters.md#extra-argumentsparams)
7-
* [Using Multiple Formatters](Formatters.md#using-multiple-formatters)
8-
* [Custom Formatter](Formatters.md#custom-formatter)
9-
* [Common Formatter Options](Formatters.md#common-formatter-options)
10-
* [PostRenderer Formatter](Formatters.md#postrender-formatter)
11-
* [UI Sample](Formatters.md#ui-sample)
5+
* [Built-in Formatter](#list-of-provided-formatters)
6+
* [Extra Params/Arguments](#extra-argumentsparams)
7+
* [Using Multiple Formatters](#using-multiple-formatters)
8+
* [Custom Formatter](#custom-formatter)
9+
- [Example of a Custom Formatter with HTML string](#example-of-a-custom-formatter-with-html-string)
10+
- [Example with `FormatterResultObject` instead of a string](#example-with-formatterresultobject-instead-of-a-string)
11+
- [Example of Custom Formatter with Native DOM Element](#example-of-custom-formatter-with-native-dom-element)
12+
* [Common Formatter Options](#common-formatter-options)
13+
* [PostRenderer Formatter](#postrender-formatter)
14+
* [UI Sample](#ui-sample)
1215

1316
#### Definition
1417

1518
`Formatters` are functions that can be used to change and format certain column(s) in the grid. Please note that it does not alter the input data, it simply changes the styling by formatting the data differently to the screen (what the user visually see).
1619

1720
A good example of a `Formatter` could be a column name `isActive` which is a `boolean` field with input data as `True` or `False`. User would prefer to simply see a checkbox as a visual indication representing the `True` flag, for this behavior you can use `Formatters.checkmark` which will use [Font-Awesome](http://fontawesome.io/icons/) icon of `fa-check` when `True` or an empty string when `False`.
1821

19-
For a [UI sample](Formatters.md#ui-sample), scroll down below.
22+
For a [UI sample](#ui-sample), scroll down below.
2023

2124
#### Provided Formatters
2225

@@ -127,7 +130,7 @@ SlickGrid only has 1 `formatter` property but if you want to use more than 1 For
127130
**Note:** please note that combining multiple Formatters has the side effect of cascading the formatted `value` output to the next Formatter. So for example if you use the `complexObject` and `dollar` Formatters, you want to make sure to define them in the correct order in your `formatters: []` array as shown below.
128131
129132
* what if you want to avoid overwriting the `value` with a Custom Formatter?
130-
* in that case you can have your Formatter return a [FormatterResultObject](Formatters.md#formatterresultobject), see below.
133+
* in that case you can have your Formatter return a [FormatterResultObject](#formatterresultobject), see below.
131134
132135
```ts
133136
// Data Example::
@@ -168,17 +171,17 @@ export interface FormatterResultObject {
168171
}
169172
```
170173
171-
#### Example of a Custom Formatter with string output
174+
#### Example of a Custom Formatter with HTML string
172175
173-
For example, we will use `Font-Awesome` with a `boolean` as input data, and display a (fire) icon when `True` or a (snowflake) when `False`. This custom formatter is actually display in the [UI sample](Formatters.md#ui-sample) shown below.
176+
For example, we will use `Font-Awesome` with a `boolean` as input data, and display a (fire) icon when `True` or a (snowflake) when `False`. This custom formatter is actually display in the [UI sample](#ui-sample) shown below.
174177
175178
```ts
176179
// create a custom Formatter with the Formatter type
177180
const myCustomCheckboxFormatter: Formatter = (row: number, cell: number, value: any, columnDef: Column, dataContext: any, grid?: any) =>
178181
value ? `<i class="fa fa-fire" aria-hidden="true"></i>` : '<i class="fa fa-snowflake-o" aria-hidden="true"></i>';
179182
```
180183
181-
**or with `FormatterResultObject` instead of a string**
184+
#### Example with `FormatterResultObject` instead of a string
182185
183186
Using this object return type will provide the user the same look and feel, it will actually be quite different. The major difference is that all of the options (`addClasses`, `tooltip`, ...) will be added the CSS container of the cell instead of the content of that container. For example a typically cell looks like the following `<div class="slick-cell l4 r4">Task 4</div>` and if use `addClasses: 'red'`, it will end up being `<div class="slick-cell l4 r4 red">Task 4</div>` while if we use a string output of let say `<span class="red">${value></span>`, then our final result of the cell will be `<div class="slick-cell l4 r4"><span class="red">Task 4</span></div>`. This can be useful if you plan to use multiple Formatters and don't want to lose or overwrite the previous Formatter result (we do that in our project).
184187

@@ -188,6 +191,27 @@ const myCustomCheckboxFormatter: Formatter = (row: number, cell: number, value:
188191
value ? { addClasses: 'fa fa-fire', text: '', tooltip: 'burning fire' } : '<i class="fa fa-snowflake-o" aria-hidden="true"></i>';
189192
```
190193
194+
### Example of Custom Formatter with Native DOM Element
195+
Since version 4.x, you can now also return native DOM element instead of an HTML string. There are 2 main reasons for going with this approach:
196+
1. CSP Safe by default, since it's native it is 100% CSP Safe (CSP: Content Security Policy)
197+
2. Performance (the reasons are similar to point 1.)
198+
- since it's native it can be appended directly to the grid cell
199+
- when it's an HTML string, it has to do 2 extra steps (which is an overhead process)
200+
i. sanitize the string (we use [DOMPurify](https://github.com/cure53/DOMPurify) by default)
201+
ii. SlickGrid then has to convert it to native element by using `innerHTML` on the grid cell
202+
203+
Demo
204+
```ts
205+
export const iconFormatter: Formatter = (_row, _cell, _value, columnDef) => {
206+
const iconElm = document.createElement('span');
207+
iconElm.className = 'mdi mdi-check';
208+
209+
return iconElm;
210+
};
211+
```
212+
213+
> **Note** you could also use our helper `createDomElement` which allows to create a DOM element and pass properties like `className` in 1 liner (and it also works with intellisense). For example `createDomElement('span', { className: 'bold title', textContent: 'Hello World', title: 'some tooltip description' })` would equal to 4 lines of code.
214+
191215
#### More Complex Example
192216
193217
If you need to add more complex logic to a `Formatter`, you can take a look at the [percentCompleteBar](../../ghiscoding/angular-slickgrid/blob/master/src/app/modules/angular-slickgrid/formatters/percentCompleteBarFormatter.ts) `Formatter` for more inspiration.

0 commit comments

Comments
 (0)