How to add clickhandler on ImageCell in GWT CellTable?

前端 未结 8 1338
再見小時候
再見小時候 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<MyRecord, String> buttonCol = new Column<MyRecord, String>(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<MyRecord, String>() {
        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();
    }
    
    0 讨论(0)
  • 2021-02-06 04:56

    I got all the above and added them in my app. Thanks to all. stackoverflow rocks!

        ButtonCell bc = new ButtonCell() {
            @Override
            public void render(Context context, SafeHtml data, SafeHtmlBuilder sb) {
                if (data != null) {
                    ImageResource icon = Connector.imageResources.minus();
                    Image image = new Image(icon);
                    //fix the mouse pointer
                    image.getElement().getStyle().setCursor(Cursor.POINTER);
                    //Do something with the DATA
                    image.setTitle("Delete " + data.asString());
                    SafeHtml html = SafeHtmlUtils.fromTrustedString(image.toString());
                    sb.append(html);
                }
            }
        };
        Column<InstanceProperty, String> imageColumn = new Column<InstanceProperty, String>(bc) {
            @Override
            public String getValue(InstanceProperty object) {
                //return the DATA
                return object.getKey();
            }
        };
        imageColumn.setFieldUpdater(new FieldUpdater<InstanceProperty, String>() {
            @Override
            public void update(int index, InstanceProperty property,
                    String value) {
                //you can also use the DATA to do something
                InstancePropertiesTable.this.dataProvider.getList().remove(index);
            }
        });
        addColumn(imageColumn, "");
    
    0 讨论(0)
提交回复
热议问题