You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Daniel Ferreira Castro <df...@gmail.com> on 2009/02/16 13:13:58 UTC

Drop Down Box - Ajax Behaviour

I read the examples of Ajax Forms and I am trying to adapt it to my needs.
I have a form with a input text, a drop down box, a feedback panel and a
submit button.

My submit is an ajaxButton.
My form setsOutputMarkupId(true);
My dropdown sets OutputMarkupId(true);

When I submit my form it inserts an item on a table.
My intention is after the form submit the dropdown uptades itself, adding
the previously inserted item to its options.

how to do it?

-- 
"Two rules to succeed in life:
1 - don´t tell people everything you know."
--------
We shall go on to the end.
We shall fight in France
We shall fightover the seas and oceans.
We shall fight with growing confidence and growing strength in the air.
We shall defend our island whatever the cost may be
We shall fight on beaches, we shall fight on the landing grounds,
We shall fight in the fields and in the streets,
We shall fight on the hills.
We shall never surrender.
Winston Churchill

Re: Drop Down Box - Ajax Behaviour

Posted by Timo Rantalaiho <Ti...@ri.fi>.
On Thu, 19 Feb 2009, Daniel Ferreira Castro wrote:
> My problem is that After I submit the form, inserting on the bank a new
> Category (Caategoria in Portuguese), the DropDown does not update its values
> list with the inserted one.

That's probably best done with a pull model

  IModel<List<Categoria>> allCategoriesModel = new AbstracReadOnlyModel<List<Categoria>> {
      @Override
      public List<Categoria> getObject() {
          return categoriaDao.getAllCategorias();
      }
  }
  categoriaPai = new DropDownChoice("categoriaPai", categoriaPaiModel, allCategoriesModel, choiceRenderer);                             

If the dropdown values should change when needed, you 
shouldn't just fetch the data on component creation and 
populate the dropdown with it, but have the model get 
fresh data on every render.

Best wishes,
Timo

-- 
Timo Rantalaiho           
Reaktor Innovations Oy    <URL: http://www.ri.fi/ >

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Drop Down Box - Ajax Behaviour

Posted by Daniel Ferreira Castro <df...@gmail.com>.
The method getHibernateTransaction is a need :)
Hibernate complains if I try to do a select without an active transaction.
But this is not the case, it is working fine - no runtime errors - so it is
ok at the moment :)

My problem is that After I submit the form, inserting on the bank a new
Category (Caategoria in Portuguese), the DropDown does not update its values
list with the inserted one.
Wierd also is the fact that I tryed to use the AjaxSelfUpdatingTimerBehavior
to update the DropDown from time to time, but the values does not change -
even with the new version inserted on the database.
So, my best guess is that I am doing something wrong...the problem is find
out what.

I tryed to look, always do, the wicket debug console, but didn't show me the
reason of the actual behaviour.


On Thu, Feb 19, 2009 at 1:23 AM, Timo Rantalaiho <Ti...@ri.fi>wrote:

> On Thu, 19 Feb 2009, Timo Rantalaiho wrote:
> > AjaxFormComponentUpdatingBehavior does not submit the whole
> > form -- for that you need to use some *submitting*behavior.
>
> Ah, now I saw that you had probably tried that?
>
> It's pretty hard to investigate what's going on -- you
> could try debugging Form.process() which usually gives a
> pretty good idea of what's happening, or prepare a
> quickstart with a problem and post a link here if you need
> more help.
>
> Best wishes,
> Timo
>
> --
> Timo Rantalaiho
> Reaktor Innovations Oy    <URL: http://www.ri.fi/ >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>


-- 
"Two rules to succeed in life:
1 - don´t tell people everything you know."
--------
We shall go on to the end.
We shall fight in France
We shall fightover the seas and oceans.
We shall fight with growing confidence and growing strength in the air.
We shall defend our island whatever the cost may be
We shall fight on beaches, we shall fight on the landing grounds,
We shall fight in the fields and in the streets,
We shall fight on the hills.
We shall never surrender.
Winston Churchill

