PDA

View Full Version : ActionListener for widgets inside RowExpander



arch_04in
22 Dec 2009, 2:23 AM
Hi All,

I was rendering gxt widgets inside grid rowExpander. I could also attach listeners to widgets (GXT 2.0.1).

Here is what I have done:

I override beforeExpand() method of RowExpander class and render a layoutContainer to the body of expander.


RowExpander expander = new RowExpander()
{
@Override
protected boolean beforeExpand( ModelData model, Element body, El row, int rowIndex )
{
if ( fireEvent( Events.BeforeExpand ) )
{
body.setInnerHTML( "" );
LayoutContainer btnContainer = new LayoutContainer();
Button sampleButton = new Button("Sample");

sampleButton .addListener( Events.OnClick, new Listener<BaseEvent>()
{
@Override
public void handleEvent( BaseEvent be )
{
Window.alert( "Sample Button Action" );
}
});

btnContainer.add( sampleButton , new RowData( 1,-1 ) );
btnContainer.render( body );
btnContainer.layout( true );
return true;
}
return false;
}
};


I have upgraded to GXT 2.1.0 recently. Now I see that no events get fired on widgets (OnClick event for Button in this case) that are inside rowExpander.
Is there any change in rowExpander plugin in this release? How can attach listener to widgets now?

Any help will be appreciated!

sven
22 Dec 2009, 4:39 AM
You are missing GWT basics here. You will need to attach the widget. You need to make sure that it gets detached also, else you have a memoryleak.

arch_04in
22 Dec 2009, 6:55 AM
Yes. I need to do that. But why does not event get fired? Same code was working in older version. Is my approach wrong ?

sven
22 Dec 2009, 7:01 AM
The button is not attached, so it does fire any events.

arch_04in
23 Dec 2009, 5:43 AM
Hi Sven,

That was a typo. I am attaching the button to LayoutContainer and then rendering the container to the body of expanded row. The button gets displayed also when the row is expanded but no event gets fired. I have tried with other gxt widgets also but no luck.
I need to add listeners to button/anchorLink for OnClick event. How should I do this?

Note: This was working in earlier version.

sven
23 Dec 2009, 5:48 AM
You need to attach the layoutcontainer. Youare missing GWT basics of the gwt attach/detach model.

Just putting an element somewhere does not make listener work.

Arno.Nyhm
28 Dec 2009, 8:00 AM
is there some solution code available?

i guess this line is wrong:
btnContainer.render( body );

sven
28 Dec 2009, 8:03 AM
No that line is correct. But that only adds elements, not listeners.


ComponentHelper.doAttach(btnContainer);

That will attach it. But you need to detach it also, else you have a memoryleak.

Arno.Nyhm
28 Dec 2009, 8:16 AM
thanks :-)

but if i look in this example into the RowExpander, ColumnConfig, BaseObservable i dont find a hint where i need to place the doDetach.

if i search the sources with ComponentHelper.do*tach in all *.java files then i found some places - but this components all are extending the BoxComponent.

but rowexpander dont inherit from the BoxComponent.

where is the best place for a doDetach?

sven
28 Dec 2009, 8:18 AM
This is not a GXT question. It is a basic gwt question. When the parent gets detached, you alos need to detach the children.

So when you detach the grid, you need to detach it.

Arno.Nyhm
28 Dec 2009, 8:45 AM
ok. but to put this in a single ExtendedRowExpander looks impossible.

i found in Component an addAttachable but this is protected :-( otherwise this can be used for the doDetach (if this ExtendedRowExpander then also implements the interface ComponentAttachable)

sven
28 Dec 2009, 8:55 AM
There are detach and attach events fired by the component.

Arno.Nyhm
28 Dec 2009, 9:03 AM
thanks :-) i miss this point :-)

so i can listen for Events.Detach on the Grid to detach my own elements.

and the reference to the grid i get setted with the init()
:-)

siberian
28 Dec 2009, 4:30 PM
Arno, could you post the code to your solution so that we can see how you handled this?

Tx!
John-

albertogiantin
29 Mar 2010, 1:30 AM
Any code for this solution?

albertogiantin
29 Mar 2010, 2:53 AM
Could be this a valid solution?



public class ExtendedRowExpander extends RowExpander {

private Widget contentWidget;
private LayoutContainer contentContainer;

public ExtendedRowExpander(Widget contentWidget) {
super();
this.contentWidget = contentWidget;
}


@Override
protected boolean beforeExpand(ModelData model, Element body, El row, int rowIndex) {
if ( fireEvent( Events.BeforeExpand ) )
{
if(contentWidget != null) {
body.setInnerHTML( "" );
contentContainer = new LayoutContainer();
contentContainer.add( contentWidget , new RowData( 1,-1 ) );
contentContainer.render( body );
contentContainer.layout( true );
if(!contentContainer.isAttached()) {
ComponentHelper.doAttach(contentContainer);
}
this.grid.addListener(Events.Detach, new Listener<BaseEvent>(){
public void handleEvent(BaseEvent be) {
if(contentContainer.isAttached()) {
ComponentHelper.doDetach(contentContainer);
}
}
});
}
return true;
}
return false;
}



public Widget getContentWidget() {
return contentWidget;
}


public void setContentWidget(Widget contentWidget) {
this.contentWidget = contentWidget;
}

}