Spring boot Vaadin流网格-使用JPA实体时如何控制列?

Spring boot Vaadin流网格-使用JPA实体时如何控制列?,spring-boot,spring-data-jpa,vaadin,vaadin10,vaadin-flow,Spring Boot,Spring Data Jpa,Vaadin,Vaadin10,Vaadin Flow,如果我使用JPA和Vaadin10网格,它会以任意顺序将所有列添加到显示中 grid = new Grid<>(Thing.class); add(grid); 是否应该使用注释,或者是否需要隐藏列,或者删除所有列并手动添加它们?我在网上和这里搜索过,没有找到任何相关信息。您可以使用 grid.setColumns(“dataField1”、“dataField2”、“dataField3”) 等等 如果要更改字段的显示名称,可以使用grid.getColumnByKey(“dat

如果我使用JPA和Vaadin10网格,它会以任意顺序将所有列添加到显示中

grid = new Grid<>(Thing.class);
add(grid);

是否应该使用注释,或者是否需要隐藏列,或者删除所有列并手动添加它们?我在网上和这里搜索过,没有找到任何相关信息。

您可以使用
grid.setColumns(“dataField1”、“dataField2”、“dataField3”)
等等 如果要更改字段的显示名称,可以使用
grid.getColumnByKey(“dataField1”).setHeader(“自定义文本”)

import javax.annotation.PostConstruct;
导入org.springframework.beans.factory.annotation.Autowired;
导入com.vaadin.flow.component.button.button;
导入com.vaadin.flow.component.grid.grid;
导入com.vaadin.flow.component.orderedlayout.HorizontalLayout;
导入com.vaadin.flow.component.orderedlayout.VerticalLayout;
导入com.vaadin.flow.component.textfield.textfield;
导入com.vaadin.flow.router.Route;
@路线
公共类主视图扩展了垂直布局{
私有静态最终长serialVersionUID=3461787310452366610L;
私有网格sGrid=新网格(Student.class);
private TextField filter=new TextField();
专用HorizontalLayout工具栏=新建HorizontalLayout();
私有按钮newStudent=新按钮(“添加”);
@自动连线
研究性回购;
公共主视图(){
设置大小();
初始化组件();
添加(工具栏);
添加(sGrid);
}
私有组件(){
setColumns(“firstName”、“lastName”、“sid”、“uid”);
sGrid.getColumnByKey(“sid”).setHeader(“学生ID”);
sGrid.getColumnByKey(“uid”).setHeader(“唯一ID”);
toolbar.setSizeUndefined();
工具栏.添加(过滤器,newStudent);
filter.setPlaceholder(“搜索…”);
}
@施工后
私有void init(){
sGrid.setItems(repo.findAll());
}
}

您可以使用
grid.setColumns(“dataField1”、“dataField2”、“dataField3”)
等等 如果要更改字段的显示名称,可以使用
grid.getColumnByKey(“dataField1”).setHeader(“自定义文本”)

import javax.annotation.PostConstruct;
导入org.springframework.beans.factory.annotation.Autowired;
导入com.vaadin.flow.component.button.button;
导入com.vaadin.flow.component.grid.grid;
导入com.vaadin.flow.component.orderedlayout.HorizontalLayout;
导入com.vaadin.flow.component.orderedlayout.VerticalLayout;
导入com.vaadin.flow.component.textfield.textfield;
导入com.vaadin.flow.router.Route;
@路线
公共类主视图扩展了垂直布局{
私有静态最终长serialVersionUID=3461787310452366610L;
私有网格sGrid=新网格(Student.class);
private TextField filter=new TextField();
专用HorizontalLayout工具栏=新建HorizontalLayout();
私有按钮newStudent=新按钮(“添加”);
@自动连线
研究性回购;
公共主视图(){
设置大小();
初始化组件();
添加(工具栏);
添加(sGrid);
}
私有组件(){
setColumns(“firstName”、“lastName”、“sid”、“uid”);
sGrid.getColumnByKey(“sid”).setHeader(“学生ID”);
sGrid.getColumnByKey(“uid”).setHeader(“唯一ID”);
toolbar.setSizeUndefined();
工具栏.添加(过滤器,newStudent);
filter.setPlaceholder(“搜索…”);
}
@施工后
私有void init(){
sGrid.setItems(repo.findAll());
}
}

如果我的回答对你有帮助,你能接受它作为答案吗?这是一个很好的答案,但我还没有尝试过。我将保留这个问题,看看是否还有其他想法,特别是在可能的情况下使用注释。如果我的回答对你有帮助,你能接受它作为答案吗?这是一个很好的答案,但我还没有尝试过。我将保留这个问题,看看是否还有其他想法,特别是如果可能的话,使用注释。
@Entity
public class Thing {

   private static final Logger log = LoggerFactory.getLogger(Thing.class);

   @Id
   @GeneratedValue
   @Column(unique = true)
   private Long id;

   @Column(nullable = false, updatable = false)
   @CreationTimestamp
   private Instant creationDate;
   ...
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.Route;

@Route
public class MainView extends VerticalLayout {

private static final long serialVersionUID = 3461787310452366610L;
private Grid<Student> sGrid = new Grid<>(Student.class);
private TextField filter = new TextField();
private HorizontalLayout toolbar = new HorizontalLayout();
private Button newStudent = new Button("Add");
@Autowired
StudentRepository repo;

public MainView() {
    setSizeFull();
    initComponents();
    add(toolbar);
    add(sGrid);
}

private void initComponents() {
    sGrid.setColumns("firstName", "lastName", "sid", "uid");
    sGrid.getColumnByKey("sid").setHeader("Student ID");
    sGrid.getColumnByKey("uid").setHeader("Unique ID");

    toolbar.setSizeUndefined();
    toolbar.add(filter, newStudent);

    filter.setPlaceholder("Search...");

}

@PostConstruct
private void init() {
    sGrid.setItems(repo.findAll());
}

}