Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert 'sub-rows' into a Wicket DataTable

I have an AjaxFallbackDefaultDataTable which contains one row per test result. A test result may have a note attached to it, which needs to be displayed prominently below the test result, hopefully giving a table similar to the following:

| Test | Result | Appraisal |
|------|--------|-----------|
|  1   |  20.0  |   PASS    |
|  2   |  1.50  |   FAIL    |
| Note: This is an epic fail|
|  3   |  19.4  |   PASS    |
|  4   |  14.9  |   PASS    |

Is there any way to achieve this row insertion (preferably with column spanning) using the Wicket DataTable constructs. As I dig down into the source I can find Item renderers but nothing that deals with a row.

Currently I have the following:

// Create the sortable data provider.
SortableDataProvider<TestResult> provider = new SortableDataProvider<TestResult>() {
    //... hibernate pagination code ...
};

List<IColumn> columns = new ArrayList<IColumn>();
columns.add(new TextFilteredPropertyColumn(new Model<String>("Test"), "test", "test"));
columns.add(new TextFilteredPropertyColumn(new Model<String>("Result"), "result", "result"));
columns.add(new TextFilteredPropertyColumn(new Model<String>("Appraisal"), "appraisal", "appraisal"));

// Create a new AJAX table using the sortable, filtered data provider.
final AjaxFallbackDefaultDataTable dataTable = new AjaxFallbackDefaultDataTable("testResultTable", columns.toArray(new IColumn[0]), provider, 20);// 20 = number of rows per page
like image 668
spikeheap Avatar asked Sep 29 '10 13:09

spikeheap


1 Answers

I don't think there is a clean way to do that, you will have to hack the resulting HTML.

I'd attach a custom behavior to the rows that generates the required HTML automatically, something like this:

new AjaxFallbackDefaultDataTable<MyCustomObject>(
    id, columns, dataProvider, rowsPerPage){

    @Override
    protected Item<MyCustomObject> newRowItem(String id,
        int index,
        final IModel<MyCustomObject> model){
        Item<MyCustomObject> item = super.newRowItem(id, index, model);
        item.add(new AbstractBehavior(){

            private static final long serialVersionUID = 1L;

            /**
             * {@inheritDoc}
             */
            @Override
            public void onRendered(Component component){
                if(model.getObject().isEpicFail()){
                    component.getResponse().write(
                    "<tr><td colspan=\"3\">This is an epic fail</td></tr>");
                }
            }

        });
        return item;
    }

    private static final long serialVersionUID = 1L;

}
like image 139
Sean Patrick Floyd Avatar answered Oct 15 '22 17:10

Sean Patrick Floyd