0

I have a cell table showing some data. For each row, I want to have two columns which contain edit / delete buttons. When each button is clicked, it should be able to notify a listener which button was clicked (and preferably also be able to pass in the object that row is associated with).

How can I do this? Specifically, I know how to render a button, but how can I process the on-click event and pass in the object which the user clicked to edit or delete?

Ali
  • 261,656
  • 265
  • 575
  • 769

2 Answers2

1

This is the standard approach:

myTable.addCellPreviewHandler(new Handler<MyObject>() {

    @Override
    public void onCellPreview(CellPreviewEvent<MyObject> event) {
        if ("click".equals(event.getNativeEvent().getType())) {
            if (event.getColumn() == 0 || event.getColumn() == 1) {
                MyObject object = event.getValue();
                Window.alert("Column clicked: " + event.getColumn());
            }
        }
    }

});

This is a more efficient solution, because you only have one handler attached to a table, instead of trying to attach a handler to each button in each row.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
  • this will probably trigger when anywhere on the row is clicked, right? i want it to only trigger when certain buttons are clicked – Ali Mar 20 '14 at 13:49
  • Add a check. I will update the answer. Note that a native event is triggered by a browser. Here you only check if this is the event that you need or not. – Andrei Volgin Mar 20 '14 at 13:50
  • Thanks, one other question.. what if a user clicks a delete button, and the given row is deleted. In that case, how would I push this info to the table, to get it to stop showing that row? – Ali Mar 20 '14 at 13:57
  • You simply remove this object from displayItems() - or whatever you are using as a List in your ListDataProvider. – Andrei Volgin Mar 20 '14 at 13:59
  • I'm using an async provider. How can I remove it in that case? Can I just ask it to request the items again? – Ali Mar 20 '14 at 14:01
  • You may need to ask again if you have more items than visible, i.e. you were showing 10 times, a user deleted one, now you need to get another item which was not visible before. – Andrei Volgin Mar 20 '14 at 14:02
  • Yeah, but there's no way to tell the table that a user deleted the item. That's what I'm asking.. how to tell the table to delete an item, so it will request again – Ali Mar 20 '14 at 14:05
  • If you delete your item and refresh the table, this item would disappear from it. If you don't need to refresh the table (i.e. all items are visible), simply remove it from the list that you passed to your data provider. – Andrei Volgin Mar 20 '14 at 14:13
  • For refreshing the table, I actually had to do `myAsyncProvider.getDisplays().setRangeAndForce` (paraphrasing) – Ali Mar 20 '14 at 17:35
0

I think you can make a foreach through all the rows in the celltable (I never worked with celltables)
And then you can add your own ClickHandler to the Button. Something like that (not tested):

final int row = myrow; // add the row value or a object identifier or similar 
Button delete_button = new Button("delete");
    delete_button.addClickHandler(new ClickHandler(){
    @Override
    public void onClick(ClickEvent event) {
        // Insert your delete funciton
        delete(row);
   }
});

You mentioned a Listener, Listener are depreciated, use Handler instead.

i3luefire
  • 59
  • 1
  • 12
  • See, the point of cell tables is, instead of having a Button widget, you render the button using a html string, writing it to the innerHtml. So I don't think adding an event handler like this or making a widget like this, would work. – Ali Mar 20 '14 at 13:19
  • Maybe if you know the ID of the Button, you could do something like that: Button mybutton = Button.wrap(DOM.getElementById("Button ID")); mybutton.addClickHandler(myHandler); This should wrap the existing button form the rendered HTMLstring – i3luefire Mar 20 '14 at 13:25
  • That would kind of defeat the purpose too, because the purpose of using CellTable is to have lightweight tables using html strings, without having widgets. I will look at the source code of ButtonCell, since it provides a click handling mechanism, to see how it does it. – Ali Mar 20 '14 at 13:35