0

I want to do something like this: I want a cell table in which each cell is a class which I have written. I need to add a click event for the cell table. So How can I get which cell was clicked. As Cell is a class which I have defined, based on the clicked cell I need to perform some action. Can I somehow get the object details of the cell which was clicked. For e.g

I need a excel sheet which is like cell table, each cell in the excel sheet is a class I have defined, say the class holds the values like:

CellClass{
 boolean isempty;
 string name;
 int id;
}

Now If i click in the excel sheet, how can I get which cell was clicked, so that I can tell user the name of the cell and whether it is empty or not.

Global Warrior
  • 5,050
  • 9
  • 45
  • 75

2 Answers2

0

Use ClickableTextCell to make clickable cell. Refer below Code detail:

nameCell = new ClickableTextCell() {
            @Override
            public void render(com.google.gwt.cell.client.Cell.Context context,
                    String data, SafeHtmlBuilder sb) {
                super.render(context, "", sb);

                    sb.appendHtmlConstant("<input type=text value='"+data+"' />);

        };

        nameColumn = new Column<Document, String>(nameCell) {

            @Override
            public String getValue(Document object) {
                //code
            }
        };
bNd
  • 7,512
  • 7
  • 39
  • 72
0

Note that there is only one instance of a Cell for each Column, not every value is represented by a Cell instance. The structure you describe above would be the structure of the data you set in CellTable.setRowData.

A Cell has to declare the events it's interested in, via the super constructor in AbstractCell or the getConsumedEvents method defined in Cell. In your case the "click" event.

You can then implement the onBrowserEvent method in your custom cell and react to the click. A context will be passed to this method indicating which row and column the event refers to (see Cell.Context) as well as the key and value associated with the click.

In your case, the custom Cell would look something like this:

public class MyCell extends AbstractCell<String> {
    public MyCell() {
        super("click");
    }

    @Override
    public void onBrowserEvent(Context context, Element parent, String value, NativeEvent event, ValueUpdater<String> valueUpdater) {
        // Handle the click event.
        if ("click".equals(event.getType())) {
            // Ignore clicks that occur outside of the outermost element.
            EventTarget eventTarget = event.getEventTarget();
            if (parent.getFirstChildElement().isOrHasChild(Element.as(eventTarget))) {
                doAction(value, valueUpdater);
            }
        }
    }
}

You can also intercept the event at the Column level.

You can find more information in the Dev Guide

meyertee
  • 2,211
  • 17
  • 16