Re: Drop Down Box - Ajax Behaviour

Posted by Timo Rantalaiho <Ti...@ri.fi>.
On Thu, 19 Feb 2009, Timo Rantalaiho wrote:
> AjaxFormComponentUpdatingBehavior does not submit the whole
> form -- for that you need to use some *submitting*behavior.

Ah, now I saw that you had probably tried that?

It's pretty hard to investigate what's going on -- you 
could try debugging Form.process() which usually gives a 
pretty good idea of what's happening, or prepare a 
quickstart with a problem and post a link here if you need 
more help.

Best wishes,
Timo

-- 
Timo Rantalaiho           
Reaktor Innovations Oy    <URL: http://www.ri.fi/ >

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Drop Down Box - Ajax Behaviour

Posted by Timo Rantalaiho <Ti...@ri.fi>.
On Wed, 18 Feb 2009, Daniel Ferreira Castro wrote:
> AjaxFormComponentUpdatingBehavior("onchange") and
> AjaxFormComponentUpdatingBehavior("onsubmit") to the DropDown Box that I
> want to be updated after the form post.
> But didn't work yet.

"onsubmit" probably doesn't exist for <select>. Have you 
looked at the ajax debug console (available when running 
Wicket in development mode, e.g. 
-Dwicket.configuration=DEVELOPMENT )?

>         getHibernateTransaction();

That sounds strange, by the way.

> *        //My failed attemp to use the Ajax event to update the form*
>         categoriaForm.getCategoriaPai().add(new
> AjaxFormComponentUpdatingBehavior("onchange") {
>             private static final long serialVersionUID =
> 1998330379948659165L;
> 
>             protected void onUpdate(AjaxRequestTarget target) {
>                 target.addComponent(categoriaForm.getCategoriaPai());
>             }
>         });

You're just updating categoriaPai with the values from the
server, whenever its selection is changed, without ever
submitting them to the server.
AjaxFormComponentUpdatingBehavior does not submit the whole
form -- for that you need to use some *submitting*behavior.
Look at the source code of the different Ajax*Form*Behavior
to see better what they do.

Best wishes,
Timo

-- 
Timo Rantalaiho           
Reaktor Innovations Oy    <URL: http://www.ri.fi/ >

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Drop Down Box - Ajax Behaviour

Posted by Daniel Ferreira Castro <df...@gmail.com>.
I tryied to add the AjaxBehaviour
AjaxFormComponentUpdatingBehavior("onchange") and
AjaxFormComponentUpdatingBehavior("onsubmit") to the DropDown Box that I
want to be updated after the form post.
But didn't work yet.

I am doing this way.
This is the constructor of the Panel that I am using to show the Form.  It
is a AJAX tabbed panel that works fine.

    public CadastroCategoriaPanel(String id) {
        super(id);
        addHeaders();
        getHibernateTransaction();
*        //Gets the list from database to populate the DropDown Box that I
want to be updated by AJAX
*        List<Categoria> categorias = (List<Categoria>)
CategoriaDAO.getAllCategorias(hibernateSession);
*        //The model used by the DropDown and passed to the Form class
constructor
*        optionModel = new CompoundPropertyModel(categorias);
*        //This is the form that has the DropDown Box that I want to be
updated by AJAX
*        final CategoriaForm categoriaForm = new
CategoriaForm("categoriaForm",optionModel);
*        //My failed attemp to use the Ajax event to update the form*
        categoriaForm.getCategoriaPai().add(new
AjaxFormComponentUpdatingBehavior("onchange") {
            private static final long serialVersionUID =
1998330379948659165L;

            protected void onUpdate(AjaxRequestTarget target) {
                target.addComponent(categoriaForm.getCategoriaPai());
            }
        });

*        // Add components to the page
*        add(categoriaForm);
        closeTransaction();
        closeSession();
    }

