How to do single row expansion with CellTable?
Asked Answered
C

4

6

I'm trying to use the new GWT CellTable widget but my table needs to support one row expansion, i.e. there is a zippy on the left of a row and when it's clicked, the row should expand to provide more detail information and this row should span across all columns. Is it possible to achieve this with the CellTable? How do I add a row that spans all columns between other rows dynamically?

Any help will be appreciated!

Chabazite answered 22/10, 2010 at 23:45 Comment(0)
R
2

GWT 2.5 will add a CellTableBuilder with the exact goal of allowing this kind of things.

You can find a live example at http://showcase2.jlabanca-testing.appspot.com/#!CwCustomDataGrid (click on the "show friends" cells)

Rickeyricki answered 23/10, 2011 at 15:48 Comment(1)
Good to know, thank you! I am still working on a solution for 2.4 (with CSS magic + manually updating table height upon expansion).Monochromatic
B
0

Can you not make the additional row invisible using getRowElement(int row) and using DOM methods to set display 'none' when rendered and as blank when the button, to show it, is hit.

Barratry answered 23/10, 2010 at 4:29 Comment(1)
Thanks! However, I'm actually not sure how to add a row that spans all columns between rows.Chabazite
M
0

I am working on the solution too and my plan for now is to use CSS classes + manual styles manipulation to make it look as I need. Not sure if I be able to merry it with GWT though: http://jsfiddle.net/7WFcF/

Monochromatic answered 25/10, 2011 at 2:34 Comment(1)
@ThomasBroyer (I cannot comment directly on his answer for some reason) - example from GWT 2.5 still uses columns and it is not clear if it would allow to make a cell which spans across all columns...Monochromatic
P
0

I took a different approach to solve this same problem.

The basic concept is using dom elements to add and remove rows based on an event. The following code is an abstract extension of CellTable. You'll want to call this method from your event that gets fired from the click to expand a row.

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;

public abstract class ActionCellTable<T> extends CellTable<T> {
    protected abstract void addActionsColumn();

Integer previousSelectedRow = null;

public void displayRowDetail(int selectedRow, Element e){
    //Get the tbody of the Cell Table
    //Assumption that we want the first (only?) tbody.
    Element tbody = this.getElement().getElementsByTagName("tbody").getItem(0);
    //Get all the trs in the body
    NodeList<Element> trs = tbody.getElementsByTagName("tr");

    //remove previously selected view, if there was one
    if(previousSelectedRow!=null){
        trs.getItem(previousSelectedRow+1).removeFromParent();
        //If the current is further down the list then the current your index will be one off.
        if(selectedRow>previousSelectedRow)selectedRow--;
    }
    if(previousSelectedRow==null || selectedRow != previousSelectedRow){// if the are equal we don't want to do anything else
        Element td = Document.get().createTDElement();
        td.setAttribute("colspan", Integer.toString(trs.getItem(selectedRow).getChildNodes().getLength()));
        td.appendChild(e);

        Element tr = Document.get().createTRElement();
        tr.appendChild(td);
        tbody.insertAfter(tr, trs.getItem(selectedRow));
        previousSelectedRow=selectedRow;
    } else {
        previousSelectedRow=null;
    }
}

}

previousSelectedRow is used to track which item is "expanded", this could probably be achieved using classes or IDs. If needed I can elaborate more on the CellTable, events, views, and activities.

Pasteurism answered 16/1, 2012 at 21:19 Comment(1)
I will be the first to admit that this may be a hacky solution, but with the level(read lack) of control that you have with CellTables, it was the best I came up with. The 2.5 solution looks great, but until it's released it's of little help to me now. The Builder concept that they introduce will make these sort of customization a lot less hacky.Pasteurism

© 2022 - 2024 — McMap. All rights reserved.