在Vaadin中,是否有一种方法可以从JavaScript中引用现有的Java类?

huangapple go评论68阅读模式
英文:

Is there a way to use a reference to an existing java class from javascript in Vaadin?

问题

  1. 用户单击按钮
Button button = new Button();
button.addClickListener(c -> tourPageController.executeShepherd(UI.getCurrent().getInternals().getTitle()));
  1. 查找活动页面的Java代码
public static void executeShepherd(String appTitle) {
    switch (appTitle) {
        case "example":
            UI.getCurrent().getPage().executeJs("window.startTour($0)", <这里我想引用具有方法的现有类>);
    }
}
  1. 执行用户所在的页面上的Shepherd Tour(为此,我需要传递一个持有执行方法的类的引用以通过JavaScript执行),在标题中看起来是这样的:
window.startTour = (classParameter) => { ... }
  1. JavaScript获取参数(该类)

  2. 从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:

  1. User clicks on a Button

> Button button = new Button(); button.addClickListener (c -> tourPageController.executeShepherd(UI.getCurrent().getInternals().getTitle());

  1. 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);

  1. 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>) => { ... }

  1. Javascript is getting the parameter (the class)

  2. 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.&lt;method()&gt;;
             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(&quot;component-x&quot;);

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&lt;Accordion&gt; accordions;

    public MyView() {
        Accordion accordion = new Accordion();
        accordion.setId(&quot;accordion-1&quot;);
        accordions.add(accordion);
        ...
    }

    @ClientCallable
    public openAccordion(String id, int index) {
        accordions.stream().filter(acc -&gt; 
        acc.getId().equals(id)).findFirst().ifPresent(acc -&gt; 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

huangapple
  • 本文由 发表于 2023年2月16日 19:11:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定