Please, can anyone help me?

On Mon, Feb 16, 2009 at 7:34 PM, Daniel Ferreira Castro
<df...@gmail.com>wrote:

> *This is my form source code*
>
> package com.jasp.ecommfwk.pages.forms;
>
> import java.io.Serializable;
> import java.util.List;
>
> import org.apache.log4j.Logger;
> import org.apache.wicket.ajax.AjaxRequestTarget;
> import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
> import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
> import org.apache.wicket.ajax.markup.html.form.AjaxButton;
> import org.apache.wicket.markup.html.basic.Label;
> import org.apache.wicket.markup.html.form.DropDownChoice;
> import org.apache.wicket.markup.html.form.Form;
> import org.apache.wicket.markup.html.form.TextField;
> import org.apache.wicket.markup.html.panel.FeedbackPanel;
> import org.apache.wicket.model.CompoundPropertyModel;
> import org.apache.wicket.model.Model;
> import org.apache.wicket.model.StringResourceModel;
> import org.hibernate.Transaction;
> import org.hibernate.classic.Session;
>
> import com.jasp.ecommfwk.util.markup.html.form.CategoriaChoiceRenderer;
> import com.jasp.persistence.hierarchy.Categoria;
> import com.jasp.persistence.hierarchy.dao.CategoriaDAO;
> import com.jasp.persistence.util.HibernateUtil;
>
> @SuppressWarnings("unchecked")
> public class CategoriaForm extends Form<Categoria> implements Serializable
> {
>     private static final long serialVersionUID = -1940617589157076677L;
>
>     private static Logger logger = Logger.getLogger(CategoriaForm.class);
>
>     private Session hibernateSession;
>
>     private transient Transaction hibernateTransaction;
>
>     // Textos i18n carregados aqui
>     private final StringResourceModel labelCategoriaValue = new
> StringResourceModel(
>             "categoria.label", this, null, new Object[] { getLocale() });
>     private final StringResourceModel labelPaiValue = new
> StringResourceModel(
>             "categoria.labelPai", this, null, new Object[] { getLocale()
> });
>
>     // End of  i18n load
>
>     // Declare component's form
>     private DropDownChoice categoriaPai;
>     private TextField nomeCategoria;
>     private Label labelCategoria;
>     private Label labelCategoriaPai;
>     private CategoriaChoiceRenderer choiceRenderer;
>     private AjaxButton cadastrar;
>     private FeedbackPanel feedbackPanel;
>
>     private void inicializaComponentesForm() {
>         categoriaPai = criaCategoriaPaiDropdown();
>         nomeCategoria = criaNomeCategoriaTextField();
>         nomeCategoria.setRequired(true);
>         labelCategoria = new Label("labelCategoria", labelCategoriaValue);
>         labelCategoriaPai = new Label("labelCategoriaPai", labelPaiValue);
>
>         cadastrar = new AjaxButton("cadastrar", this) {
>             private static final long serialVersionUID =
> 6045168066074475539L;
>
>             @Override
>             protected void onSubmit(AjaxRequestTarget target, Form<?> form)
> {
>                 getHibernateTransaction();
>                 int idBanco = Integer.parseInt(categoriaPai.getValue());
>                 Categoria escolhida = null;
>                 Categoria nova = new Categoria();
>                 nova.setNome(nomeCategoria.getValue());
>                 hibernateSession.persist(nova);
>                 if (0 < idBanco) {
>                     escolhida = new Categoria(idBanco);
>                     hibernateSession.clear();
>                     //Seto o pai
>                     nova.setCategoriaPai(escolhida);
>                     hibernateSession.merge(nova);
>                 }
>                 hibernateTransaction.commit();
>                 closeTransaction();
>                 String mensageSucesso = "Categoria " + nova.getNome()
>                         + " cadastrada com sucesso!";
>                 info(mensageSucesso);
>                 target.addComponent(feedbackPanel);
> /*
> This is the part that I am having trouble with.
> The idea is after the form submission the Dropdown categoriaPai  should be
> updated with the recent created item
> */
>                 this.add(new AjaxFormSubmitBehavior("onchange"){
>
>                     @Override
>                     protected void onError(AjaxRequestTarget target) {
>                         // TODO Auto-generated method stub
>
>                     }
>
>                     @Override
>                     protected void onSubmit(AjaxRequestTarget target) {
>                         // TODO Auto-generated method stub
>                         target.addComponent(categoriaPai);
>                     }
>
>                 });
>             }
>
>             @Override
>             protected void onError(AjaxRequestTarget target, Form<?> form)
> {
>                 // repaint the feedback panel so errors are shown
>                 target.addComponent(feedbackPanel);
>             }
>         };
>
>     }
>
>     public CategoriaForm(String id) {
>         super(id);
>         setOutputMarkupId(true);
>         inicializaComponentesForm();
>         feedbackPanel = criaFeedbackPanel();
>         // Adicionando todos os componentes ao form
>         this.add(categoriaPai);
>         this.add(nomeCategoria);
>         this.add(labelCategoria);
>         this.add(labelCategoriaPai);
>         this.add(cadastrar);
>         this.add(feedbackPanel);
>
>     }
> /*
> This is the method used to create the DropDown
> */
>     private DropDownChoice criaCategoriaPaiDropdown() {
>         setOutputMarkupId(true);
>         choiceRenderer = new CategoriaChoiceRenderer();
>         getHibernateTransaction();
>         List<Categoria> data = (List<Categoria>) CategoriaDAO
>                 .getAllCategorias(hibernateSession);
>
>         CompoundPropertyModel categoriaPaiModel = new
> CompoundPropertyModel(data);
>         categoriaPai = new DropDownChoice("categoriaPai",categoriaPaiModel,
> data, choiceRenderer);
>         return categoriaPai;
>     }
>
>     private TextField criaNomeCategoriaTextField() {
>         return new TextField("nomeCategoria", new Model(""));
>     }
>
>     public void setSession(Session session) {
>         this.hibernateSession = session;
>     }
>
>     public DropDownChoice getCategoriaPai() {
>         return categoriaPai;
>     }
>
>     public void setCategoriaPai(DropDownChoice categoriaPai) {
>         this.categoriaPai = categoriaPai;
>     }
>
>     public TextField getNomeCategoria() {
>         return nomeCategoria;
>     }
>
>     public void setNomeCategoria(TextField nomeCategoria) {
>         this.nomeCategoria = nomeCategoria;
>     }
>
>     public StringResourceModel getLabelCategoriaValue() {
>         return labelCategoriaValue;
>     }
>
>     public StringResourceModel getLabelPaiValue() {
>         return labelPaiValue;
>     }
>
>     public Label getLabelCategoria() {
>         return labelCategoria;
>     }
>
>     public Label getLabelCategoriaPai() {
>         return labelCategoriaPai;
>     }
>
>     private Session getHibernateSession() {
>         if (null == hibernateSession || !hibernateSession.isConnected()) {
>             hibernateSession =
> HibernateUtil.getSessionFactory().openSession();
>         }
>         return hibernateSession;
>     }
>
>     public void setHibernateSession(Session hibernateSession) {
>         this.hibernateSession = hibernateSession;
>     }
>
>     private Transaction getHibernateTransaction() {
>         getHibernateSession();
>         if (null == hibernateTransaction ||
> !hibernateTransaction.isActive()) {
>             hibernateTransaction = hibernateSession.beginTransaction();
>         }
>         return hibernateTransaction;
>     }
>
>     public void setHibernateTransaction(Transaction hibernateTransaction) {
>         this.hibernateTransaction = hibernateTransaction;
>     }
>
>     private FeedbackPanel criaFeedbackPanel() {
>         FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
>         feedbackPanel.setOutputMarkupId(true);
>         return feedbackPanel;
>     }
>
>     private void closeTransaction(){
>         closeSession();
>         hibernateTransaction = null;
>     }
>
>     private void closeSession() {
>         if (null != hibernateSession
>                 && (hibernateSession.isConnected() ||
> hibernateSession.isOpen())) {
>             hibernateSession.close();
>         }
>     }
> }
>
>
> *And this is the render used*
>
> package com.jasp.ecommfwk.util.markup.html.form;
>
> import java.util.List;
>
> import org.apache.wicket.markup.html.form.ChoiceRenderer;
>
> import com.jasp.persistence.hierarchy.Categoria;
>
> public class CategoriaChoiceRenderer extends ChoiceRenderer{
>     /**
>      *
>      */
>     private static final long serialVersionUID = -3455900118160934254L;
>
>     @Override
>     public Object getDisplayValue(Object object) {
>         if (object instanceof Categoria) {
>             Categoria cat = (Categoria) object;
>             return cat.getNome();
>         } else {
>             throw new IllegalArgumentException(
>                     "O objeto deve ser uma instancia de Categoria");
>         }
>     }
>
>     @Override
>     public String getIdValue(Object object, int index) {
>         String retorno = null;
>         Categoria cat = null;
>         if (object instanceof Categoria) {
>             cat = (Categoria) object;
>         } else {
>             if(object instanceof List){
>                 List <Categoria> temp = (List<Categoria>) object;
>                 if(temp.size()>0){
>                     cat = temp.get(0);
>                 }else{
>                     cat = new Categoria();
>                 }
>
>             }else{
>                 throw new IllegalArgumentException(
>                 "O objeto deve ser uma instancia de Categoria");
>             }
>         }
>         retorno = String.valueOf(cat.getIdCategoria());
>         return retorno;
>     }
> }
>
>
> --
> "Two rules to succeed in life:
> 1 - don´t tell people everything you know."
> --------
> We shall go on to the end.
> We shall fight in France
> We shall fightover the seas and oceans.
> We shall fight with growing confidence and growing strength in the air.
> We shall defend our island whatever the cost may be
> We shall fight on beaches, we shall fight on the landing grounds,
> We shall fight in the fields and in the streets,
> We shall fight on the hills.
> We shall never surrender.
> Winston Churchill
>



