Try creating your checkbox like this, with a proper boolean model:
add(new CheckBox("check", new IModel<Boolean>() {
public Boolean getObject() {
return pax.contains((Passenger) item.getModelObject());
}
public void setObject(Boolean bool) {
if (bool) {
pax.add((Passenger) item.getModelObject());
} else {
pax.remove((Passenger) item.getModelObject());
}
}
public void detach() {
// no-op
}
}));
--
Jeremy Thomerson
http://www.wickettraining.com
On Thu, Nov 20, 2008 at 10:40 AM, Harrison, Andy
<[EMAIL PROTECTED]>wrote:
> Hi...
>
>
>
> I've been having lots of fun trying to get a list of checkboxes to
> display correctly on a page. What I am attempting to do is to display a
> list of checkboxes based on a list of passengers and when the checkbox
> is checked, add the selected passenger to another object.
>
>
>
> I had great fun trying to figure out why all of the checkboxes are
> checked all of the time and was hoping that overriding the
> onComponentTag and attempting to set checked as true/false or even
> checked/unchecked based on a condition might help, but to no avail. I'm
> probably missing something really simple, but need pointing in the right
> direction.
>
>
>
>
>
> CheckGroup group = new CheckGroup("group", passengers) {
>
>
>
> @Override
>
> protected void onSelectionChanged(final Collection
> newSelection) {
>
>
>
> Extra extra = (Extra) getParent().getModelObject();
>
>
>
> List<Passenger> pax = extra.getExtraPassengers();
>
> List<Passenger> selection = (List<Passenger>)
> newSelection;
>
> if (pax.containsAll(selection)) {
>
> pax.removeAll(selection);
>
> } else {
>
> pax.addAll(selection);
>
> }
>
> }
>
>
>
> @Override
>
> protected boolean wantOnSelectionChangedNotifications() {
>
> return true;
>
> }
>
>
>
> };
>
>
>
>
>
> ListView people = new ListView("people", passengers) {
>
>
>
> @Override
>
> protected void populateItem(final ListItem item) {
>
> final Passenger pax = (Passenger) item.getModelObject();
>
> Check check = new Check("checkbox", item.getModel()) {
>
>
>
> @Override
>
> protected void onComponentTag(final ComponentTag
> tag) {
>
> super.onComponentTag(tag);
>
> if (extra.getExtraPassengers().contains(pax)) {
>
> tag.put("checked", "checked");
>
> } else {
>
> tag.put("checked", "unchecked");
>
> }
>
> }
>
>
>
> };
>
> item.add(check);
>
> item.add(new Label("name", pax.getDefaultDetails()));
>
> }
>
> };
>
> people.setReuseItems(true);
>
> group.add(people);
>
>
>
> extraItem.add(group);
>
>
>
> boolean displaySelected = extra.isSelectedExtra();
>
>
>
> group.setVisible(displaySelected);
>
>
>
> Regards
>
>
>
> Andrew
>
>
>
>
>
> ..........................
>
> CarbonNeutral? office
>
> Thomson.co.uk <http://thomson.co.uk/> for Holidays, Flights, Hotels,
> customer reviews and over 2000 videos. Find us at www.thomson.co.uk, Sky
> 637 or on your high street.
>
> CONFIDENTIALITY NOTICE & DISCLAIMER
>
> This message, together with any attachments, is for the confidential and
> exclusive use of the intended addresses(s). If you receive it in error,
> please delete the message and its attachments from your system immediately
> and notify us by return e-mail. Do not disclose copy, circulate or use any
> information contained in this e-mail.
> (1) The content of this e-mail is to be read subject to our terms of
> business, as applicable.
> (2) E-mail may be intercepted or affected by viruses and we accept no
> responsibility for any interception or liability for any form of viruses
> introduced with this e-mail.
> (3) The sender shall remain solely accountable for any statements,
> representations or opinions that are clearly his or her own and not made in
> the course of employment.
> (4) For risk, protection and security purposes, we may monitor e-mails and
> take appropriate action.
> Registered Office: TUI Travel House, Crawley Business Quarter, Fleming Way,
> Crawley, West Sussex RH10 9QL
>
> TUI Travel PLC, Registered in England and Wales (Number 6072876)
> TUI Northern Europe Limited, Registered in England and Wales (Number
> 3490138)
> TUI UK Limited, Registered in England and Wales (Number 2830117) ; VAT
> Number: 233 3687 62
> Thomsonfly Limited, Registered in England and Wales (Number 444359); VAT
> Number: 490 2120 79
>
> Telephone: +44 (0)24 7628 2828 | Fax: +44 (0)24 7628 2844 | IT Helpdesk:
> +44 (0)20 7383 1555 | IT E-mail: [EMAIL PROTECTED]
>