I have a radio button that let users select whether to freeze a column or not.
The grid instance may already load before the user's selection.
How do I implement this? I'm thinking about destroy the grid instance but $("#Grid").ejGrid("destroy"); showing error that "ejGrid is not a function"
But please feel free to suggest another solution
Hi Edward,
Greetings from Syncfusion support
We have checked your query and we could see that on clicking in that radio button you like to freeze the column. Based on your query we have prepared a sample and we suggest you use the below way to achieve your requirement. Please refer the below code example and sample for more information.
|
let grid: Grid = new Grid( { dataSource: orderData, allowPaging: true, dataBound: function() { let radiobutton: RadioButton = new RadioButton({ label: 'Ship Country', change: change, name: 'default' }); // Render initialized RadioButton. radiobutton.appendTo('#element1'); }, height: 365, columns: [ { field: 'OrderID', headerText: 'Order ID', width: 120, textAlign: 'Right' }, { field: 'CustomerName', headerText: 'Customer Name', width: 150 }, { field: 'OrderDate', headerText: 'Order Date', width: 135, format: 'yMd', textAlign: 'Right' }, { field: 'Freight', width: 120, format: 'C', textAlign: 'Right' }, { field: 'ShippedDate', headerText: 'Shipped Date', width: 140, format: 'yMd', textAlign: 'Right' }, { field: 'ShipCountry', headerTemplate: '#datetemplate', headerText: 'Ship Country', width: 150 } ], pageSettings: { pageCount: 2, pageSizes: true } }); grid.appendTo('#Grid');
function change(args) { //change event of radio button var column = grid.getColumnByIndex(+args.event.target.closest('th').getAttribute('aria-colindex')); column.isFrozen = true; grid.refreshColumns(); //UI changes }
|
Sample: https://stackblitz.com/edit/mzj4da?file=index.ts,index.html
Regards,
Rajapandi R
Thanks for your response. I tried your solution, but the grid.refreshColumns(); makes the datasource reloading non stop.
Btw, I should mention that the button is a control outside of the grid.
The scenario is when user selects the button, the grid will refresh and freeze the first column.
Hi Edward,
Thanks for your update
Based on your reported scenario we have prepared a sample and achieved your requirement, please refer the below code example and sample for more information.
|
let radiobutton: RadioButton = new RadioButton({ label: 'Check to Freeze', change: change, name: 'state', checked: false }); radiobutton.appendTo('#radiobutton1');
let grid: Grid = new Grid( { dataSource: orderData, allowPaging: true, height: 365, columns: [ { field: 'OrderID', headerText: 'Order ID', width: 120, textAlign: 'Right' }, { field: 'CustomerName', headerText: 'Customer Name', width: 150 }, { field: 'OrderDate', headerText: 'Order Date', width: 135, format: 'yMd', textAlign: 'Right' }, { field: 'Freight', width: 120, format: 'C', textAlign: 'Right' }, { field: 'ShippedDate', headerText: 'Shipped Date', width: 140, format: 'yMd', textAlign: 'Right' }, { field: 'ShipCountry', headerText: 'Ship Country', width: 150 } ], pageSettings: { pageCount: 2, pageSizes: true } }); grid.appendTo('#Grid');
function change(args) { var column = grid.getColumnByField('ShipCountry'); column.isFrozen = true; grid.refreshColumns(); }
|
Sample: https://stackblitz.com/edit/mzj4da-gjdfbb?file=index.ts,index.html
Regards,
Rajapandi R
thank you for your solution. I tried your code and having another issue with refreshColumn() after clicking on the radio button. It performs multiple requests to the service that provides the data and do not stop. The grid keeps refreshing.
I include my code here. When the grid is refreshed, I call onReload function and I aware that using the refreshColumn() causing multiple requests. That's why I use a workaround approach to update dataSource and dataQuery. is there any suggestion to apply your solution for freezing column into my code ?
Hi Edward,
Thanks for the update
Currently, we are validating this query with your shared information, and we will update you the details on or before 27th June 2022. Until then we appreciate your patience.
Rajapandi R
Hi Edward,
Thanks for the update.
By default, when we invoke the refreshColumns() method the dataBound event gets triggered, in your query you have performed some actions in dataBound event, so it will trigger again and again like loop. It was the cause of the problem. To resolve the problem, we suggest you use the below way to achieve your requirement. Please refer the below code example and sample for more information.
|
<script> var flag = false; function load() { //load event of Grid flag = true; } function bound() { //dataBound event of Grid if (flag) { flag = false; . . . . . . . . . . . //use your implementation here . . . . . . . . . . . this.refreshColumns(); } } </script>
|
API: https://ej2.syncfusion.com/documentation/api/grid/#databound
https://ej2.syncfusion.com/documentation/api/grid/#load
Regards,
Rajapandi R