-- 
"Two rules to succeed in life:
1 - don´t tell people everything you know."
--------
We shall go on to the end.
We shall fight in France
We shall fightover the seas and oceans.
We shall fight with growing confidence and growing strength in the air.
We shall defend our island whatever the cost may be
We shall fight on beaches, we shall fight on the landing grounds,
We shall fight in the fields and in the streets,
We shall fight on the hills.
We shall never surrender.
Winston Churchill

Re: Drop Down Box - Ajax Behaviour

Posted by Daniel Ferreira Castro <df...@gmail.com>.
*This is my form source code*

package com.jasp.ecommfwk.pages.forms;

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

import org.apache.log4j.Logger;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.StringResourceModel;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import com.jasp.ecommfwk.util.markup.html.form.CategoriaChoiceRenderer;
import com.jasp.persistence.hierarchy.Categoria;
import com.jasp.persistence.hierarchy.dao.CategoriaDAO;
import com.jasp.persistence.util.HibernateUtil;

@SuppressWarnings("unchecked")
public class CategoriaForm extends Form<Categoria> implements Serializable {
    private static final long serialVersionUID = -1940617589157076677L;

    private static Logger logger = Logger.getLogger(CategoriaForm.class);

    private Session hibernateSession;

    private transient Transaction hibernateTransaction;

