Results 1 to 3 of 3

Thread: Create grid columns from Array

  1. #1
    Sencha User
    Join Date
    Aug 2011
    Posts
    8

    Question Create grid columns from Array

    Hello everyone.
    I'm trying to create the columns of my grid dynamically using CakePHP and ride an array controller in this way:

    PHP Code:
    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:

    Code:
    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?

  2. #2
    Sencha User
    Join Date
    Nov 2010
    Posts
    36

    Default

    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:

    Code:
            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?

  3. #3
    Sencha User
    Join Date
    Aug 2011
    Posts
    8

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •