How to add clickhandler on ImageCell in GWT CellTable?

前端 未结 8 1330
再見小時候
再見小時候 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:34

    A good solution for this issue, if you use ActionCell that is able to handle clicking. The use of it is a bit complicatied, but for me it worked pretty well.

    First you need to initialize ActionCell with a delegate, in the constructor, write new ActionCell.Delegate. In this override the method execute and in that write your code that handle the clicking event.

    The other thing you need to do is building up a html from the image. The SafeHtmlUtils class gives you a very easy way to do that. It's fromTrustedString method helps you building up the html:

    SafeHtmlUtils.fromTrustedString(AbstractImagePrototype.create ("Your image from a resource class").getHTML());
    

    This way the SafeHtml field can be initalized and if you give the ActionCell's contructor the SafeHtml and the Delegate, that it will do the work for you.

    In this example a button will be initialized with the image from the bundle file in it. You can make it without the button if you override the render method of the ActionCell and append the SafeHtmlBuilder in the method with the same SafeHtml variable as above.

    My code looks like the following:

    IdentityColumn imageCell = new IdentityColumn(new ActionCell("AnyString", 
                    new ActionCell.Delegate() {
    
                @Override
                public void execute(final Type item) {
                    "your code"
                }
            }) {
    
                @Override
                public void render(Context context, Type value, SafeHtmlBuilder sb) {
                    if (value != null) {
                        SafeHtml html = SafeHtmlUtils.fromTrustedString(AbstractImagePrototype.create(resource.image).getHTML());
                        sb.append(html);
                    }
                }
            });
    

    You'd rather override the method in an another class but I didn't want to split them for this post. It worked for me very well, I hope it will help other's too.

提交回复
热议问题