    // Textos i18n carregados aqui
    private final StringResourceModel labelCategoriaValue = new
StringResourceModel(
            "categoria.label", this, null, new Object[] { getLocale() });
    private final StringResourceModel labelPaiValue = new
StringResourceModel(
            "categoria.labelPai", this, null, new Object[] { getLocale() });

    // End of  i18n load

    // Declare component's form
    private DropDownChoice categoriaPai;
    private TextField nomeCategoria;
    private Label labelCategoria;
    private Label labelCategoriaPai;
    private CategoriaChoiceRenderer choiceRenderer;
    private AjaxButton cadastrar;
    private FeedbackPanel feedbackPanel;

    private void inicializaComponentesForm() {
        categoriaPai = criaCategoriaPaiDropdown();
        nomeCategoria = criaNomeCategoriaTextField();
        nomeCategoria.setRequired(true);
        labelCategoria = new Label("labelCategoria", labelCategoriaValue);
        labelCategoriaPai = new Label("labelCategoriaPai", labelPaiValue);

        cadastrar = new AjaxButton("cadastrar", this) {
            private static final long serialVersionUID =
6045168066074475539L;

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form)
{
                getHibernateTransaction();
                int idBanco = Integer.parseInt(categoriaPai.getValue());
                Categoria escolhida = null;
                Categoria nova = new Categoria();
                nova.setNome(nomeCategoria.getValue());
                hibernateSession.persist(nova);
                if (0 < idBanco) {
                    escolhida = new Categoria(idBanco);
                    hibernateSession.clear();
                    //Seto o pai
                    nova.setCategoriaPai(escolhida);
                    hibernateSession.merge(nova);
                }
                hibernateTransaction.commit();
                closeTransaction();
                String mensageSucesso = "Categoria " + nova.getNome()
                        + " cadastrada com sucesso!";
                info(mensageSucesso);
                target.addComponent(feedbackPanel);
/*
This is the part that I am having trouble with.
The idea is after the form submission the Dropdown categoriaPai  should be
updated with the recent created item
*/
                this.add(new AjaxFormSubmitBehavior("onchange"){

                    @Override
                    protected void onError(AjaxRequestTarget target) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    protected void onSubmit(AjaxRequestTarget target) {
                        // TODO Auto-generated method stub
                        target.addComponent(categoriaPai);
                    }

                });
            }

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
                // repaint the feedback panel so errors are shown
                target.addComponent(feedbackPanel);
            }
        };

    }

    public CategoriaForm(String id) {
        super(id);
        setOutputMarkupId(true);
        inicializaComponentesForm();
        feedbackPanel = criaFeedbackPanel();
        // Adicionando todos os componentes ao form
        this.add(categoriaPai);
        this.add(nomeCategoria);
        this.add(labelCategoria);
        this.add(labelCategoriaPai);
        this.add(cadastrar);
        this.add(feedbackPanel);

    }
