Skip to main content

Ajax request+onclick

9 replies [Last post]
gabox01
Offline
Joined: 2007-04-22
Points: 0

Hi,

I'm trying to attach ajax event via code like this:

myButton.setOnclick("jsf.ajax.request(this, event, {execute: this.id, render: 'dataTable'}); return false;");

Then clicking the button does nothing.

The generated code is:

It might worth mentioning that the button button that should fire the ajax request is acutally inside the component that should be rendered wich is 'dataTable'.

Any idea?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
gabox01
Offline
Joined: 2007-04-22
Points: 0

I will trim down my production code as narrow as possible, and post that code.

Jim:

I doing it via code, because the button is inside a datatable, and it is a kind of datatable, that not only the rows are dynamic but the columns also. I couldn't figure out how to build such datatable via markup.

Jim Driscoll

On 7/28/09 9:17 AM, webtier@javadesktop.org wrote:
> I will trim down my production code as narrow as possible, and post that code.
>
> Jim:
>
> I doing it via code, because the button is inside a datatable, and it is a kind of datatable, that not only the rows are dynamic but the columns also. I couldn't figure out how to build such datatable via markup.

Ah, that sounds like a good reason. Lots of pain to make dyanamic
components though, as you're finding.

Jim

---------------------------------------------------------------------
To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: webtier-help@glassfish.dev.java.net

gabox01
Offline
Joined: 2007-04-22
Points: 0

[b]xhtml[/b]:









[b]bean[/b]:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIColumn;
import javax.faces.component.UIData;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class TestBean implements Serializable {

private static final long serialVersionUID = -7299773608161438216L;

private UIData dataTable;

private List numbers = new ArrayList();

public List getNumbers() {
return numbers;
}

public UIData getDataTable() {
return dataTable;
}

public void add() {
numbers.add(4);
System.out.println("add");
}

@PostConstruct
public void init() {
dataTable = new HtmlDataTable();
UIColumn col = null;

numbers.add(1);
numbers.add(2);
numbers.add(3);

col = new UIColumn();
dataTable.getChildren().add(col);

HtmlCommandButton numButton = new HtmlCommandButton();
numButton.setValueExpression("value", FacesContext.getCurrentInstance().getApplication().getExpressionFactory().createValueExpression(FacesContext.getCurrentInstance().getELContext(), "#{number}", String.class));
numButton.setActionExpression(FacesContext.getCurrentInstance().getApplication().getExpressionFactory().createMethodExpression(FacesContext.getCurrentInstance().getELContext(), "#{testBean.add}", String.class, new Class[] {}));
numButton.setOnclick("jsf.ajax.request(this, event, {execute: this.id, render: 'dataTable'}); return false;");
numButton.setValue("add");

col.getChildren().add(numButton);

}

}

Jim Driscoll

Well, I'm able to say that the ajax stuff is working perfectly.

There's at least one problem with your code, though, that I can see.

I'm not really an expert, but I can tell you that you shouldn't bind to
anything that isn't request scoped - among other problems, there's
thread safety to look out for - UIComponent isn't thread safe.

Also, I'm not really clear as to what you think the ajax call should be
updating, based on this small example - you don't actually have it
updating anything, so no state changes on the server side. But yes,
this example works perfectly, updating the datatable via ajax (to have
the same value as before, but updating nonetheless).

Jim

On 7/29/09 10:17 AM, webtier@javadesktop.org wrote:
> [b]xhtml[/b]:
>
>
>
>
>
>

>
>
>
>
>
>
>

>
>

>
>

