You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Sean Schofield <se...@gmail.com> on 2005/01/04 20:56:10 UTC

Can you add a component to the tree after a POST?

I was wondering how if its possible with JSF to add a component to the
tree via as the result of a POST.

A simple example: you have a list of phone numbers and you have a
wizard on the page that allows them to add another one.  How would
Faces handle something like this?  In the typical examples, the user
is editing a form with exisitng elements that are known at design
time.  I guess I'd be most interested in adding to an existing list as
that would be the only case where I wouldn't know all of the data
ahead of time.

My guess is that something is possible but that it wouldn't be the
easiest thing to accomplish.  Even if you are able to create a new
component and add it to the tree, it seems there would be an issue
with binding that data to the model.

I'm not an expert on faces (yet) but I wanted to get started thinking
about this issue.  Does anyone have any ideas on how I could approach
this?  Maybe I would need to extend UIData or something?

sean

Re: Can you add a component to the tree after a POST?

Posted by Martin Marinschek <ma...@gmail.com>.
by the way - the new MyFaces examples show something like that in
practice (just the UIData approach, not the one with actually adding
components; I would say that more often the UIData approach will be
used):

goto: http://www.irian.at/myfaces/

examples>components>master/detail example

click on edit all countries; now you can click on "Add New Country" to
add a new value to the list - or delete one...

regards,

Martin


On Tue, 4 Jan 2005 13:54:14 -0800, Craig McClanahan <cr...@gmail.com> wrote:
> On Tue, 4 Jan 2005 14:56:10 -0500, Sean Schofield
> <se...@gmail.com> wrote:
> > I was wondering how if its possible with JSF to add a component to the
> > tree via as the result of a POST.
> 
> Yes, you can make arbitrary changes to the components in the tree in
> the event handler to a POST.  However, if you are using JSP they will
> only show up if the added components are inside a component whose
> rendersChildren property is set to true (so that the parent component
> takes responsibility for actually doing the rendering.
> 
> >
> > A simple example: you have a list of phone numbers and you have a
> > wizard on the page that allows them to add another one.  How would
> > Faces handle something like this?  In the typical examples, the user
> > is editing a form with exisitng elements that are known at design
> > time.  I guess I'd be most interested in adding to an existing list as
> > that would be the only case where I wouldn't know all of the data
> > ahead of time.
> 
> There's a couple of different approaches that could be used.
> 
> If your list is inside a UIData component (such as <h:dataTable>),
> simply add the new object to the list that is bound to the "value"
> property.  Assuming you haven't set the "rows" property to limit how
> many rows are rendered, the next rendering of the component will pick
> them all up.  You don't need to mess with the component tree at all,
> because UIData iterates over all its column children, once per row,
> for you.
> 
> if you are constructing your presentation out of individual
> components, then you'd just add them as children of the appropriate
> parent, set their values, and then ensure that you're going to render
> the same page again.
> 
> >
> > My guess is that something is possible but that it wouldn't be the
> > easiest thing to accomplish.  Even if you are able to create a new
> > component and add it to the tree, it seems there would be an issue
> > with binding that data to the model.
> 
> The binding is actually pretty easy -- just create a new ValueBinding
> instance (via Application.createValueBinding()) and then update the
> component with something like:
> 
>   component.setValueBinding("value", vb);
> 
> >
> > I'm not an expert on faces (yet) but I wanted to get started thinking
> > about this issue.  Does anyone have any ideas on how I could approach
> > this?  Maybe I would need to extend UIData or something?
> >
> > sean
> >
> 
> Craig
>

Re: Can you add a component to the tree after a POST?

Posted by Craig McClanahan <cr...@gmail.com>.
On Tue, 4 Jan 2005 14:56:10 -0500, Sean Schofield
<se...@gmail.com> wrote:
> I was wondering how if its possible with JSF to add a component to the
> tree via as the result of a POST.

Yes, you can make arbitrary changes to the components in the tree in
the event handler to a POST.  However, if you are using JSP they will
only show up if the added components are inside a component whose
rendersChildren property is set to true (so that the parent component
takes responsibility for actually doing the rendering.

> 
> A simple example: you have a list of phone numbers and you have a
> wizard on the page that allows them to add another one.  How would
> Faces handle something like this?  In the typical examples, the user
> is editing a form with exisitng elements that are known at design
> time.  I guess I'd be most interested in adding to an existing list as
> that would be the only case where I wouldn't know all of the data
> ahead of time.

There's a couple of different approaches that could be used.

If your list is inside a UIData component (such as <h:dataTable>),
simply add the new object to the list that is bound to the "value"
property.  Assuming you haven't set the "rows" property to limit how
many rows are rendered, the next rendering of the component will pick
them all up.  You don't need to mess with the component tree at all,
because UIData iterates over all its column children, once per row,
for you.

if you are constructing your presentation out of individual
components, then you'd just add them as children of the appropriate
parent, set their values, and then ensure that you're going to render
the same page again.

> 
> My guess is that something is possible but that it wouldn't be the
> easiest thing to accomplish.  Even if you are able to create a new
> component and add it to the tree, it seems there would be an issue
> with binding that data to the model.

The binding is actually pretty easy -- just create a new ValueBinding
instance (via Application.createValueBinding()) and then update the
component with something like:

  component.setValueBinding("value", vb);

> 
> I'm not an expert on faces (yet) but I wanted to get started thinking
> about this issue.  Does anyone have any ideas on how I could approach
> this?  Maybe I would need to extend UIData or something?
> 
> sean
> 

Craig