How to add clickhandler on ImageCell in GWT CellTable?

前端 未结 8 1339
再見小時候
再見小時候 2021-02-06 04:01

I have tried this but didn\'t work

Column imageColumn = new Column(new ImageCell()) {
       @Override
             


        
8条回答
  •  醉梦人生
    2021-02-06 04:47

    This worked for me:

    public class ButtonImageCell extends ButtonCell{
        @Override
        public void render(com.google.gwt.cell.client.Cell.Context context, 
            String renderedHtmlStr, SafeHtmlBuilder sb) {
            sb.appendHtmlConstant(renderedHtmlStr);
        }
    }
    

    In a class containing CellTable table:

    TableResources resources = GWT.create(TableResources.class);
    ImageResourceRenderer imageRenderer = new ImageResourceRenderer();
    

    ...

    Column buttonCol = new Column(new ButtonImageCell()) {
        @Override
        public String getValue(MyRecord record) {
            if(record.isOn())
                return imageRenderer.render(resources.getOnImg()).asString();
            else
                return imageRenderer.render(resources.getOffImg()).asString();
        }
    };
    
    buttonCol.setFieldUpdater(new FieldUpdater() {
        public void update(int index, MyRecordobject, String value) {
            if (Window.confirm("Do stuff?")) {
                //todo: stuff
            }
        }
    });
    

    ...

    table.addColumn(buttonCol, "");
    

    Where the ImageResource comes from (resources):

    public interface TableResources extends CellTable.Resources {
    
        interface TableStyle extends CellTable.Style {
        }
    
        @Source("/images/on.png")
        ImageResource getOnImg();
    
        @Source("/images/off.png")
        ImageResource getOffImg();
    }
    

提交回复
热议问题