Calling JQuery function from GWT

℡╲_俬逩灬. 提交于 2019-12-04 07:43:21

I tried the solution by elvispt, it works. In the JSNI code, I had to replace $ with $wnd.jQuery because otherwise it doesn't compile .

I tried also a second solution which I decide to implement : Instead of using a wrapper around Menu, I overided onAttach() in the Menu class it self and call bind

import com.google.gwt.core.client.GWT;     
public class Menu extends Composite  {

    private static MenuUiBinder uiBinder = GWT.create(MenuUiBinder.class);    
    interface MenuUiBinder extends UiBinder<Widget, Menu> {}        

    public Menu() {
        initWidget(uiBinder.createAndBindUi(this));
    }

     @Override
     public void onAttach() {
         super.onAttach();
         bind();
     }      

     private static native void bind() /*-{
          $wnd.jQuery('#menu').find('.submenu').each(function(){
         alert("inside");
           var totalWidth = 0;
            $wnd.jQuery(this).children().each(function(){
              totalWidth +=  $wnd.jQuery(this).outerWidth();
           }).end().css({
          'display'   : 'none',
          'width'     : totalWidth
           });
         }).end().css({
           'overflow'  : 'visible'
         });
     }-*/;      
}

Thanks again

Wrapp the uiBinder generated class in a SimplePanel then override the onAttach() method

Since the generated class is menu:

Create another class, name it for example: menuCaller

    public class menuCaller extends SimplePanel {

        menu menuWrap = new menu();

        public menuCaller() {
            add(menuWrapp);
        }

        @Override
        public void onAttach()
        {
            super.onAttach();
            bind();
        }

            private static native void bind() /*-{
                $('#menu').find('submenu').each(function(){
                    alert("inside");
                      var totalWidth = 0;
                      $(this).children().each(function(){
                         totalWidth += $(this).outerWidth();
                      }).end().css({
                     'display'   : 'none',
                     'width'     : totalWidth
                      });
               }).end().css({
                      'overflow'  : 'visible'
                   });
        }-*/;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!