PDA

View Full Version : Create grid columns from Array



heavenly
10 Aug 2011, 5:08 AM
Hello everyone.
I'm trying to create the columns of my grid dynamically using CakePHP and ride an array controller in this way:



arrayColumns

array(
[0] => array(
['id'] =>'icon_type'
['header'] =>'Oper.'
['width'] =>40
['sortable'] =>'true'
['align'] =>'left'
['dataIndex'] =>'type_name'
['renderer'] =>'renderIconTypeOperation'
)
[1] =>array(
['id'] =>'id'
['header'] =>'Id'
['width'] =>100
['sortable'] =>'true'
['align'] =>'right'
['hidden'] =>'true'
['dataIndex'] =>'id' ......

But when I create the grid with these values ??the renderer does not work because it is a string, then I tried to create as a column type this:



Ajax request columns from controller:
var result = Ext.util.JSON.decode(response.responseText);
var columnsGrid = new Ext.grid.Column(result.arrayColumns);
.....
rGrid = new Ext.grid.GridPanel({
store: rStore,
pageSize: rGridLimit,
loadMask: true,
remoteSort: true,
selModel: new Ext.grid.TaoRowSelectionModel(),
columns: columnsGrid,
.....


but it did not work either.

Can anyone help me?

kaendsle
16 Aug 2011, 8:48 AM
I have used functions to dynamically construct arrays of fields for a reader. Basically, I wrap a self-executing function in parentheses and assign it to the fields config of my Reader. Below is an example:



columns = [
{header: 'Bridge Key', dataIndex: 'brkey', type: 'string', hidden:true},
{header: 'Struct. Num.',dataIndex: 'strc_num', type: 'int'},
{header: 'Region#', dataIndex: 'region', type: 'int'},
{header: 'Facility', dataIndex: 'facility', type: 'string'},
{header: 'Intersect.', dataIndex: 'featint', type: 'string', hidden: true},
{header: 'Location', dataIndex: 'location', type: 'string', hidden: true},
{header: 'Lat', dataIndex: 'latitude', type: 'float', renderer: fix(5), hidden: true},
{header: 'Long', dataIndex: 'longitude', type: 'float', renderer: fix(5), hidden: true},
{header: 'Yr Built', dataIndex: 'yearbuilt', type: 'int', hidden: true},
{header: 'Yr Recon.', dataIndex: 'yearrecon', type: 'int', hidden: true},
{header: 'Yr Painted', dataIndex: 'yearpntd', type: 'int', hidden: true},
{header: 'Yr Overlay', dataIndex: 'yearovly', type: 'int', hidden: true},
{header: 'Deck Rating', dataIndex: 'dkrating', type: 'string'},
{header: 'Sup. Rating', dataIndex: 'suprating', type: 'string'},
{header: 'Sub. Rating', dataIndex: 'subrating', type: 'string'},
{header: 'Culv. Rating',dataIndex: 'culvrating',type: 'string'},
{header: 'Suff. Rate', dataIndex: 'suff_rate', type: 'float', renderer: fix(1)},
{header: 'Lanes', dataIndex: 'lanes', type: 'int', hidden: true},
{header: 'Spans', dataIndex: 'num_spans', type: 'int', hidden: true},
{header: 'Deck Width', dataIndex: 'deck_width',type: 'float', hidden: true, renderer: fix(1)},
{header: 'Length', dataIndex: 'length', type: 'float', hidden: true, renderer: fix(1)},
{header: 'ADT Total', dataIndex: 'adttotal', type: 'int'}
];

reader = new Ext.data.JsonReader({
root: 'data', totalProperty: 'results',
fields: (function() {
var d = [];
Ext.each(columns, function(i) {
d.push({name: i.dataIndex, type: i.type, useNull: true});
});
return d;
}())
});


The result of the function is an Array, and that's exactly what the fields config is looking for. Maybe you could do the same with the columns config of your ColumnModel?

heavenly
16 Aug 2011, 8:53 AM
thanks for the reply.
But I ended up solving, creating functions to create and destroy the store and grid.
So dynamic was the creation, and while the array of columns directly to the grid. ~o)