PDA

View Full Version : Dynamic addition to a list store



eugenparaschiv
7 May 2009, 11:09 PM
I have found a bug when adding elements to a list store. The bug is also submitted here:
http://extjs.com/forum/showthread.php?t=67443

I'm having problems adding elements dynamically to a list; I'm using a simple ListView and I want to be able to add more items to it after render.
Just to go a little into details - I'm looking at the addition flow:
FIRST ADD CALL (store.add( firstModel );)
- the item gets added into the store of the ListView
- the StoreListener (of the ListView) gets triggered
- the onAdd method (from ListView) gets called (but it seems not to actually add the item the the 'all' field (which ends up with size 0 after the addition))
- done
SECOND ADD CALL
- the call now fails in the onAdd method (ListView) - actually in the ArrayList.addAll method which gets called with wrong arguments

The code:


public class DnukHome implements EntryPoint{
public void onModuleLoad(){
final RootPanel rootPanel = RootPanel.get();
final LayoutContainer lc = new LayoutContainer();
lc.setWidth( 700 );
lc.setLayout( new RowLayout( Orientation.VERTICAL ) );
rootPanel.add( lc );

// create a simple model
final BaseModel firstModel = new BaseModel();
firstModel.set( "author", "Current User" );
firstModel.set( "date", "May 04, 2009" );
firstModel.set( "text", "sample text" );

// create, configure and add the list
final ListStore< BaseModel > store = new ListStore< BaseModel >();
final ListView< BaseModel > list = new ListView< BaseModel >( store );
list.setSelectionModel( null );
list.setTemplate( this.createTemplate() );

store.add( firstModel );
store.add( firstModel ); // this fails

lc.add( list );

}

private final native String createTemplate() /*-{
return [
'<tpl for=".">',
'<div>',
'<span><a href="#"><b>{author}</b></a></span> <span>- {date}</span>',
'<p>{text}</p>',
'</div>',
'</tpl>'].join("");
}-*/;
}


Details aside, am I doing something wrong?

MihaiMese
8 May 2009, 1:07 AM
I would also like to know the answer to this as I need to dynamically add models to a store, in a ListView, so if someone knows how to solve this, a solution would be much appreciated.

sven
8 May 2009, 5:36 AM
There is an issue in your code.


list.setItemSelector("div");

eugenparaschiv
8 May 2009, 7:32 AM
Cool fix, I have to say.
Can you please explain why the default value was no good and what this one does?
Thanks for the solution.
Eugen.

sven
8 May 2009, 7:41 AM
Your template uses "div" is itemSelector. So you have to set it.