您可以使用等设置网格列
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());
}
}