Kendo Grid build columns dynamically

1 Answer 1481 Views
Grid
Neelima
Top achievements
Rank 1
Neelima asked on 16 Nov 2017, 09:12 PM

Hi there,

I have an array with the list of column names, that needs to be displayed in the grid. 

So, while initializing the grid, I need to loop through each column in the datasource - and if that column is present in the Array, then I need to display it.

For example,

var ColumnNames = ["col1","col3"];

var dataSource = [{"col1": "a", "col2": "1", "col3": "11","col4": "1111"}, {"col1": "b", "col2": "2", "col3": "22","col4": "2222"}, {"col1": "c", "col2": "3", "col3": "33","col4": "3333"}, .....]

 

The grid should display only with the columns present in the array. Here, as shown below:

col1    col3

---------------

a        11

b        22

c        33

.......

 

The ColumnNames Array and dataSource actually comes from DB, based on the user selection. So, I can not hardcode column names. I tried various options (using column templates, foreach loops for building the model, etc) , but facing one or the other issue. Can someone help me out on this please?

 

Thanks in advance!

 

Regards

Neelima

1 Answer, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 20 Nov 2017, 01:20 PM
Hello Neelima,

The example below illustrates how you can specify the Grid model dynamically from the received data. Using similar approach you can iterate through an array and set the columns settings for the Grid dynamically. 



Regards,
Viktor Tachev
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Marco
Top achievements
Rank 1
commented on 14 Mar 2019, 03:51 PM

Hello Viktor,

Is there a way to do the same thing with kendo asp.net mvc?
As I understand I have to pass a fixed model to kendo but I need to create a grid and a treelist in a dynamic way.

Regards,

Marco

Viktor Tachev
Telerik team
commented on 18 Mar 2019, 01:14 PM

Hello Marco,

We have couple of examples that illustrate how to build the Grid dynamically and also how to bind it to dynamic data. Check them out below:



Regards,
Viktor Tachev
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Neelima
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Share this question
or