Actionscript:如何以编程方式编写DataGrid?

时间:2011-08-10 08:59:49

标签: flex actionscript-3 datagrid

我有这个代码,但我试图将其转换为actionscript,所以我渲染这个programaticaly但我失败了。我怎么在AS上写这个?

<mx:DataGrid  allowMultipleSelection="true" id="dg_disk" dataProvider="{people}" height="100%" width="100%" selectedIndex="0" x="50" y="50">
        <mx:columns>
            <mx:DataGridColumn dataField="name"         id="empname"     headerText="EmpName"/>
            <mx:DataGridColumn dataField="title"     id="title" headerText="Title"/>
            <mx:DataGridColumn dataField="description"         id="desc"        headerText="Description"/>
           </mx:DataGridColumn>
        </mx:columns>

    </mx:DataGrid>

5 个答案:

答案 0 :(得分:5)

这是我编写的程序的一部分。 mSessionLogDG是DataGrid(在这种情况下,我们将动态列添加到现有的列中)。

private function createColumns() : void {
            var _columnArray : Array = mSessionLogDG.columns;
            var _initialOffset : uint = _columnArray.length;
            // TODO
            if ( _initialOffset > CreateCommentNamesCommand.MAX_NO_COMMENTS ) return;

            this.mOffset = _initialOffset;

            var _headerLabel : String = '';
            for ( var i : uint = 0; i < CreateCommentNamesCommand.MAX_NO_COMMENTS; i++ )
            {
                var _dataColumn : DataGridColumn = new DataGridColumn();
                _dataColumn.headerText = this.createCommentNamesCommand.labelArray[i];
                _dataColumn.setStyle( "paddingLeft", "3" );
                _dataColumn.setStyle( "textAlign", "left" );
                _dataColumn.setStyle( "customID", i );
                _dataColumn.width = 160;
                _dataColumn.labelFunction = labelFunc;                                      
                _dataColumn.sortCompareFunction = SessionDescriptionXmlVo.mCommentCompareFunctionArray[i];
                _dataColumn.itemRenderer = new ClassFactory( SessionCommentItemRenderer );
                _dataColumn.visible = false;
                _dataColumn.minWidth = 160;
                _columnArray.push( _dataColumn );                   
                mColumnArray[i] = _dataColumn;
            }

            mSessionLogDG.columns = _columnArray;               
        }

答案 1 :(得分:1)

类似的东西:

var empname:DataGridColumn = new DataGridColumn();
empname.dataField = "name";
empname.headerText="EmpName";

var title:DataGridColumn = new DataGridColumn();
…

var desc:DataGridColumn = new DataGridColumn();
…

var dg_disk:DataGrid = new DataGrid();
dg_disk.allowMultipleSelection = true;
dg_disk.percentWidth = 100;
dg_disk.percentHeight = 100;
dg_disk.x = 50;
dg_disk.y = 50;
dg_disk.columns = [empname, title, desc];
dg_disk.dataProvider = people;
dg_disk.selectedIndex = 0;

someContainer.addElement(dg_disk);

答案 2 :(得分:1)

var dg_disk : DataGrid = new DataGrid();
dg_disk.allowMultipleSelection = true;
BindingUtils.bindProperty(this, "people", dg_disk, "dataProvider");
dg_disk.percentHeight = 100;
dg_disk.percentWidth = 100;
dg_disk.selectedIndex = 0;
dg_disk.x = 50;
dg_disk.y = 50;
var empname : DataGridColumn = new DataGridColumn();
empname.dataField = "name";
empname.headerText = "EmpName";
var title : DataGridColumn = new DataGridColumn();
title.dataField = "title";
title.headerText = "Title";
var desc : DataGridColumn = new DataGridColumn();
desc.dataField = "description";
desc.headerText = "Description";
dg_disk.columns = [empname, title, desc];
addChild(dg_disk);

答案 3 :(得分:0)

这样可以解决问题:

private function createDataGrid():void{
    var dg:DataGrid = new DataGrid(); // create your grid
    dg.id="dg_disk"
    dg.percentHeight = 100;
    dg.percentWidth=100;
    dg.x = 50;
    dg.y=50;
    dg.selectedIndex = 0; //set its properties

    dg.columns = createDgColumns(); // Add columns to your grid

    addChild(dg); // Add the grid to the display list
}

private function createDgColumns():Array{
    var columns:Array = [];

    var empColumn:DataGridColumn = new DataGridColumn();
    empColumn.dataField="name";
    empColumn.headerText="EmpName";

    var titleColumn:DataGridColumn = new DataGridColumn();
    titleColumn.dataField="title";
    titleColumn.headerText="Title";

    var descColumn:DataGridColumn = new DataGridColumn();
    descColumn.dataField="description";
    descColumn.headerText="Description";


    columns.push(empColumn);
    columns.push(titleColumn);
    columns.push(descColumn);

    return columns
}

答案 4 :(得分:0)

var myGrid:AdvancedDataGrid = new AdvancedDataGrid();
var adgColumn:AdvancedDataGridColumn = new AdvancedDataGridColumn();
adgColumn.headerText = "Header Text";
adgColumn.dataField = "header_text";
var columnArr:Array = new Array();
columnArr.push(adgColumn);
myGrid.columns = columnArr;

this.addChild(myGrid);