0

几个小时前,我询问了如何创建自定义组件(textInput 和标签组件并创建了组件定义),有了您的回答,我现在可以做到。

问题 2:我想在 datagrid 列中使用该组件,以便用户可以在 textInput 中键入一个值,这将反过来更新底层数据提供程序。我知道我应该使用 cellrenderer,就像我使用复选框列所做的那样(也在网络上的帮助下),但在这个阶段我只是把头发拉出来。请帮忙。

4

1 回答 1

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 {
        }

    }
}

代码主要来自这篇 devnet文章。

它工作正常,因为它是可编辑的。

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

如果 DataGrid 有点臃肿,并且您想使用组件定义或更简单的东西。我想您可以使用 List 组合解决方案并使用 styles 设置自定义 cellRenderer。我猜自定义剪辑被用作tweenlite页面上插件列表中的单元格渲染器。

HTH,乔治

于 2010-01-09T03:11:14.650 回答