/*
This is the method used to create the DropDown
*/
    private DropDownChoice criaCategoriaPaiDropdown() {
        setOutputMarkupId(true);
        choiceRenderer = new CategoriaChoiceRenderer();
        getHibernateTransaction();
        List<Categoria> data = (List<Categoria>) CategoriaDAO
                .getAllCategorias(hibernateSession);

        CompoundPropertyModel categoriaPaiModel = new
CompoundPropertyModel(data);
        categoriaPai = new DropDownChoice("categoriaPai",categoriaPaiModel,
data, choiceRenderer);
        return categoriaPai;
    }

    private TextField criaNomeCategoriaTextField() {
        return new TextField("nomeCategoria", new Model(""));
    }

    public void setSession(Session session) {
        this.hibernateSession = session;
    }

    public DropDownChoice getCategoriaPai() {
        return categoriaPai;
    }

    public void setCategoriaPai(DropDownChoice categoriaPai) {
        this.categoriaPai = categoriaPai;
    }

    public TextField getNomeCategoria() {
        return nomeCategoria;
    }

    public void setNomeCategoria(TextField nomeCategoria) {
        this.nomeCategoria = nomeCategoria;
    }

    public StringResourceModel getLabelCategoriaValue() {
        return labelCategoriaValue;
    }

    public StringResourceModel getLabelPaiValue() {
        return labelPaiValue;
    }

    public Label getLabelCategoria() {
        return labelCategoria;
    }

    public Label getLabelCategoriaPai() {
        return labelCategoriaPai;
    }

    private Session getHibernateSession() {
        if (null == hibernateSession || !hibernateSession.isConnected()) {
            hibernateSession =
HibernateUtil.getSessionFactory().openSession();
        }
        return hibernateSession;
    }

    public void setHibernateSession(Session hibernateSession) {
        this.hibernateSession = hibernateSession;
    }

    private Transaction getHibernateTransaction() {
        getHibernateSession();
        if (null == hibernateTransaction ||
!hibernateTransaction.isActive()) {
            hibernateTransaction = hibernateSession.beginTransaction();
        }
        return hibernateTransaction;
    }

    public void setHibernateTransaction(Transaction hibernateTransaction) {
        this.hibernateTransaction = hibernateTransaction;
    }

    private FeedbackPanel criaFeedbackPanel() {
        FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
        feedbackPanel.setOutputMarkupId(true);
        return feedbackPanel;
    }

    private void closeTransaction(){
        closeSession();
        hibernateTransaction = null;
    }

    private void closeSession() {
        if (null != hibernateSession
                && (hibernateSession.isConnected() ||
hibernateSession.isOpen())) {
            hibernateSession.close();
        }
    }
}


