Skip to content

Commit e62df32

Browse files
authored
Merge pull request #1918 from telerik/kb-grid-custom-expand-update
docs(grid): Updating custom expand-collapse cell KB
2 parents f433452 + 305c311 commit e62df32

File tree

2 files changed

+96
-6
lines changed

2 files changed

+96
-6
lines changed

knowledge-base/custom-expand-collapse-column.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@ How can I have control over the expand/collapse column of the KendoReact Grid an
3030

3131
To modify the expand/collapse column of the Grid:
3232

33-
1. Create a custom column that is bound to the expanded field.
34-
1. Show the icons conditionally.
33+
1. Remove the onExpandChange event handler from the Grid
34+
1. Create a custom column that is bound to a custom expand field (different from the expandField set to the Grid).
35+
1. Show the icons conditionally within the custom cell.
36+
1. On click of the icons, trigger the onChange from the cell props, which will trigger the onItemChange event of the Grid
37+
1. Within the onItemChange of the Grid, when the field is the custom expand field, change the expanded field state
3538

36-
For more information, refer to the following props:
37-
* [`cell`]({% slug api_grid_gridcolumnprops %}#toc-cell)
38-
* [`headerCell`]({% slug api_grid_gridcolumnprops %}#toc-headercell)
3939

40-
For the full implementation of the suggested approach, refer to the [demo on creating a custom expand/collapse column in the KendoReact Grid](https://stackblitz.com/edit/react-abh1id-pwtfuf?file=app/main.jsx).
40+
Following is an example demonstrating this approach:
41+
42+
{% meta id height:540 %}
43+
{% embed_file grid/custom-expand-collapse-column/main.jsx preview %}
44+
{% embed_file shared/products.json %}
45+
{% endmeta %}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import * as React from 'react';
2+
import * as ReactDOM from 'react-dom';
3+
import { Grid, GridColumn as Column } from '@progress/kendo-react-grid';
4+
import products from './products.json';
5+
import { IconWrap } from '@progress/kendo-react-common';
6+
import { minusIcon, plusIcon } from '@progress/kendo-svg-icons';
7+
const DetailComponent = (props) => {
8+
const dataItem = props.dataItem;
9+
return (
10+
<p>
11+
<strong>In Stock:</strong> {dataItem.UnitsInStock} units
12+
</p>
13+
);
14+
};
15+
16+
const expandCell = (props) => {
17+
console.log(props);
18+
return (
19+
<td {...props.tdProps}>
20+
<a
21+
onClick={(e) => {
22+
e.preventDefault();
23+
if (props.onChange) {
24+
props.onChange({
25+
dataItem: props.dataItem,
26+
dataIndex: props.dataIndex,
27+
syntheticEvent: e,
28+
field: 'expanded_custom',
29+
value: !props.dataItem['expanded'],
30+
});
31+
}
32+
}}
33+
href="#"
34+
tabIndex={-1}
35+
>
36+
<IconWrap
37+
name={props.dataItem['expanded'] ? 'minus' : 'plus'}
38+
icon={props.dataItem['expanded'] ? minusIcon : plusIcon}
39+
/>
40+
</a>
41+
</td>
42+
);
43+
};
44+
const App = () => {
45+
const [data, setData] = React.useState(products);
46+
const onItemChange = (event) => {
47+
//This is for the expand only
48+
if (event.field == 'expanded_custom') {
49+
let newData = data.map((item) => {
50+
if (item.ProductID === event.dataItem.ProductID) {
51+
item.expanded = !event.dataItem.expanded;
52+
}
53+
54+
return item;
55+
});
56+
setData(newData);
57+
}
58+
59+
//Add standard onItemChange here
60+
};
61+
return (
62+
<Grid
63+
data={data}
64+
detail={DetailComponent}
65+
style={{
66+
height: '400px',
67+
}}
68+
expandField="expanded"
69+
onItemChange={onItemChange}
70+
>
71+
<Column
72+
field="expanded"
73+
title=" "
74+
width="50px"
75+
cells={{ data: expandCell }}
76+
editable={false}
77+
/>
78+
<Column field="ProductName" title="Product" width="300px" />
79+
<Column field="ProductID" title="ID" width="50px" />
80+
<Column field="UnitPrice" title="Unit Price" width="100px" />
81+
<Column field="QuantityPerUnit" title="Qty Per Unit" />
82+
</Grid>
83+
);
84+
};
85+
ReactDOM.render(<App />, document.querySelector('my-app'));

0 commit comments

Comments
 (0)