如何使用clickHandler将Anchor添加到GWT中的ListGridRecord

时间:2013-05-13 15:47:23

标签: java gwt smartgwt

我需要使用clickHandler将Anchor添加到ListGridRecord。 Click to Anchor应调用该函数(向ListGrid添加更多行)。我可以用href:

编写Anchor
private void addAnchorToRecord(ListGridRecord rec) {
    String img  = "<img src='pict/Plus.png'/>";
    Anchor myAnchor = new Anchor(img, true, "example.html");
    String value = myAnchor.toString();
    rec.setAttribute("versions", value);
}

但是这段代码:

private void addAnchorToRecord(final ListGridRecord rec) {
    String img  = "<img src='pict/Plus.png'/>";
    Anchor myAnchor = new Anchor(img, true);
    myAnchor.addClickHandler(new ClickHandler() {
           @Override
           public void onClick(ClickEvent event) {
               addExtraRows(rec);
           }
        });
    String value = myAnchor.toString();
    rec.setAttribute("versions", value);
}

不起作用。我看到图像,但点击后没有任何反应。在第二种情况下

value = "<a href="javascript:;" class="gwt-Anchor"><img src="pict/Plus.png"></a>"

我认为在这个地方我丢失了有关clickHandler的信息。在这种情况下如何成功添加ClickHandler?

修改

我使用了LinkItem:

private void updateVersions(final ListGridRecord rec) {
    LinkItem linkItem = new LinkItem("link");  
    linkItem.setTitle("LinkItem");  
    linkItem.setLinkTitle("Click Me");  
    linkItem.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            showPreviousVersions(rec);
        }
    });
    rec.setAttribute("versions", linkItem);
}

然后在我的单元格中只有“[object Object]”

我需要的功能:将属性设置为ListGridRecord,它具有icon和clickHandler。

1 个答案:

答案 0 :(得分:0)

如果您只是在ListGridRecord中添加一个按钮,然后在该按钮上添加一个clickhandler,则会更容易。必须这样做:

配置ListGrid时:

    setShowRecordComponents      (true );
    setShowRecordComponentsByCell(true );

设置字段时

    ...
    ListGridField btnField= new ListGridField("btnFieldName", " ");
    ...

然后像这样覆盖createRecordComponent:

 @Override
 protected Canvas createRecordComponent(final ListGridRecord record, final Integer colNum){
        if(getFieldName(colNum).equalsIgnoreCase(("btnFieldName"))){
               IButton btn= new IButton("");
               //configure the button
               btn.addClickHandler(new ClickHandler() {
                  @Override
                  public void onClick(ClickEvent event) {
                    //your logic
                  }
               });
               return btn;
         }
     return super.createRecordComponent(record, colNum);
 }