*And this is the render used*

package com.jasp.ecommfwk.util.markup.html.form;

import java.util.List;

import org.apache.wicket.markup.html.form.ChoiceRenderer;

import com.jasp.persistence.hierarchy.Categoria;

public class CategoriaChoiceRenderer extends ChoiceRenderer{
    /**
     *
     */
    private static final long serialVersionUID = -3455900118160934254L;

    @Override
    public Object getDisplayValue(Object object) {
        if (object instanceof Categoria) {
            Categoria cat = (Categoria) object;
            return cat.getNome();
        } else {
            throw new IllegalArgumentException(
                    "O objeto deve ser uma instancia de Categoria");
        }
    }

    @Override
    public String getIdValue(Object object, int index) {
        String retorno = null;
        Categoria cat = null;
        if (object instanceof Categoria) {
            cat = (Categoria) object;
        } else {
            if(object instanceof List){
                List <Categoria> temp = (List<Categoria>) object;
                if(temp.size()>0){
                    cat = temp.get(0);
                }else{
                    cat = new Categoria();
                }

            }else{
                throw new IllegalArgumentException(
                "O objeto deve ser uma instancia de Categoria");
            }
        }
        retorno = String.valueOf(cat.getIdCategoria());
        return retorno;
    }
}


-- 
"Two rules to succeed in life:
1 - don´t tell people everything you know."
--------
We shall go on to the end.
We shall fight in France
We shall fightover the seas and oceans.
We shall fight with growing confidence and growing strength in the air.
We shall defend our island whatever the cost may be
We shall fight on beaches, we shall fight on the landing grounds,
We shall fight in the fields and in the streets,
We shall fight on the hills.
We shall never surrender.
Winston Churchill

Re: Drop Down Box - Ajax Behaviour

Posted by Daniel Ferreira Castro <df...@gmail.com>.
I have this method that is run on the momento of the form construction
The List<Categoria> keyValuePairsTemp  stores the values of the DropDown

    private DropDownChoice criaCategoriaPaiDropdown() {
        setOutputMarkupId(true);
        choiceRenderer = new CategoriaChoiceRenderer();
        getHibernateTransaction();
        List<Categoria> keyValuePairsTemp = (List<Categoria>) CategoriaDAO
                .getAllCategorias(hibernateSession);
        Categoria categoria = null;
        // Trata o caso de não existir categoria cadastrada
        if (keyValuePairsTemp.size() < 1) {
            categoria = new Categoria();
        } else {
            categoria = keyValuePairsTemp.get(0);
        }

        PropertyModel categoriaPaiModel = new PropertyModel(categoria,
                "categoriaPai");
        categoriaPai = new DropDownChoice("categoriaPai", categoriaPaiModel,
                keyValuePairsTemp, choiceRenderer);
        return categoriaPai;
    }


