简体   繁体   English

使用GWTQuery调用Bootstrap JS折叠方法

[英]Using GWTQuery to call Bootstrap JS collapse method

I want to replicate this jQuery call in gQuery to open all closed Accordion panels at once: 我想在gQuery中复制此jQuery调用,以一次打开所有关闭的手风琴面板:

$('.panel-collapse:not(".in")').collapse('show');

I have tested this on my app and it works, however I am unable to successfully implement the same logic using gQuery to call the JavaScript collapse method: 我已经在我的应用程序上对此进行了测试,并且可以正常工作,但是我无法使用gQuery来成功实现相同的逻辑来调用JavaScript折叠方法:

@Override
public void onClick(ClickEvent event) {
  GQuery.$(".panel-collapse").not(".in").trigger("collapse", "show");
}

What is the correct way to call JavaScript methods from gQuery? 从gQuery调用JavaScript方法的正确方法是什么?

Further Info - I have successfully tested this gQuery code in my onClick method to add a test class to the affected divs - so I am certain the selector part of the above query works: 进一步的信息-我已经在onClick方法中成功测试了该gQuery代码,以向受影响的div添加测试类-因此,我确定上述查询的选择器部分有效:

GQuery.$(".panel-collapse").not(".in, .MENU").addClass("test");

Why you can't use collapse with GQuery 为什么不能在GQuery中使用collapse

IIRC collapse() is not a jQuery API method. IIRC crash collapse()不是jQuery API方法。 Maybe you're using Bootstrap JS, or some jQuery plugin that provides this functionality, but it's not one of the jQuery API methods and thus it's not provided by GQuery. 也许您正在使用Bootstrap JS或提供此功能的jQuery插件,但是它不是jQuery API方法之一,因此GQuery也不提供。

Why trigger wouldn't work either 为什么trigger也不起作用

GQuery, or GwtQuery, is not a wrapper for jQuery, but a full Java implementation of the jQuery API. GQuery或GwtQuery不是jQuery的包装器,而是jQuery API的完整Java实现。
What this means is that, when you do something like this: 这就是说,当您执行以下操作时:

GQuery.$(".panel-collapse").not(".in").slideToggle();

You're not invoking jQuery's $() , not() , or slideToggle() ; 不是在调用jQuery的$()not()slideToggle() you are using a Java library to achieve the same result. 您正在使用Java库来实现相同的结果。
That's the reason why trying something like trigger("slideToggle") won't work: because a) slideToggle() is not an event, but a function; 这就是为什么尝试使用trigger("slideToggle")不起作用的原因:因为a) slideToggle()不是事件,而是函数; and b) GQuery doesn't make use of jQuery's JS functions. b)GQuery不使用jQuery的JS函数。

Solution with GQuery GQuery解决方案

You can achieve the same "accordion" effect using the slideUp() , slideDown() and slideToggle() functions methods. 您可以使用slideUp()slideDown()slideToggle() 函数 方法实现相同的“手风琴”效果。 To open all the collapsed elements, just calling slideDown() on them should work: 要打开所有折叠的元素,只需在其上调用slideDown()即可:

GQuery.$(".panel-collapse").not(".in").slideDown();

For full accordion effect, combine those with the toggleClass() and removeClass() methods to mark which elements are open / closed so you know which ones to toggle when clicked. 为了获得完全的手风琴效果,请将它们与toggleClass()removeClass()方法结合使用,以标记打开/关闭的元素,以便您单击时知道要切换的元素。

Solution with native collapse() 具有本机collapse()解决方案collapse()

Now, if you don't mind the advice... GQuery is great, but the animations are far from being as smooth as in native jQuery. 现在,如果您不介意这些建议……GQuery非常棒,但是动画远没有原生jQuery那样流畅。 I guess the same happens with Bootstrap. 我猜Bootstrap也会发生同样的情况。
If you can (and I can't see a reason why you couldn't), just use JSNI to make a native call to collapse() like this: 如果可以(并且我看不到不能这样做的原因),只需使用JSNI像这样对collapse()进行本地调用:

private native void collapseAll() /*-{
    $wnd.$('.panel-collapse:not(".in")').collapse('show');
}-*/;

This requires that you load jQuery (or Bootstrap) in your page, but since you said that invoking collapse() in plain JS worked, I guess that's your case. 这要求您在页面中加载jQuery(或Bootstrap),但是由于您说在普通JS中调用collapse()起作用,所以我想这就是您的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM