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

时间:2018-11-09 14:42:17

标签: spring-boot spring-data-jpa vaadin vaadin10 vaadin-flow

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

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

如何使其仅添加一两列,并控制顺序,大小,宽度等。

我的“东西”看起来像

@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;
   ...

我应该使用注释吗?还是需要隐藏列,或者删除所有列并手动添加它们?我已经在网上搜索,并且在此处搜索到SO,却未发现与之相关的任何内容。

1 个答案:

答案 0 :(得分:2)

您可以使用     grid.setColumns("dataField1","dataField2","dataField3") 等等。 如果要更改字段的显示名称,可以使用grid.getColumnByKey("dataField1").setHeader("Custom Text");

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());
}

}