We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Re-set the column layout after initialization

Hi, 
We save the settings of the grid externally and I need to completely reorganize the columns in the grid after it has been rendered. By that, I mean that I have to change the order, the width and the visibility of many columns, and I don't know in advance which it will be. The user can change the configuration of the grid at any moment. I try to just put the new columns in the grid 'column' member. It changes the display correctly, but the columnMenu and the drag and drop grouping stops working.

Let's says I have this grid:
let grid: Grid = new Grid({
    dataSource: data,
    columns: [
                { field: 'OrderID', headerText: 'Order ID', textAlign: 'Right', width: 120, type: 'number' },
                { field: 'CustomerID', width: 140, headerText: 'Customer ID', type: 'string' },
                { field: 'Freight', headerText: 'Freight', textAlign: 'Right', width: 120, format: 'C' },
                { field: 'OrderDate', headerText: 'Order Date', width: 140, format: 'yMd' }
    ],
    allowPaging: true,
    allowReordering: true,
    allowGrouping: true,
    allowSorting: true,
    pageSettings: { pageSize: 7 }
});

And I want to change the columns to this:
let newColumns = [
     { field: 'OrderID', headerText: 'Order ID', textAlign: 'Right', width: 120, type: 'number' },
     { field: 'OrderDate', headerText: 'Order Date', width: 80, format: 'yMd' }
     { field: 'Freight', headerText: 'Freight', textAlign: 'Right', width: 200, format: 'C' },
     { field: 'CustomerID', width: 70, headerText: 'Customer ID', type: 'string', visible: false },
];

I tried to do:
grid.columns = newColumns;
but the sorting, the columnMenu and the drag and drop grouping stops working.

How would I do that without having to destroy the grid and keep the functionalities of the grid?

Thanks!

3 Replies

TS Thavasianand Sankaranarayanan Syncfusion Team February 27, 2019 08:56 AM UTC

Hi Jessica, 
 
Greetings from Syncfusion. 
 
Query: I try to just put the new columns in the grid 'column' member. It changes the display correctly, but the columnMenu and the drag and drop grouping stops working. I tried to do: grid.columns = newColumns; but the sorting, the columnMenu and the drag and drop grouping stops working. How would I do that without having to destroy the grid and keep the functionalities of the grid? 
 
We have validated your query and we have checked the reported problem at our end. It works fine. Here, we have prepared a sample based on your code snippets. We have changed the grid columns dynamically by using button click. Grid functionalities like sorting, grouping and column menu are working fine. Please find the below code example and sample for your reference. 
 
[code example] 
... 
import { Grid, Selection, Page, Reorder, Group, Sort, ColumnMenu } from '@syncfusion/ej2-grids'; 
... 
 
Grid.Inject(Selection, Page, Reorder, Group, Sort, ColumnMenu); 
 
/** 
* Default Grid sample 
*/ 
 
     
    let data: Object = new DataManager(orderData as JSON[]).executeLocal(new Query().take(15)); 
    let grid: Grid = new Grid({ 
    dataSource: data, 
    columns: [ 
                { field: 'OrderID', headerText: 'Order ID', textAlign: 'Right', width: 120, type: 'number' }, 
                { field: 'CustomerID', width: 140, headerText: 'Customer ID', type: 'string' }, 
                { field: 'Freight', headerText: 'Freight', textAlign: 'Right', width: 120, format: 'C' }, 
                { field: 'OrderDate', headerText: 'Order Date', width: 140, format: 'yMd' } 
    ], 
    allowPaging: true, 
    allowReordering: true, 
    allowGrouping: true, 
    allowSorting: true, 
    pageSettings: { pageSize: 7 }, 
    showColumnMenu: true, 
}); 
 
let newColumns = [ 
     { field: 'OrderID', headerText: 'Order ID', textAlign: 'Right', width: 120, type: 'number' }, 
     { field: 'OrderDate', headerText: 'Order Date', width: 80, format: 'yMd' }, 
     { field: 'Freight', headerText: 'Freight', textAlign: 'Right', width: 200, format: 'C' }, 
     { field: 'CustomerID', width: 70, headerText: 'Customer ID', type: 'string', visible: false }, 
]; 
grid.appendTo('#Grid'); 
 
let chngBut: Button = new Button({ cssClass: 'e-flat'},'#change'); 
 
document.getElementById('change').onclick = () => { 
  (grid as any).columns = newColumns;      //change  
} 
 
 
 
Please get back to us if you need further assistance. 
 
Regards,
Thavasianand S. 



JD Jessica DeBlois February 27, 2019 10:52 PM UTC

Hi,
My bad, I was deleting the prototypes of the columns when applying the new ones. So the grid was not able to use the methods afterward.

Thanks for the help!


TS Thavasianand Sankaranarayanan Syncfusion Team February 28, 2019 04:24 AM UTC

Hi Jessica, 
 
We are happy that the problem has been solved. 
 
Please get back to us if you need any further assistance.  
                          
Regards, 
Thavasianand S.

Loader.
Live Chat Icon For mobile
Up arrow icon