View Full Version : [AS3] comboBox as itemEditor
_zoidberg_
03-03-2009, 09:07 PM
hey guys,
i want use a comboBox in a datagrid as itemEditor while still using the default textfield as cellrenderer (like here: http://blog.flexmonkeypatches.com/2008/02/18/simple-datagrid-combobox-as-item-editor-example/ - you have to scroll down a little bit. this example is written in flex, i need it in common as3.).
when i click into a datagrid cell, the combobox appears. so far so good. but if close the dropdown list, i'm getting these error messages:
TypeError: Error #1009: Der Zugriff auf eine Eigenschaft oder eine Methode eines null-Objektverweises ist nicht möglich.
at fl.controls::ComboBox/close()
at classes.trainingSchedule::ComboBoxCellRenderer/close()
at fl.controls::ComboBox/fl.controls:ComboBox::onListChange()
at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunctio n()
at flash.events::EventDispatcher/dispatchEvent()
at fl.controls::SelectableList/fl.controls:SelectableList::handleCellRendererClic k()
i tried to solve this problem for hours and days without any result.
could someone help me?
thanks
zoidberg
stereotyp
03-11-2009, 10:51 PM
hey, i had exactly the same problem bothering me the last few days.. and i know as well as you that theres really no tutorial online.
but finally i got it made! :) its pretty ugly but it seems to work..
the problem is that in the close function the combobox uses the stage property but the stage object is null at that time (dont ask me why).
so i've overwritten the close function and now it works, the trick is to give the class a owner parameter (the datagrid) and then call the stage from there. i had to overwrite the listchange function, too in order to update the datagrid after selecting an item from the combobox.
here's my class:
package {
import fl.controls.ComboBox;
import fl.controls.DataGrid;
import fl.data.DataProvider;
import fl.controls.listClasses.ICellRenderer;
import fl.controls.listClasses.ListData;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;
import fl.core.InvalidationType;
public class ComboCellRenderer extends ComboBox implements ICellRenderer {
private var _listData:ListData;
private var _data:Object;
private var _selected:Boolean;
private var _owner:DataGrid;
public function ComboCellRenderer(owner:DataGrid) {
_owner = owner;
}
override public function close():void {
highlightCell();
highlightedCell = -1;
if (! isOpen) { return; }
dispatchEvent(new Event(Event.CLOSE));
_owner.stage.removeEventListener(MouseEvent.MOUSE_ DOWN, onStageClick);
isOpen = false;
_owner.stage.removeChild(list);
}
override protected function onListChange(event:Event):void {
editableValue = null;
dispatchEvent(event);
invalidate(InvalidationType.SELECTED);
if (isKeyDown) { return; }
var itm = _owner.editedItemPosition;
var col = _owner.getColumnAt(itm.columnIndex);
_owner.editField(itm.rowIndex, col.dataField, selectedLabel);
close();
}
public function set data(d:Object):void {
_data = d;
}
public function get data():Object {
return _data;
}
public function set listData(ld:ListData):void {
_listData = ld;
}
public function get listData():ListData {
return _listData;
}
public function set selected(s:Boolean):void {
_selected = s;
}
public function get selected():Boolean {
return _selected;
}
public function setMouseState(state:String):void {
}
}
}
And this is the Code to embed it:
var comboColumn = new DataGridColumn("Anrede");
var combo = comboColumn.itemEditor = new ComboCellRenderer(dg);
var anredeDP = new DataProvider();
anredeDP.addItem( { label: "Herr" } );
anredeDP.addItem( { label: "Frau" } );
combo.dataProvider = anredeDP;
dg.addColumn(comboColumn);
i'm pretty sure the code is quite messy but its the best i could do. one problem is that when i try to tab through the datagrid items i get stuck whenever there is a column with a combobox. maybe you have an idea to get rid of that..
hope i could help you,
Henner
Mahalo
04-12-2010, 04:52 PM
And how to attach combo showing the correct item?
For example, in a datagrid cell there's a '3'. And the combo has the items 1,2,3,4,5. Ok? When I click on the cell, the combo appears showing '1' and not '3'.
Hummm, sorry, I'am spanish and I speak english very bad.
Saludos!
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.