You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by eugenebalt <eu...@yahoo.com> on 2013/06/26 19:20:59 UTC

Select AjaxCheckbox from Another AjaxCheckbox inside ListView

I have a ListView which contains AjaxCheckboxes. (Each AjaxCheckbox is added
as a ListItem inside the ListView.)

I have the requirement that if a particular 'special' AjaxCheckbox is
selected, that should instantly select another particular AjaxCheckbox.

There is the overrideable method onUpdate() that gets called on an
AjaxCheckbox change,

protected void onUpdate(AjaxRequestTarget arg0) { .. }

I can certainly capture the "source event" -- identify when my source
checkbox is selected --  but how do I find the particular checkbox B that
needs to be turned on? The debugger shows that the hierarchy is

List.get("1").get("chkbox"), List.get("2").get("chkbox")

Is there a better way than to iterate over ListItems in this clumsy way to
find the destination AjaxCheckbox that I need, and add it to the Ajax
target? Thanks



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Select-AjaxCheckbox-from-Another-AjaxCheckbox-inside-ListView-tp4659829.html
Sent from the Users forum mailing list archive at Nabble.com.

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


Re: Select AjaxCheckbox from Another AjaxCheckbox inside ListView

Posted by Paul Bors <pa...@bors.ws>.
I have the same use case only a lot more complicated (nested list views in
different panels).

The easier way to auto-select or auto-deselect (aka change the model
object) is to share a CompoundPropertyModel across your component
hierarchy. In this way a single component can interact with its model
object and have access to everything on the page. Then it becomes a matter
of adding the component you want to refresh to the AjaxRequestTarget.


On Thu, Jun 27, 2013 at 1:58 PM, eugenebalt <eu...@yahoo.com> wrote:

> I found a solution, but I'm not sure it's the best/most elegant one, there
> is
> a lot of messy code.
>
> Upon selecting a certain AjaxCheckbox A in a ListView, I had to
> 1) disable AjaxCheckbox B
> 2) set its value to TRUE
>
> and conversely, upon deselecting A, I had to (1) enable B and (2) set its
> value to FALSE.
>
> First of all, I got the Enable/Disable to work by overriding the
> AjaxCheckbox's isEnabled. But in order to make that work, I had to use 2
> flags. The first flag holds the current value of A, and the second flag
> shows that clicking has begun. These are the flags "Clicked" and
> "Selected",
> and they get updated on onUpdate().
>
> // override isEnabled
> public boolean isEnabled() {
>         if (bean.getKey().equals(DELETEOWN)
>                 && eNotesMgrClicked)
>         {
>                 if (eNotesMgrSelected)
>                         return false;
>                 else
>                         return true;
>         }
>         else
>                     return super.isEnabled();
> }
>
> // set the flag that Checkbox A has been clicked and selected/de-selected
> @Override
> protected void onUpdate(AjaxRequestTarget arg0) {
>            if (bean.getKey().equals(ADMIN))
>            {
>                      eNotesMgrClicked = true;
>
>                      if (getModelObject())
>                            eNotesMgrSelected = true;
>                 else
>                            eNotesMgrSelected = false
>            }
>         arg0.addComponent(checkboxDeleteOwn);
>         }
> }
>
> The 2nd issue, actually checking/unchecking the checkbox, was more
> difficult. I had to actually save a reference to the AjaxCheckbox I want to
> select/de-select. To save it, I overrode onBeforeRender(), and if it was
> the
> right checkbox B, I saved its reference in a variable. Then, I could
> reference it in onUpdate() and set its model object.
>
> My questions:
> 1) I also wanted to do the setEnabled(true)/setEnabled(false) on Checkbox
> B,
> now that I have its reference, as part  of onUpdate, but that does NOT
> work.
> Can anyone tell me why this Enable/Disable doesn't work:
>
> if (getModelObject())
> {
>   eNotesMgrSelected = true;
>   checkboxDeleteOwn.setEnabled(false);
>   checkboxDeleteOwn.setModelObject(true);
> }
> else
> {
>   eNotesMgrSelected = false;
>   checkboxDeleteOwn.setEnabled(true);
>   checkboxDeleteOwn.setModelObject(false);
> }
>
> The setModelObject works on my saved-reference checkboxDeleteOwn, but
> setEnabled() does NOT work.
>
> 2) This code is pretty messy, is there any better way to disable and set
> individual AjaxCheckboxes based on the action of another AjaxCheckbox?
>
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Select-AjaxCheckbox-from-Another-AjaxCheckbox-inside-ListView-tp4659829p4659872.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: Select AjaxCheckbox from Another AjaxCheckbox inside ListView

Posted by eugenebalt <eu...@yahoo.com>.
I found a solution, but I'm not sure it's the best/most elegant one, there is
a lot of messy code.

Upon selecting a certain AjaxCheckbox A in a ListView, I had to 
1) disable AjaxCheckbox B
2) set its value to TRUE

and conversely, upon deselecting A, I had to (1) enable B and (2) set its
value to FALSE.

First of all, I got the Enable/Disable to work by overriding the
AjaxCheckbox's isEnabled. But in order to make that work, I had to use 2
flags. The first flag holds the current value of A, and the second flag
shows that clicking has begun. These are the flags "Clicked" and "Selected",
and they get updated on onUpdate().

// override isEnabled
public boolean isEnabled() {
	if (bean.getKey().equals(DELETEOWN)
		&& eNotesMgrClicked)
	{
		if (eNotesMgrSelected)
			return false;
		else
			return true;
	}
	else
	            return super.isEnabled(); 
}

// set the flag that Checkbox A has been clicked and selected/de-selected
@Override
protected void onUpdate(AjaxRequestTarget arg0) {
           if (bean.getKey().equals(ADMIN))
           {
                     eNotesMgrClicked = true;
							
                     if (getModelObject())							
                           eNotesMgrSelected = true;
	        else						
                           eNotesMgrSelected = false
           }
        arg0.addComponent(checkboxDeleteOwn);
        }
}

The 2nd issue, actually checking/unchecking the checkbox, was more
difficult. I had to actually save a reference to the AjaxCheckbox I want to
select/de-select. To save it, I overrode onBeforeRender(), and if it was the
right checkbox B, I saved its reference in a variable. Then, I could
reference it in onUpdate() and set its model object.

My questions:
1) I also wanted to do the setEnabled(true)/setEnabled(false) on Checkbox B,
now that I have its reference, as part	of onUpdate, but that does NOT work.
Can anyone tell me why this Enable/Disable doesn't work:

if (getModelObject())
{								
  eNotesMgrSelected = true;
  checkboxDeleteOwn.setEnabled(false);
  checkboxDeleteOwn.setModelObject(true);
}
else
{
  eNotesMgrSelected = false;
  checkboxDeleteOwn.setEnabled(true);
  checkboxDeleteOwn.setModelObject(false);
}

The setModelObject works on my saved-reference checkboxDeleteOwn, but
setEnabled() does NOT work.

2) This code is pretty messy, is there any better way to disable and set
individual AjaxCheckboxes based on the action of another AjaxCheckbox?



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Select-AjaxCheckbox-from-Another-AjaxCheckbox-inside-ListView-tp4659829p4659872.html
Sent from the Users forum mailing list archive at Nabble.com.

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