0

我有一个从 Excel 导出的制表符分隔的 .txt 文件,如下所示:

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

在这种情况下,我有九列,但该方法应该适用于任意数量的列。我想从这些值动态地创建一个 dataGrid ;这是我的第一次尝试:

<?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 from loaded .txt file
                var lines:Array = evt.target.data.split(/\n/);
                var dg:DataGrid = new DataGrid();
                var columns:Array = [];
                var dataProvider:ArrayCollection = new ArrayCollection();

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

                    var line:String = lines[i];
                    //create array of column values from 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 and assign value of prop to dataField 
                            var c:DataGridColumn = new DataGridColumn(prop);
                            c.dataField = prop;
                            columns.push(c);
                        }
                        else{
                                //create new object and add it to 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 to dataGrid
                dg.columns = columns;
                //assign dataProvider to dataGrid
                dg.dataProvider = dataProvider;
                //add dataGrid to stage
                this.addElement(dg);
            }
        ]]>
    </fx:Script>

</s:Application>

我能够动态地创建列和数据字段,但我无法为数据提供者项目这样做(见下一行):

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] };

如何动态创建这些值(不知道列号和数据字段名称)?

提前致谢

4

1 回答 1

0

您的解决方案几乎是好的。请参阅下面的代码,希望对您有所帮助。

在此处输入图像描述

//应用程序

<?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" 
           minWidth="955" minHeight="600" 
           creationComplete="creationCompleteHandler(event)">
<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.controls.DataGrid;
        import mx.controls.dataGridClasses.DataGridColumn;
        import mx.events.FlexEvent;
        import mx.utils.ObjectUtil;

        private var loader:URLLoader = new URLLoader();
        private var request:URLRequest = new URLRequest("com/dgtextfile/products.txt");

        protected function creationCompleteHandler(event:FlexEvent):void
        {
            loader.addEventListener(Event.COMPLETE, loader_complete);
            loader.load(request);
        }

        protected function loader_complete(evt:Event):void 
        {
            var lines:Array = evt.target.data.split(/\n/);
            var dg:DataGrid = new DataGrid();
            var columns:Array = [];
            var dataProvider:ArrayCollection = new ArrayCollection();
            var propArray:Array;

            for (var i:Number = 0; i<lines.length; i++) 
            {
                var line:String = lines[i];

                var lineArray:Array = line.split(/\t/);

                var j:Number;

                if (i == 0)
                {
                    propArray = ObjectUtil.copy(lineArray) as Array;

                    for (j = 0; j < propArray.length; j++) 
                    {
                        var c:DataGridColumn = new DataGridColumn(propArray[j]);
                        columns.push(c);
                    }
                }
                else
                {
                    var obj:Object = new Object();

                    for(j = 0; j < lineArray.length; j++)
                    {
                        obj[propArray[j]] = lineArray[j];
                    }

                    dataProvider.addItem(obj);
                }
            }

            dg.columns = columns;
            dg.dataProvider = dataProvider;

            vgMain.addElement(dg);
        }
    ]]>
</fx:Script>

<s:VGroup id="vgMain" x="20" y="20"/>
</s:Application>
于 2013-07-04T09:20:33.363 回答