Dinamically creating DataGrid from imported .txt file


i have tab delimited .txt file exported excel looks this:

 

    sector    section    family    code    brand    image    description    quantity    price

    sector 1    section 1    family 1    10000    fiat    10000    description 10000    8    25,00

    sector 1    section 1    family 1    10001    kawasaky    10001    description 10001    10    45,00

    sector 1    section 1    family 1    10002    ford    10002    description 10002    15    10,00

    sector 1    section 1    family 2    10003    fiat    10003    description 10003    100    8,00

 

 

 

in case have 9 columns, method should work number of columns.

the code developed in flash builder, as3.

i dinamically create datagrid these values; first attempt:

 

    <?xml version="1.0" encoding="utf-8"?>

    <s:application xmlns:fx="http://ns.adobe.com/mxml/2009"

                   xmlns:s="library://ns.adobe.com/flex/spark"

                   xmlns:mx="library://ns.adobe.com/flex/mx"

                   creationcomplete="creationcompletehandler(event)">

   

        <fx:script>

            <![cdata[

                import flash.net.urlloader;

                 import flash.net.urlrequest;

                import mx.collections.arraycollection;

                import mx.controls.datagrid;

                import mx.controls.datagridclasses.datagridcolumn;

                import mx.events.flexevent;

   

                private var loader:urlloader = new urlloader();

                private var request:urlrequest = new urlrequest("txtdata/products.txt");

   

                protected function creationcompletehandler(event:flexevent):void{

                    loader.addeventlistener(event.complete, loader_complete);

                    loader.load(request);

                }

               

                protected function loader_complete(evt:event):void {

                    //create array of lines loaded .txt file

                    var lines:array = evt.target.data.split(/\n/);

                    var dg:datagrid = new datagrid();

                    var columns:array = [];

                    var dataprovider:arraycollection = new arraycollection();

                   

                    (var i:number = 0; i<lines.length; i++) {

   

                        var line:string = lines[i];

                        //create array of column values each line

                        var linearray:array = line.split(/\t/);

                       

                        for(var j:number = 0; j<linearray.length; j++){

                            var prop:string = linearray[j];

                            if(i==0){

                                    //if first line of array, create column , assign value of prop datafield

                                var c:datagridcolumn = new datagridcolumn(prop);

                                c.datafield = prop;

                                columns.push(c);

                            }

                            else{

                                    //create new object , add dataprovider

                                var dataobject:object = {sector: linearray[0], section: linearray[1], family: linearray[2], code: linearray[3], brand: linearray[4], image: linearray[5], description: linearray[6], quantity: linearray[7], price: linearray[8] };

                                dataprovider.additem(dataobject);

                            }

                        }

                    }

                    //assign columns datagrid

                    dg.columns = columns;

                    //assign dataprovider datagrid

                    dg.dataprovider = dataprovider;

                    //add datagrid stage

                    this.addelement(dg);

                }

            ]]>

        </fx:script>

   

    </s:application>

 

i able create columns , datafields dinamically, instead wasn't able data provider items (see line below):

 

    var dataobject:object = {sector: linearray[0], section: linearray[1], family: linearray[2], code: linearray[3], brand: linearray[4], image: linearray[5], description: linearray[6], quantity: linearray[7], price: linearray[8] };

 

how can create these values dinamically (without knowing columns number , datafield names)?

 

thanks in advance

use:

 

if (i==0) {

            (var j:int= 0; j<linearray.length; j++) {

                var prop:string = linearray[j];

                var c:datagridcolumn = new datagridcolumn(prop);

                c.datafield = prop;

                columns.push(c);

            }

        } else {

            var dataobject:object = {};

            for(j=0;j<linearray.length;j++){

                dataobject[columns[j].datafield]=linearray[j];

            }

            dataprovider.additem(dataobject);

        }



More discussions in ActionScript 3


adobe

Comments

Popular posts from this blog

how to devide a circle into equal parts

"Could not fill because there are not enough opaque source pixels" - not solved by any other thread

Why can't I change the billing info for my account?