>
>
> [b]bean[/b]:
>
>
>
> import java.io.Serializable;
> import java.util.ArrayList;
> import java.util.List;
>
> import javax.annotation.PostConstruct;
> import javax.faces.bean.ManagedBean;
> import javax.faces.bean.SessionScoped;
> import javax.faces.component.UIColumn;
> import javax.faces.component.UIData;
> import javax.faces.component.html.HtmlCommandButton;
> import javax.faces.component.html.HtmlDataTable;
> import javax.faces.context.FacesContext;
>
> @ManagedBean
> @SessionScoped
> public class TestBean implements Serializable {
>
> private static final long serialVersionUID = -7299773608161438216L;
>
> private UIData dataTable;
>
> private List numbers = new ArrayList();
>
> public List getNumbers() {
> return numbers;
> }
>
> public UIData getDataTable() {
> return dataTable;
> }
>
> public void add() {
> numbers.add(4);
> System.out.println("add");
> }
>
> @PostConstruct
> public void init() {
> dataTable = new HtmlDataTable();
> UIColumn col = null;
>
> numbers.add(1);
> numbers.add(2);
> numbers.add(3);
>
> col = new UIColumn();
> dataTable.getChildren().add(col);
>
> HtmlCommandButton numButton = new HtmlCommandButton();
> numButton.setValueExpression("value", FacesContext.getCurrentInstance().getApplication().getExpressionFactory().createValueExpression(FacesContext.getCurrentInstance().getELContext(), "#{number}", String.class));
> numButton.setActionExpression(FacesContext.getCurrentInstance().getApplication().getExpressionFactory().createMethodExpression(FacesContext.getCurrentInstance().getELContext(), "#{testBean.add}", String.class, new Class[] {}));
> numButton.setOnclick("jsf.ajax.request(this, event, {execute: this.id, render: 'dataTable'}); return false;");
> numButton.setValue("add");
>
> col.getChildren().add(numButton);
>
> }
>
> }
> [Message sent by forum member 'gabox01' (gabox01)]
>
> http://forums.java.net/jive/thread.jspa?messageID=358097
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: webtier-help@glassfish.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: webtier-help@glassfish.dev.java.net

gabox01
Offline
Joined: 2007-04-22
Points: 0

It works for you??

Well, not for me.

If i comment out the line:

numButton.setOnclick("jsf.ajax.request(this, event, {execute: this.id, render: 'dataTable'}); return false;");

then it makes a submit, and a new button appears as expected.
If the line is on, there is no call to the serverside. At least i should see an 'add' message, but it looks like add() is not invoked.

Message was edited by: gabox01

Jim Driscoll

Yes, it works for me.

Of course, I'm using the latest version (nightly) of JSF, as well as the
latest version of GlassFish.

There were problems with tables and IE in older JSF versions - so that
may be one problem. Could you try it with the nightly?

Also, to repeat my question: What are you seeing with Firebug?

Jim

On 7/30/09 12:45 AM, webtier@javadesktop.org wrote:
> It works for you??
>
> Well, not for me.
>
> If i comment out the line:
>
> numButton.setOnclick("jsf.ajax.request(this, event, {execute: this.id, render: 'dataTable'}); return false;");
>
> then it makes a submit, and a new button appears as expected.
> If the this line is there, there is no call to the serverside. At least i should see an 'add' message, but it looks like add() is not invoked.
> [Message sent by forum member 'gabox01' (gabox01)]
>
> http://forums.java.net/jive/thread.jspa?messageID=358183
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: webtier-help@glassfish.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: webtier-help@glassfish.dev.java.net

Jim Driscoll

There's a lot of stuff going on in there - it's chaining. Why? What
else are you doing?

It's unlikely it's "doing nothing". Have you tried firebug? You could
be throwing an exception (unlikely, but possible, I suppose).

Also, why add this programmatically, instead of in the page itself? Not
that that shouldn't work, but I'm always curious when folks do stuff
like that.

Jim

On 7/28/09 3:33 AM, webtier@javadesktop.org wrote:
> Hi,
>
> I'm trying to attach ajax event via code like this:
>
> myButton.setOnclick("jsf.ajax.request(this, event, {execute: this.id, render: 'dataTable'}); return false;");
>
> Then clicking the button does nothing.
>
> The generated code is:
>
>
>
> It might worth mentioning that the button button that should fire the ajax request is acutally inside the component that should be rendered wich is 'dataTable'.
>
> Any idea?
> [Message sent by forum member 'gabox01' (gabox01)]
>
> http://forums.java.net/jive/thread.jspa?messageID=357866
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: webtier-help@glassfish.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: webtier-help@glassfish.dev.java.net

rogerk
Offline
Joined: 2004-05-06
Points: 0

Usually folks do stuff programmatically with components when they want to dynamically create components on the fly - even a table. Not sure if that is what your intended use of your approach is.

rogerk
Offline
Joined: 2004-05-06
Points: 0

So it looks like you are using the programmatic approach to creating your controls.
Can you provide more code to your sample?