Hi, i want to ask something. so i have component called ParentComponent. then i defined a refrence let grid: Grid | null. and the i passed it to Table Component and ColumnAction Component. in parent component there is function called hide with method hideColumns, then i put on button. it's doesn't work, meanwhile the same function & button inside Table, it'working. can you tell me what's wrong in my code? Thank you
Hi Muhammad,
Greetings from Syncfusion support.
Upon reviewing your query, it appears that you are experiencing an issue with the hide method not properly hiding columns in the Grid component within the ParentComponent. After analyzing the shared code example, it seems that the problem may be due to the Grid reference not being defined correctly. Therefore, we recommend using the "useRef" hook to define the Grid reference. Please refer the below sample and the code example where we initialized the Grid instance with a useRef hook. Now the hide method is working properly with both ParentComponent and the Table Component.
|
Index.js
function ParentComponent() { const grid = useRef(null); // Creating a reference by initializing it with useRef as null. const hide = () => { // Accessing the grid instance using grid.current. if (grid.current) { grid.current.hideColumns('OrderID', 'field'); } }; return ( <div className='control-pane'> <div className='control-section'> <h2>Parent Component</h2> <button onClick={hideColumn}>Hide OrderID Column</button> </div> <div className='control-section'> <h2>Child Component</h2> <Table refGrid={grid} /> </div> </div> ); }
function Table({ refGrid }) { const hide = () => { if (refGrid) { refGrid.current.hideColumns('OrderID', 'field'); } }; return ( <div className='control-pane'> <button onClick={hideColumn}>Hide OrderID Column</button> <div className='control-section'> <GridComponent ref={refGrid} dataSource={data} allowPaging={true} pageSettings={{ pageCount: 5 }} allowSorting={true}> <ColumnsDirective> <ColumnDirective field='OrderID' headerText='Order ID' width='120' textAlign='Right' /> <ColumnDirective field='CustomerName' headerText='Customer Name' width='150' /> <ColumnDirective field='OrderDate' headerText='Order Date' width='130' format='yMd' textAlign='Right' /> <ColumnDirective field='Freight' headerText='Freight' width='120' format='C2' textAlign='Right' /> <ColumnDirective field='ShippedDate' headerText='Shipped Date' width='130' format='yMd' textAlign='Right' /> <ColumnDirective field='ShipCountry' headerText='Ship Country' width='150' /> </ColumnsDirective> <Inject services={[Page, Sort]} /> </GridComponent> </div> </div>
); }
|
Sample: https://stackblitz.com/edit/react-egkytk-qeci3y?file=index.js,index.html
If you are still experiencing an issue, Please try to replicate it in the provided sample.
Regards
Aishwarya R