英文:
Is there a way to use a reference to an existing java class from javascript in Vaadin?
问题
- 用户单击按钮
Button button = new Button();
button.addClickListener(c -> tourPageController.executeShepherd(UI.getCurrent().getInternals().getTitle()));
- 查找活动页面的Java代码
public static void executeShepherd(String appTitle) {
switch (appTitle) {
case "example":
UI.getCurrent().getPage().executeJs("window.startTour($0)", <这里我想引用具有方法的现有类>);
}
}
- 执行用户所在的页面上的Shepherd Tour(为此,我需要传递一个持有执行方法的类的引用以通过JavaScript执行),在标题中看起来是这样的:
window.startTour = (classParameter) => { ... }
-
JavaScript获取参数(该类)
-
从JavaScript中使用Shepherd,但存在对Java方法的引用(这些方法用于Vaadin组件,以打开和关闭相应的选项卡,我想在旅游中使用它)。
beforeShowPromise: function () {
return new Promise(function (resolve) {
classParameter.$server.<method()>;
resolve();
});
},
如何实现这一点?
英文:
i currently have the problem, that i want to use a reference of an existing java class in javascript to be able to execute methods from Javascript of the Java Class to use Shepherd (Javascript Tool for guiding users through the web app). Im using Vaadin for Web deployment with java but Shepherd only works through Javascript.
I dont want to execute the JS from the Class with the methods, moreover i want to execute it from an other class, so it looks like this:
- User clicks on a Button
> Button button = new Button(); button.addClickListener (c -> tourPageController.executeShepherd(UI.getCurrent().getInternals().getTitle());
- Java Code to find active Page
> public static void executeShepherd(String appTitle) {switch(appTitle) {case "example": UI.getCurrent().getPage().executeJs("window.startTour($0)", <here i want to reference the existing class with the methods);
- execute the Shepherd Tour four the explicit page, the user is on (For that, i need to hand over
a reference of the class holding the methods to execute via javascript), its looks something like this in the header:
> window.startTour = (<classParameter>) => { ... }
-
Javascript is getting the parameter (the class)
-
From Javascript im going to use Shepherd, but there are references to Java Methods (these Methods are for the Vaadin Components, to open and close the respectively tabs i want to use in the tour). I use it like this:
beforeShowPromise: function () { return new Promise(function(resolve) { classParameter.$server.<method()>; resolve(); }); },
How can i do this?
答案1
得分: 1
你可以在Java中将id设置给组件:
component.setId("component-x");
这意味着DOM元素将具有设置的id属性。
因此,如果你在Java代码中对组件进行某些记录,可以在@ClientCallable
调用中将id作为参数传递,然后在Java代码中查找与该id匹配的组件。
如果这仅涉及特定视图的一个组件,那么你不需要存储组件的引用以外的任何东西(甚至不需要id)。但是,如果你的视图中确实有多个Accordion
,则思路如下:
public class MyView extends Div {
List<Accordion> accordions;
public MyView() {
Accordion accordion = new Accordion();
accordion.setId("accordion-1");
accordions.add(accordion);
...
}
@ClientCallable
public void openAccordion(String id, int index) {
accordions.stream().filter(acc ->
acc.getId().equals(id)).findFirst().ifPresent(acc -> acc.open(null));
accordion.open(index);
}
}
有关上下文,请参见:https://stackoverflow.com/questions/75457945/how-can-i-change-vaadin-components-in-java-through-javascript/75459882
英文:
What you can do is to set id to the component in Java
component.setId("component-x");
This will mean that the DOM element will be have the id attribute set.
So if you have some bookkeeping of the components in your Java code, you can pass the id as a parameter in that @ClientCallable
call, and then search for the component matching that id in your Java code.
If this is about only one view specific component, you do not need anything else than storing reference to the component in a class field (not even the id). But if you indeed have more than one Accordion
in your view, the idea is like this.
public class MyView extends Div {
List<Accordion> accordions;
public MyView() {
Accordion accordion = new Accordion();
accordion.setId("accordion-1");
accordions.add(accordion);
...
}
@ClientCallable
public openAccordion(String id, int index) {
accordions.stream().filter(acc ->
acc.getId().equals(id)).findFirst().ifPresent(acc -> acc.open(null));
accordion.open(index);
}
}
For context, see: https://stackoverflow.com/questions/75457945/how-can-i-change-vaadin-components-in-java-through-javascript/75459882
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论