GwtMaterialDesign / gwt-material

A Google Material Design wrapper for GWT
https://gwtmaterialdesign.github.io/gmd-core-demo/
Apache License 2.0
411 stars 124 forks source link

ChangeEvent in MaterializeListBox does not work. #24

Closed jkubiszewski closed 9 years ago

jkubiszewski commented 9 years ago

There is still one problem with MaterialListBox. ChangeEvent does not work.

listBox.addChangeHandler(new ChangeHandler() {
      public void onChange(ChangeEvent event) {
        // Get the index of the selected Item
        Window.alert(listBox.getSelectedItemText());
      }
    });
jkubiszewski commented 9 years ago

Is there any way to be able to use it? Or maybe I am doing something wrong?

anton-johansson commented 9 years ago

+1

Also wondering why this doesn't work.

anton-johansson commented 9 years ago

I implemented a workaround for this. Note that I'm not extending MaterialListBox because all it does is call a .material_select().

public class CustomListBox extends ListBox
{
    private final String id;

    public CustomListBox()
    {
        this.id = UUID.randomUUID();
        getElement().setId(this.id);
    }

    @Override
    protected void onLoad()
    {
        super.onLoad();
        createInternalChangeHandler(this.id, this);
    }

    private void onChangeInternal()
    {
        // Do your thing!
    }

    private static native void createInternalChangeHandler(String id, CustomListBox self)
    /*-{
        var callback = $entry(function()
        {
            self.@com.viskan.client.components.CustomListBox::onChangeInternal()();
        });

        $wnd.jQuery('#' + id).material_select();
        $wnd.jQuery('#' + id).change(callback);
    }-*/;
}

I could submit a pull request for fixing MaterialListBox this way, but I'd rather see a proper solution only using native GWT without using JSNI. Any ideas or thoughts?

kevzlou7979 commented 9 years ago

@anton-johansson -> Thanks for this , Yes Im ok with your workaround just submit a pull request and we will merge it :). We are happy that we have at least a solution on this part.

Regards, kevzlou7979

anton-johansson commented 9 years ago

@kevzlou7979: Allright, I'll work on it. Hopefully I can get rid of the UUID.randomUUID() also. One question though. Should we make this work with addChangeHandler(ChangeHandler)? I'm relatively new to GWT and I haven't investigated exactly how the event handlers work yet.

kevzlou7979 commented 9 years ago

@anton-johansson - you can use DOM.createUniqueId() for better solution . Yes sure you, I tested your code running pretty well. Thank you

anton-johansson commented 9 years ago

Ahh, of course. Thanks!