PDA

View Full Version : datagrid invalidateList() v.s. ListEvent.ITEM_ROLL_OVER, possible bugs


yial2
12-24-2008, 10:59 PM
Hi eveybody,
I have a problem when trying to change datagrid's dataprovider then call invalidateList() to refresh inside a onItemRollOver function


import mx.events.ListEvent;
import mx.controls.DataGrid;

private var dg:DataGrid;
private var ar:Array = [
{c1:'Item 1A', c2:'Item 1B'},
{c1:'Item 2A', c2:'Item 2B'},
{c1:'Item 3A', c2:'Item 3B'}];

private function onInit():void
{
dg = new DataGrid();

dg.dataProvider = ar;

dg.addEventListener(ListEvent.ITEM_ROLL_OVER, onItemRollOver);

this.addChild(dg);
}

private function onItemRollOver(event:ListEvent):void
{
event.itemRenderer.data.c1 = "change " + event.rowIndex.toString();
(event.target as DataGrid).invalidateList();
}


The problem is that when I call invalidateList on itemrollover, the datagrid rolloverColor will no longer work. Has anybody experienced the same problem before? Is this a bug or it is just the way datagrid work? Any work around?

Thanks in advance!! I have been trying to find a way to work around this problem for a week without any success.

drkstr
12-28-2008, 04:18 AM
It's not a bug, invalidateList() completely resets the state so this would be expected behavior.

If you fill your data provider with strongly typed data models and you won't have to call invalidateList().

YourDataModel(event.itemRenderer.data).c1 = "change";


Best Regards,
~Aaron

yial2
12-29-2008, 07:55 PM
Hi drkstr,
Thank you for your reply. Is it possible for you to show me how to contruct a strongly typed data model in this example? As you can see, I have a pair of string(c1 and c2) as the data source. How do I contruct a strongly type data model and feed them to dataprovider? Thanks again.

drkstr
12-29-2008, 09:00 PM
Well I use a code generator I wrote to generate my model code, so this may be a bit over kill for your purposes. But this is technically the most efficient after it's compiled.

package pkg.models {

import flash.events.EventDispatcher;
import flash.events.Event;


public class DataModel extends EventDispatcher {

public function DataModel( memento:Object=null ) {
if( memento != null ) {
setMemento(memento);
}
}


////
//// --== Private Vars ==--
////

private var _c1:String;
private var _c2:String;


////
//// --== Public Vars ==--
////

[Bindable(event='c1Change')]
[Bindable(event='propertyChange')]
public function get c1(): String {
return _c1;
}

public function set c1( data:String ): void {
_c1 = data;
dispatchEvent( new Event('c1Change') );
}

[Bindable(event='c2Change')]
[Bindable(event='propertyChange')]
public function get c2(): String {
return _c2;
}

public function set c2( data:String ): void {
_c2 = data;
dispatchEvent( new Event('c2Change') );
}


////
//// --== Public Functions ==--
////

public function setMemento( memento:Object): void {

_c1 = memento.c1;
_c2 = memento.c2;

dispatchEvent( new Event('propertyChange') );
}

public function getMemento(): Object {
var memento:Object = new Object();

memento.c1 = _c1;
memento.c2 = _c2;

return memento;
}
}
}

var dataModel:DataModel = new DataModel( {c1:"C1", c2:"C2"} );
yourDataProvider.addItem(dataModel);
//technically you should add to an array then push all at once to your data provider


Best Regards,
~Aaron