MaNn
MaNn

Reputation: 785

Add clickHandler for buttoncell added in Cell List

I have created a cellList : I want to add a clickhandler when user clicks on button "Send"

Please Help. FieldUpdater should work if user clicks on "Send" button.

Here is the code :

  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)
    {

this renders each of the cells inside the composite cell in a new table cell

        // 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) {

    }
});

Upvotes: 0

Views: 205

Answers (1)

appbootup
appbootup

Reputation: 9537

Also, I do not see in code any piece that handles event. Did you read through http://www.gwtproject.org/doc/latest/DevGuideUiCustomCells.html#cell-onBrowserEvent

Have you tried the code sample provided by GWT - http://gwt.googleusercontent.com/samples/Showcase/Showcase.html#!CwCellSampler . Browse the "Source Code" !!!

If you have not read already then you should start here @ DevGuideUiCustomCells

Upvotes: 1

Related Questions