I have found a solution, based mainly on this example:
https://cwiki.apache.org/WICKET/simple-sortable-datatable-example.html.
However, I set up the columns and create the table in a separate method
that is called on the form submit to recreate the table with the new
columns.

Note that this code works, but I'm sure it can (and will) be improved.
Maybe this will be a help to others.

Shelli

CODE: 

package my.test;

// imports...

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;

    List<String> columnAList = DataService.getColumnA();
    List<String> columnBList = DataService.getColumnB();
    List<String> columnCList = DataService.getColumnC();
    List<String> columnDList = DataService.getColumnD();

    String selectedColumnA;
    String selectedColumnB;
    String selectedColumnC;
    String selectedColumnD;

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

    final DataRecordProvider dataProvider = new DataRecordProvider();

    /**
     * Constructor that is invoked when page is invoked without a
session.
     * 
     * @param parameters Page parameters
     */
    @SuppressWarnings("rawtypes")
    public HomePage(final PageParameters parameters)
    {
        add(CSSPackageResource.getHeaderContribution(HomePage.class,
        "style.css"));

        @SuppressWarnings("serial")
        Form queryForm = new Form("queryForm")
        {
            @Override
            protected void onSubmit()
            {
                // This is how I am currently getting/storing my data
results in the app
                ((Application)
RequestCycle.get().getApplication()).setCurrentDataRecords(DataService.g
etDataRecords(
                        selectedColumnA,
                        selectedColumnB,
                        selectedColumnC,
                        selectedColumnD,
                        null,
                        true));                
                        
                createDataTable();
            }
        };

        add(queryForm);

        // Create filter lists
        queryForm.add(new DropDownChoice<String>("columnAList",
                new PropertyModel<String>(this, "selectedColumnA"),
                columnAList));
        queryForm.add(new DropDownChoice<String>("columnBList",
                new PropertyModel<String>(this, "selectedColumnB"),
                columnBList));
        queryForm.add(new DropDownChoice<String>("columnCList",
                new PropertyModel<String>(this, "selectedColumnC"),
                columnCList));
        queryForm.add(new DropDownChoice<String>("columnDList",
                new PropertyModel<String>(this, "selectedColumnD"),
                columnDList));

        // And their labels
        queryForm.add(new Label("columnALabel", this.columnA));
        queryForm.add(new Label("columnBLabel", this.columnB));
        queryForm.add(new Label("columnCLabel", this.columnC));
        queryForm.add(new Label("columnDLabel", this.columnD));

        // 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);

        createDataTable();
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private void createDataTable()
    {
        ArrayList<SelectOption> list = (ArrayList<SelectOption>)
columnCheckGroup.getModelObject();
        
        if (list == null || list.size() < 1)
        {
            // can't have no columns selected, revert to default
            list = this.getDefaultColumns();
        }

        columns = new ArrayList<IColumn>();

        for (SelectOption option : list)
        {
            columns.add(new PropertyColumn<SelectOption>(new
StringResourceModel(option.getPropertyValue(),
                    this,
                    null),
                    option.getPropertyValue(),
                    option.getPropertyValue()));
        }

        if(datatable != null)
        {
            datatable.remove();
        }
        
        datatable = new DefaultDataTable("datatable",
                columns,
                dataProvider,
                20);
        
        add(datatable);
    }



    private 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 ....
    }
}

-----Original Message-----
From: Shelli Orton 
Sent: Tuesday, September 21, 2010 10:49 AM
To: users@wicket.apache.org
Subject: Dynamic Column Datatable - Example

Hi,

 

I think what I need to do is fairly common.  I have a number of
checkboxes that users can select to determine which columns they want
displayed in a datatable.

 

I've been searching for an example of a dynamic datatable and haven't
been able to find one.  I've seen references to a solution in 2008 mail
archives that suggest it might be implemented in 1.4
(http://www.mail-archive.com/users@wicket.apache.org/msg10528.html).
Can anyone confirm that it has been or not?  In either case, is there an
example that someone can point me to?

 

Thanks in advance!

 

Shelli


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

Reply via email to