为“单元格列表”中添加的按钮单元格添加clickHandler



我创建了一个cellList:当用户点击按钮"发送"时,我想添加一个点击处理程序

请帮忙。如果用户单击"发送"按钮,FieldUpdater应该可以工作。

这是代码:

  final String imageHtml =AbstractImagePrototype.create(images.contact()).getHTML();
// first make a list of HasCell type - MyClass is the type of object being displayed in the CellList (could be String for simple labels)
List<HasCell<contactinfo, ?>> hasCells = new ArrayList<HasCell<contactinfo, ?>>();
hasCells.add(new HasCell<contactinfo, String>()
        {
    public ButtonCell cell = new ButtonCell();
    public Cell<String> getCell()
    {
        return cell;
    }
    @Override
    public String getValue(contactinfo object)
    {
        return "Send";
    }
    @Override
    public FieldUpdater<contactinfo, String> getFieldUpdater() {
        FieldUpdater< contactinfo, String > updater=                 new FieldUpdater<contactinfo, String>() {
            @Override
            public void update(int index, contactinfo object, String value) {
                Window.alert("You clicked  "+object.getName());
            }
        };
        return updater;
    }
        }
);
// now construct the actual composite cell using the list (hasCells)
Cell<contactinfo> myClassCell = new CompositeCell<contactinfo>(hasCells)
{
    @Override
    public void render(Context context, contactinfo value, SafeHtmlBuilder sb)
    {
        sb.appendHtmlConstant("<table><tbody><tr>");
        super.render(context, value, sb);
        sb.appendHtmlConstant("</tr></tbody></table>");
    }
    @Override
    protected Element getContainerElement(Element parent)
    {
        // Return the first TR element in the table.
        return parent.getFirstChildElement().getFirstChildElement();
    }
    @Override
    protected <X> void render(Context context, contactinfo contactinfo, SafeHtmlBuilder sb, HasCell<contactinfo, X> hasCell)
    {

这将在新的表格单元格中呈现复合单元格内的每个单元格

        // Value can be null, so do a null check..
        if (contactinfo == null) {
            return;
        }
        sb.appendHtmlConstant("<table>");
        // Add the contact image.
        sb.appendHtmlConstant("<tr><td rowspan='3'>");
        sb.appendHtmlConstant(imageHtml);
        sb.appendHtmlConstant("</td>");
        // Add the name and address.
        sb.appendHtmlConstant("<td style='font-size:95%;'>");
        if(contactinfo.getName()!=null)
            sb.appendEscaped(contactinfo.getName());
        sb.appendHtmlConstant("</td></tr><tr><td>");
        if(contactinfo.getAddress()!=null)
            sb.appendEscaped(contactinfo.getRemarks());
        sb.appendHtmlConstant("</td>");
        Cell<X> cell = hasCell.getCell();
        sb.appendHtmlConstant("<td>");
        cell.render(context, hasCell.getValue(contactinfo), sb);
        sb.appendHtmlConstant("</td></tr></table>");
    }
};
// then make the actual cellList, passing the composite cell
cellList =new CellList<contactinfo>(myClassCell,KEY_PROVIDER);
// Add a selection model so we m select cells.
singleselectionModel = new SingleSelectionModel<contactinfo>(
        KEY_PROVIDER);

cellList.setSelectionModel(singleselectionModel);
singleselectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
    @Override
    public void onSelectionChange(SelectionChangeEvent event) {
    }
});

此外,我在代码中并没有看到任何处理事件的部分。你通读了吗http://www.gwtproject.org/doc/latest/DevGuideUiCustomCells.html#cell-onBrowserEvent

您尝试过GWT提供的代码示例吗http://gwt.googleusercontent.com/samples/Showcase/Showcase.html#!CwCellSampler。浏览"源代码"!!!

如果你还没有阅读,那么你应该从这里开始@DevGuideUiCustomCells

最新更新