Skip to content

Commit 83e4b32

Browse files
committed
backport new aggrid tips
1 parent 160c8c3 commit 83e4b32

File tree

1 file changed

+108
-1
lines changed

1 file changed

+108
-1
lines changed

docs/assets/tips.md

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1940,4 +1940,111 @@ const App = () => (
19401940
);
19411941

19421942
export default App;
1943-
```
1943+
```
1944+
1945+
---
1946+
1947+
By using `<DatagridAG>` or `<DatagridAGClient>`, you can easily customize the cell style in the `columnDefs` definition using `cellStyle`:
1948+
1949+
{% raw %}
1950+
```jsx
1951+
const columnDefs = [
1952+
// same style for each row
1953+
{
1954+
field: 'static',
1955+
cellStyle: {color: 'red', 'background-color': 'green'}
1956+
},
1957+
// different styles for each row
1958+
{
1959+
field: 'dynamic',
1960+
cellStyle: params => {
1961+
if (params.value === 'Police') {
1962+
//mark police cells as red
1963+
return {color: 'red', backgroundColor: 'green'};
1964+
}
1965+
return null;
1966+
}
1967+
},
1968+
];
1969+
```
1970+
{% endraw %}
1971+
1972+
https://www.ag-grid.com/react-data-grid/cell-styles/#cell-style
1973+
1974+
---
1975+
1976+
Similar to what you can do with `<Datagrid>`, you can also use [the CSS API](https://marmelab.com/react-admin/Datagrid.html#sx-css-api) to style your `<DatagridAG>` or your `<DatagridAGClient>`.
1977+
1978+
{% raw %}
1979+
```tsx
1980+
<DatagridAG
1981+
columnDefs={columnDefs}
1982+
sx={{
1983+
".ag-cell.ag-column-first" {
1984+
backgroundColor: "#2244CC44",
1985+
}
1986+
".ag-cell.ag-column-last" {
1987+
backgroundColor: "#CC333344",
1988+
}
1989+
}}
1990+
/>
1991+
```
1992+
{% endraw %}
1993+
1994+
https://www.ag-grid.com/react-data-grid/cell-styles/#first--last-columns
1995+
1996+
---
1997+
1998+
You can add custom CSS classes to any column or cell in `DatagridAG` or `DatagridAGClient`, using the `cellClass` or `cellClassRules` keys of `columnDefs`:
1999+
2000+
{% raw %}
2001+
```tsx
2002+
const columnDefs = [
2003+
// return same class for each row
2004+
{
2005+
field: 'static',
2006+
cellClass: 'my-class'
2007+
},
2008+
// return same array of classes for each row
2009+
{
2010+
field: 'staticArray',
2011+
cellClass: ['my-class1','my-class2'],
2012+
},
2013+
// return class based on function
2014+
{
2015+
field: 'function',
2016+
cellClass: params => {
2017+
return params.value === 'something' ? 'my-class-1' : 'my-class-2';
2018+
},
2019+
},
2020+
// return array of classes based on function
2021+
{
2022+
field: 'functionArray',
2023+
cellClass: params => ['my-class-1','my-class-2'],
2024+
},
2025+
// return array of classes based on many functions with `cellClassRules`
2026+
{
2027+
field: 'functionRules',
2028+
cellClassRules: {
2029+
// apply green to 2008
2030+
'rag-green-outer': params => params.value === 2008,
2031+
// apply blue to 2004
2032+
'rag-blue-outer': params => params.value === 2004,
2033+
// apply red to 2000
2034+
'rag-red-outer': params => params.value === 2000,
2035+
}
2036+
}
2037+
// return array of classes based on many functions with `cellClassRules`
2038+
{
2039+
field: 'simplifiedFunctionRules',
2040+
cellClassRules: {
2041+
'rag-green': 'x < 20',
2042+
'rag-blue': 'x >= 20 && x < 25',
2043+
'rag-red': 'x >= 25',
2044+
}
2045+
}
2046+
];
2047+
```
2048+
{% endraw %}
2049+
2050+
https://www.ag-grid.com/react-data-grid/cell-styles

0 commit comments

Comments
 (0)