我正在使用 GridExtensionPack 附加组件,因此我可以将标头包装在我的 Vaadin 7 应用程序中。由于某种原因,它在初始加载时看起来不太好,需要一些“刷新”(因为缺少更好的术语)才能让它看起来不错/合理。我做错了什么?
这就是我的意思,这是我单击“关闭环绕”之前的样子,默认情况下环绕是打开的: .
我从这里改编了这个示例代码,以构建这个完整的类:
package com.mobiwms.website.view.demo;
import java.util.Random;
import org.vaadin.teemusa.gridextensions.cachestrategy.CacheStrategyExtension;
import org.vaadin.teemusa.gridextensions.client.tableselection.TableSelectionState.TableSelectionMode;
import org.vaadin.teemusa.gridextensions.tableselection.TableSelectionModel;
import org.vaadin.teemusa.gridextensions.wrappinggrid.WrappingGrid;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.server.Responsive;
import com.vaadin.shared.ui.MarginInfo;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.Grid;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.ValoTheme;
@SuppressWarnings("serial")
public class HeaderWrappingDemoWindow extends Window {
public static final String ID = "plan-info-window";
private HeaderWrappingDemoWindow(boolean wrapInitially ) {
addStyleName("profile-window");
setId(ID);
Responsive.makeResponsive(this);
setModal(true);
addCloseShortcut(KeyCode.ESCAPE, null);
setResizable(true);
setHeight(90.0f, Unit.PERCENTAGE);
setWidth(75.0f, Unit.PERCENTAGE);
setClosable(false);
VerticalLayout content = new VerticalLayout();
content.setSizeFull();
content.setMargin(new MarginInfo(true, false, false, false));
setContent(content);
content.addComponent(buildHeader());
HeaderWrapExtensionLayout info = new HeaderWrapExtensionLayout(wrapInitially);
content.addComponent(info);
content.setExpandRatio(info, 1f);
content.addComponent(buildFooter());
}
private Component buildHeader() {
HorizontalLayout footer = new HorizontalLayout();
footer.addStyleName(ValoTheme.WINDOW_BOTTOM_TOOLBAR);
footer.setWidth(100.0f, Unit.PERCENTAGE);
Button ok = new Button("Close");
ok.addStyleName(ValoTheme.BUTTON_PRIMARY);
ok.addClickListener(e->close());
ok.focus();
footer.addComponent(ok);
footer.setComponentAlignment(ok, Alignment.TOP_RIGHT);
return footer;
}
private Component buildFooter() {
HorizontalLayout footer = new HorizontalLayout();
footer.addStyleName(ValoTheme.WINDOW_BOTTOM_TOOLBAR);
footer.setWidth(100.0f, Unit.PERCENTAGE);
return footer;
}
public static void open(boolean wrapInitially ) {
Window w = new HeaderWrappingDemoWindow(wrapInitially);
UI.getCurrent().addWindow(w);
w.focus();
}
private class HeaderWrapExtensionLayout extends VerticalLayout {
private static final String BUTTON_WRAPPING_ENABLED_TEXT = "Turn wrapping off";
private static final String BUTTON_WRAPPING_DISABLED_TEXT = "Turn wrapping on";
private int state;
public HeaderWrapExtensionLayout(boolean wrapInitially) {
setMargin(true);
final Grid grid = new Grid();
final WrappingGrid wrap = WrappingGrid.extend(grid);
TableSelectionModel selectionModel = new TableSelectionModel();
selectionModel.setMode(TableSelectionMode.SHIFT);
grid.setSelectionModel(selectionModel);
generateData(grid, 5, 100);
Grid.HeaderRow headerRow = grid.prependHeaderRow();
headerRow.join(grid.getColumns().get(1).getPropertyId(), grid.getColumns().get(2).getPropertyId());
Grid.HeaderRow headerRow1 = grid.appendHeaderRow();
headerRow1.join(grid.getColumns().get(2).getPropertyId(), grid.getColumns().get(3).getPropertyId());
grid.setWidth("100%");
grid.setHeight("100%");
final Button button = new Button(BUTTON_WRAPPING_DISABLED_TEXT);
button.addClickListener(new Button.ClickListener() {
// int state = 0;
public void buttonClick(ClickEvent event) {
// state = (state + 1) % 2;
switch (state) {
case 0:
// Disable wrapping, attempt to restore original behavior
wrap.setWrapping(false);
button.setCaption(BUTTON_WRAPPING_DISABLED_TEXT);
state = 1;
break;
case 1:
// Apply wrapping rules
wrap.setWrapping(true);
button.setCaption(BUTTON_WRAPPING_ENABLED_TEXT);
state = 0;
break;
}
}
});
addComponent(button);
addComponent(grid);
CacheStrategyExtension.extend(grid, 5, 0.2d);
if(wrapInitially)
{
wrap.setWrapping(true);
button.setCaption(BUTTON_WRAPPING_ENABLED_TEXT);
state = 0;
}
}
private void generateData(Grid g, int cols, int rows) {
g.addColumn("#");
for (int x = 1; x < cols; ++x) {
g.addColumn("Column with really long title " + (x + 1), String.class);
}
Random r = new Random();
for (int y = 0; y < rows; ++y) {
String[] values = new String[cols];
values[0] = "" + (y + 1);
for (int x = 1; x < cols; ++x) {
values[x] = "" + r.nextInt() + " babies born last year";
}
g.addRow(values);
}
}
}
}
我有使用
HeaderWrappingDemoWindow.open(true)
打开窗口的代码,因此默认情况下打开环绕(在它显示给浏览器之前)。
我尝试了各种方法,但没有任何效果。我目前的工作理论是我需要在屏幕显示后打开环绕,但到目前为止没有人能告诉我该怎么做。我认为这是因为演示代码默认情况下没有打开它,它需要用户单击一个按钮。所以从不同的角度来攻击这个问题,并以不同的方式提出问题。