EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: lusabo on February 29, 2012, 10:50:01 AM



Title: Datagrid with variable number of columns
Post by: lusabo on February 29, 2012, 10:50:01 AM
How to create a datagrid whose number of columns is variable?


Title: Re: Datagrid with variable number of columns
Post by: devnull on March 21, 2012, 01:17:13 AM
Hi;

Well the columns will be created based on the columns provided to the datagrid:

This will dynamically create 3 columns:

Code:
columns: [[
{field:'xxx',title:'xxx'},
{field:'xxx',title:'xxx'},
{field:'xxx',title:'xxx'},
]]

And this will dynamically create 5 columns:

Code:
columns: [[
{field:'xxx',title:'xxx'},
{field:'xxx',title:'xxx'},
{field:'xxx',title:'xxx'},
{field:'xxx',title:'xxx'},
{field:'xxx',title:'xxx'},
]]

I recall that there's also a demo on how to change the columns under the demo section.

Cheers


Title: Re: Datagrid with variable number of columns
Post by: mzeddd on March 23, 2012, 03:03:05 AM
Hi,

I had no time to study this much but in my project I create complete list of columns but make only part of them visible.
And of course to increase dataload performance I do not load data into hidden columns.

Similar idea could be seen in Demo examples.

//Valery


Title: Re: Datagrid with variable number of columns
Post by: Kevin on April 04, 2012, 04:40:29 AM
Hi lusabo

I agree with mzeddd response. Just hide/unhide columns, but it you want to add/remove column, just reinit the datagrid. I'm sure there are better ways to do this, but you should get the idea from the example;
var menuStruct = [];
var menuItems = [];
var menuItem1 = {
   field: 'field1',
   title: 'Title1',
   width: 100
}
var menuItem2 = {
   field: 'field2',
   title: 'Title2',
   width: 105
}

function insertCol() {
   menuStruct.pop();
   menuItems.push(menuItem2);
   menuStruct.push(menuItems);

   $('#myDatagrid').datagrid({
      columns: menuStruct
   });
}

$(function () {
   menuItems.push(menuItem1);
   menuStruct.push(menuItems);

   $('#myDatagrid').datagrid({
      columns: menuStruct,
      fit: false,
      width: 370,
      nowrap: false,
      striped: true,
      collapsible: true,
      url: url
   });
}

To delete a column means you need to remove the menu object from the menuItems array and reinit the datagrid.