Datagrid Cell使用自定义组件进行渲染

时间:2010-01-06 15:20:31

标签: actionscript-3 flash-cs4 flash-v3-components

几个小时前,我已经问过如何创建一个自定义组件(textInput和标签组件并创建了一个组件定义),现在我可以用你的答案做到这一点。

问题2:我想在datagrid列中使用该组件,以便用户可以在textInput中键入一个值,该值又将更新基础数据提供者。 我知道我应该像使用复选框一样使用一个cellrenderer(也在网上有帮助),但在这个阶段我只是把我的头发拉出来。 请帮忙。

1 个答案:

答案 0 :(得分:0)

这可能看起来很乱,因为它是一个修改过的例子。

确保您想要尝试使用fla的库中的DataGrid,Label和TextInput组件:

// Import the required component classes.
import fl.controls.DataGrid;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.data.DataProvider;
//get some data ready, notice data and label
var dp:DataProvider = new DataProvider();
for(var i:int = 0 ; i < 7; i++)
    dp.addItem({data:'input '+(i+1),label:'label '+(i+1), title:"item " + (i+1)});
var dataCol:DataGridColumn = new DataGridColumn("data");
dataCol.cellRenderer = CustomCell;
var titleCol:DataGridColumn = new DataGridColumn("title");

var myDataGrid:DataGrid = new DataGrid();
myDataGrid.addColumn(dataCol);
myDataGrid.addColumn(titleCol);
myDataGrid.dataProvider = dp;
myDataGrid.rowHeight = 64;
myDataGrid.width = 500;
myDataGrid.rowCount = dp.length - 1;
myDataGrid.move(10, 10);
myDataGrid.editable = true;
addChild(myDataGrid);

CustomCell类看起来像这样:

package {
    // Import the required component classes.
    import fl.controls.listClasses.ICellRenderer;
    import fl.controls.listClasses.ListData;
    import fl.controls.Label;
    import fl.controls.TextInput;
    import fl.core.InvalidationType;
    import fl.core.UIComponent;
    import fl.data.DataProvider;
    import flash.display.Sprite;
    import flash.events.Event;

    public class CustomCell extends UIComponent implements ICellRenderer {
        protected var _data:Object;
        protected var _listData:ListData;
        protected var _selected:Boolean;
        //the custom components
        private var labelComponent:Label;
        private var inputComponent:TextInput;
        /**
         * Constructor.
         */
        public function CustomCell():void {
            super();
            init();
        }
        /**
         * Draws the Label and TextInput components
         */
        private function init():void{
            labelComponent = new Label();
            labelComponent.autoSize = 'right';
            inputComponent = new TextInput();
            inputComponent.editable = true;

            addChild(labelComponent);
            addChild(inputComponent);
            inputComponent.x = labelComponent.width + 5;//5 pixels distance between components
            inputComponent.drawFocus(true);
        }

        public function get data():Object {
            return _data;
        }
        /** 
         * @private (setter)
         */
        public function set data(value:Object):void {
            _data = value;
            //there's label data, update the label
            if(_data.label) labelComponent.text = _data.label;
            //there's data for the input, update that too
            if(_data.data) inputComponent.text = _data.data;
        }

        public function get listData():ListData {
            return _listData;
        }
        public function set listData(value:ListData):void {
            _listData = value;
            invalidate(InvalidationType.DATA);
            invalidate(InvalidationType.STATE);
        }
        public function get selected():Boolean {
            return _selected;
        }
        public function set selected(value:Boolean):void {
            _selected = value;
            invalidate(InvalidationType.STATE);
        }
        public function setMouseState(state:String):void {
        }

    }
}

代码主要来自this devnet文章。

它可以正常使用,因为它是可编辑的。

解决方案是一个组件类(扩展fl.core.UIComponent的类),实现ICellRender接口,因此可以将其设置为渲染器,并包含Label和TextInput组件。此外,数据将映射到TextInput.text,因此可以轻松编辑。

如果DataGrid有点臃肿,并且您想要使用组件定义或更简单的东西。我想你可以使用List和setting a custom cellRenderer using styles来破解解决方案。 我猜测自定义剪辑在tweenlite页面的插件列表中用作单元格渲染器。

HTH, 乔治

相关问题