【问题标题】:How to load a combobox with vaadin fusion如何使用 vaadin fusion 加载组合框
【发布时间】:2022-01-25 12:55:00
【问题描述】:

您好,我正在使用 vaadin 启动器来了解有关 vaadin 的更多信息。

我刚开始一个新项目(Java+Typescript)

我有问题要解决。

我有一个UsersRol 实体,RolUser 的一个属性,问题是当我设置使用 vaading start 创建的视图时,我试图设置一个组合框来加载用于创建新用户的角色,但到目前为止没有任何作用。

在 vaading 网页的教程中,他们以与 vaadin start 创建的拱门和文件不同的方式解决了这个问题,所以我认为这可能是另一种方法。

我的实体

用户

package com.example.application.data.entity;

import com.vaadin.fusion.Nonnull;
import com.example.application.data.AbstractEntity;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;



@Entity
public class Users extends AbstractEntity {


    @ManyToOne
    @Nonnull
    private Rol rol;

    
    public Rol getRol() {
        return rol;
    }
    public void setRol(Rol rol) {
        this.rol = rol;
    }

}

角色

package com.example.application.data.entity;

import com.vaadin.fusion.Nonnull;
import com.example.application.data.AbstractEntity;
import javax.persistence.Entity;


@Entity
public class Rol extends AbstractEntity{
    
    @Nonnull
    private String name;

    @Nonnull
    private String description;

    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}


为了在我的users-view.ts 中选择一个角色,我应该怎么做才能将它加载到所有角色中

<vaadin-combo-box label="Rol" id="rol" ${field(this.binder.model.rol)} item-label-path="name"></vaadin-combo-box>

现在我明白了

How the combobox shows

提前谢谢各位。

【问题讨论】:

  • 我做到了,问题是我缺乏 typecrypt languaje 技能造成的。问题是我做了我的研究并且可以做到。 @state() private status: Status[] = []; 刚刚添加了这一行并使用一种方法从StatusRepository.java 中获取所有项目
  • 您能否制定您的解决方案作为对这个问题的回答,这样它就不会悬而未决。在这里回答自己的问题是完全可以的。

标签: java typescript combobox vaadin


【解决方案1】:

我的解决方案是:

在我的 typecrypt 类中添加了 tis 行

@state()
private roles: Rol[] = [];

@state 来自'lit/decorators.js'

然后在connectedCallback函数中添加了这一行

this.roles = await RolesEndpoint.listAll();

listAll() 是我在 endpint 类上创建的一个方法。

像这样:

@Nonnull
public List<@Nonnull Rol> listAll() {
   return service.listAll();
}

在我的服务类中

public List<Rol> listAll() {
   return repository.findAll();
}

现在您可以调用组合框元素中的数据

<vaadin-combo-box .items=${this.roles} label="Rol" id="rol"  ${field(this.binder.model.rol)} item-label-path="name" item-value-path="id"></vaadin-combo-box>

我希望这会有所帮助。

【讨论】:

  • 如果我有一个简单字符串列表,那么如何填充它?只是给 .items=${this.mylist} ?还是需要其他任何东西?
  • @kushalBaldev 是的,您可以这样做,在这种特殊情况下,我需要这样做。从数据库加载以获取所有项目。但如果它已经在 ts 文件上创建,你可以这样做。直接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多