On Mon, Feb 16, 2009 at 11:06 AM, Eyal Golan <eg...@gmail.com> wrote:

> Where do yo u have the values in the DropDown in the first place?
> If you use a model, why not updating it?
>
>
> Eyal Golan
> egolan74@gmail.com
>
> Visit: http://jvdrums.sourceforge.net/
> LinkedIn: http://www.linkedin.com/in/egolan74
>
> P  Save a tree. Please don't print this e-mail unless it's really necessary
>
>
> On Mon, Feb 16, 2009 at 2:13 PM, Daniel Ferreira Castro
> <df...@gmail.com>wrote:
>
> > I read the examples of Ajax Forms and I am trying to adapt it to my
> needs.
> > I have a form with a input text, a drop down box, a feedback panel and a
> > submit button.
> >
> > My submit is an ajaxButton.
> > My form setsOutputMarkupId(true);
> > My dropdown sets OutputMarkupId(true);
> >
> > When I submit my form it inserts an item on a table.
> > My intention is after the form submit the dropdown uptades itself, adding
> > the previously inserted item to its options.
> >
> > how to do it?
> >
> > --
> > "Two rules to succeed in life:
> > 1 - don´t tell people everything you know."
> > --------
> > We shall go on to the end.
> > We shall fight in France
> > We shall fightover the seas and oceans.
> > We shall fight with growing confidence and growing strength in the air.
> > We shall defend our island whatever the cost may be
> > We shall fight on beaches, we shall fight on the landing grounds,
> > We shall fight in the fields and in the streets,
> > We shall fight on the hills.
> > We shall never surrender.
> > Winston Churchill
> >
>



-- 
"Two rules to succeed in life:
1 - don´t tell people everything you know."
--------
We shall go on to the end.
We shall fight in France
We shall fightover the seas and oceans.
We shall fight with growing confidence and growing strength in the air.
We shall defend our island whatever the cost may be
We shall fight on beaches, we shall fight on the landing grounds,
We shall fight in the fields and in the streets,
We shall fight on the hills.
We shall never surrender.
Winston Churchill

Re: Drop Down Box - Ajax Behaviour

Posted by Eyal Golan <eg...@gmail.com>.
Where do yo u have the values in the DropDown in the first place?
If you use a model, why not updating it?


Eyal Golan
egolan74@gmail.com

Visit: http://jvdrums.sourceforge.net/
LinkedIn: http://www.linkedin.com/in/egolan74

P  Save a tree. Please don't print this e-mail unless it's really necessary


On Mon, Feb 16, 2009 at 2:13 PM, Daniel Ferreira Castro
<df...@gmail.com>wrote:

> I read the examples of Ajax Forms and I am trying to adapt it to my needs.
> I have a form with a input text, a drop down box, a feedback panel and a
> submit button.
>
> My submit is an ajaxButton.
> My form setsOutputMarkupId(true);
> My dropdown sets OutputMarkupId(true);
>
> When I submit my form it inserts an item on a table.
> My intention is after the form submit the dropdown uptades itself, adding
> the previously inserted item to its options.
>
> how to do it?
>
> --
> "Two rules to succeed in life:
> 1 - don´t tell people everything you know."
> --------
> We shall go on to the end.
> We shall fight in France
> We shall fightover the seas and oceans.
> We shall fight with growing confidence and growing strength in the air.
> We shall defend our island whatever the cost may be
> We shall fight on beaches, we shall fight on the landing grounds,
> We shall fight in the fields and in the streets,
> We shall fight on the hills.
> We shall never surrender.
> Winston Churchill
>