Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top