我想在Vaadin页面中显示两个标签(Providers
和Products
)。 Providers
应该包含一个包含一条记录的网格。
我为此编写了以下代码:
import java.util.ArrayList;
import java.util.List;
import com.vaadin.flow.component.Text;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.tabs.Tab;
import com.vaadin.flow.component.tabs.Tabs;
import com.vaadin.flow.router.Route;
@Route("")
public class MainView extends VerticalLayout {
public MainView() {
final Text header = new Text("Header");
add(header);
final Tab productsTab = new Tab("Products");
final Tab providersTab = new Tab("Providers");
final Grid<LoanProvider> providersGrid = new Grid<>();
providersGrid.addColumn(LoanProvider::getName).setHeader("Name");
providersGrid.addColumn(LoanProvider::getRegions).setHeader("Regions");
providersGrid.addColumn(LoanProvider::getUrl).setHeader("URL");
providersGrid.setSizeFull();
providersTab.add(providersGrid);
final Tabs tabs = new Tabs(providersTab, productsTab);
add(tabs);
providersGrid.setItems(getLoanProviders());
}
private List<LoanProvider> getLoanProviders() {
final List<LoanProvider> coll = new ArrayList<>();
final LoanProvider sample = new LoanProvider();
sample.setName("Provider name");
sample.setRegions("Region1");
sample.setUrl("http://www.someserver.com");
coll.add(sample);
return coll;
}
}
public class LoanProvider {
private String name;
private String regions;
private String url;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRegions() {
return regions;
}
public void setRegions(String regions) {
this.regions = regions;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
运行应用程序时,在Products
标签中看不到任何数据:
如何更改代码以显示网格内容?
呼叫setSizeFull
和设置高度没有帮助。
答案 0 :(得分:2)
“流程中的选项卡”不应包含页面内容,您现在要做的是将网格添加到选项卡本身中(在 Providers 文本之后)。
此页面上有一个示例Tabs with pages
https://vaadin.com/components/vaadin-tabs/java-examples
基本上,您应该将SelectedChangeListener
添加到选项卡组件,并根据单击的选项卡显示或隐藏网格(通过setVisible
或将其删除并将其重新添加到父)。
Vaadin目录中还有一个插件,它包装了上面示例的类似逻辑 https://vaadin.com/directory/component/paged-tabs