Hi,

I am trying to use a CheckGroup to manage which columns are used in a
datatable.  I've set up my columns as per the code below.  The
getColumnCheckBoxes method creates the check boxes that will be part of
the group.  The getDefaultColumns defines which columns should be
checked by default.  The default columns are being set, but the boxes
are not being displayed as "checked" when the page is first loaded.
However, the correct columns are being displayed in the datatable, so
they are being set "behind the scenes".

How do I get them to display as checked on page load?

Thanks in advance!

Shelli

CODE:

public class HomePage extends WebPage
{
    StringResourceModel columnA = new StringResourceModel("columnA",
            this,
            null);
    StringResourceModel columnB = new StringResourceModel("columnB",
            this,
            null);   
    StringResourceModel columnC = new StringResourceModel("columnC",
            this,
            null);
    StringResourceModel columnD = new StringResourceModel("columnD",
            this,
            null);    

    ArrayList<SelectOption> columnCheckBoxes = null;

    CheckGroup<SelectOption> columnCheckGroup = new
CheckGroup<SelectOption>("columnCheckGroup",
            new ArrayList<SelectOption>());
    
    @SuppressWarnings("rawtypes")
    List<IColumn> columns = new ArrayList<IColumn>();

    /**
     * Constructor that is invoked when page is invoked without a
session.
     * 
     * @param parameters Page parameters
     */
    @SuppressWarnings("rawtypes")
    public HomePage(final PageParameters parameters)
    {        
        @SuppressWarnings("serial")
        Form queryForm = new Form("queryForm")
        {
            @Override
            protected void onSubmit()
            {
                // do stuff
            }
        };

        add(queryForm);

        // Create column selection boxes
        columnCheckGroup.add(new
CheckGroupSelector("columnCheckGroupSelector"));
        
        @SuppressWarnings({ "unchecked", "serial" })
        ListView checkBoxes = new ListView("columnCheckGroup",
                getColumnCheckBoxes())
        {
            @SuppressWarnings("unchecked")
            protected void populateItem(ListItem item)
            {
                item.add(new Check("columnCheckbox", item.getModel()));
                item.add(new Label("displayValue",
                        new PropertyModel(item.getModel(),
"displayValue")));
            }

        };

        if (columnCheckGroup.getModelObject().isEmpty())
        {
            columnCheckGroup.setModelObject(this.getDefaultColumns());
        }

        columnCheckGroup.add(checkBoxes);
        queryForm.add(columnCheckGroup);
        
        // more init stuff...

    }

    public ArrayList<SelectOption> getColumnCheckBoxes()
    {
        if (columnCheckBoxes == null)
        {
            columnCheckBoxes = new ArrayList<SelectOption>();
            
            columnCheckBoxes.add(new SelectOption("columnA",
this.columnA.getObject()));
            columnCheckBoxes.add(new SelectOption("columnB",
this.columnB.getObject()));
            columnCheckBoxes.add(new SelectOption("columnC",
this.columnC.getObject()));
            columnCheckBoxes.add(new SelectOption("columnD",
this.columnD.getObject()));
        }

        return columnCheckBoxes;
    }

    /*
        By default, columns A and B are selected
    */
    private ArrayList<SelectOption> getDefaultColumns()
    {
        ArrayList<SelectOption> defaultColumns = new
ArrayList<SelectOption>();

        defaultColumns.add(new SelectOption("columnA",
this.columnA.getObject()));
        defaultColumns.add(new SelectOption("columnB",
this.columnB.getObject()));

        return defaultColumns;
    }
    
    class SelectOption implements Serializable
    {
        private static final long serialVersionUID = 1L;

        private String propertyValue;
        private String displayValue;
        
        /**
         * Utility class for storing property and display values for
option 
         * widgets (e.g. drop down lists, check boxes).
         */
        public SelectOption(String propertyValue, String displayValue)
        {
            this.propertyValue = propertyValue;
            this.displayValue = displayValue;
        }

        // getters/setters ....
    }
}


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

Reply via email to