我在我的DataGrid
一个布局中使用 a 。我已经成功地指定了一些静态文本在没有结果时使用setEmptyTableWidget(Widget)
. 通常,我通过类似new HTML("No results")
.
但是...当我的列数超出了我在视口中的容量(即浏览器窗口的当前显示宽度)时,我看不到所有列标题,也无法滚动到(向右)查看它们。
我有一个布局,当有数据时支持滚动!但是没有数据时水平滚动条不会出现。我无法摆脱这个问题。
欢迎任何提示或建议。
这是我的布局...(您将在下面看到我setEmptyTableWidget
在 PagingDataTableDatGrid#initGrid(DataGridConfiguration)
's 方法实现中调用的位置)。
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:gwt="urn:import:com.google.gwt.user.client.ui"
xmlns:com="urn:import:org.spp.im.mui.gwt.client.module.common.widget.grid">
<ui:with field="res" type="org.spp.im.mui.gwt.client.resources.UiResources" />
<ui:with field="msg" type="org.spp.im.mui.gwt.shared.i18n.UiMessages" />
<!-- Layout -->
<gwt:DockLayoutPanel width="100%" height="100%" styleName="{res.style.container}" unit="PX">
<gwt:north size="25">
<gwt:HorizontalPanel width="100%">
<!-- Buttons -->
<gwt:cell horizontalAlignment="ALIGN_RIGHT">
<com:GridButtonBar ui:field="bar" />
</gwt:cell>
</gwt:HorizontalPanel>
</gwt:north>
<gwt:center>
<!-- Grid -->
<com:ReadOnlyGrid ui:field="grid" width="100%" />
</gwt:center>
<gwt:south size="25">
<gwt:HorizontalPanel width="100%">
<!-- Pagination controls -->
<gwt:cell horizontalAlignment="ALIGN_LEFT">
<com:CustomPager ui:field="pager" width="225px" />
</gwt:cell>
</gwt:HorizontalPanel>
</gwt:south>
</gwt:DockLayoutPanel>
</ui:UiBinder>
ReadOnlyGrid
是自定义实现...
public class ReadOnlyGrid extends PagingDataTableDataGrid<DataRow> {
private Set<Column<DataRow, ?>> columns = new HashSet<Column<DataRow, ?>>();
// preferred constructor in most cases
public ReadOnlyGrid() {
super();
}
public ReadOnlyGrid(final DataGridConfiguration config) {
super(config);
}
@Override
protected Set<Column<DataRow, ?>> allColumns() {
return columns;
}
@Override
// first column always has default ascending sort order
public void initTableColumns(final DataGrid<DataRow> dataGrid, final DataTable table,
final ListHandler<DataRow> sortHandler) {
Column<DataRow, String> column;
final int totalColumns = table.getColumnCount();
String columnName = null;
ColumnHints columnHints = null;
for (int col = 0; col < totalColumns; col++) {
final int c = col;
columnName = table.getColumnName(c);
columnHints = table.getColumnHints(columnName);
if (columnHints.isVisible()) {
column = new Column<DataRow, String>(new DataTableCell()) {
@Override
public String getValue(final DataRow row) {
String result = "";
if (row != null) {
result = row.get(c);
}
return result;
}
};
column.setSortable(true);
sortHandler.setComparator(column, new Comparator<DataRow>() {
@Override
public int compare(final DataRow r1, final DataRow r2) {
final String one = r1.get(c);
final String two = r2.get(c);
if (one == null ^ two == null) {
return one == null ? -1 : 1;
}
if (one == null && two == null) {
return 0;
}
return one.compareToIgnoreCase(two);
}
});
final String header = columnHints.getColumnHeader() == null ? columnName : columnHints.getColumnHeader();
// create the column header
// header should have the same alignment as the column data
dataGrid.addColumn(column, createHeader(header, columnHints.getColumnAlignment()));
// keep track of columns added
allColumns().add(column);
// Set the column width
dataGrid.setColumnWidth(column, columnHints.getColumnWidth(), Unit.PX);
// Set the column alignment
if (columnHints.getColumnAlignment() != null) {
column.setHorizontalAlignment(columnHints.getColumnAlignment());
}
else {
column.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);//default
}
if (c == 0) {
dataGrid.getColumnSortList().push(column);
}
}
}
}
@Override
protected void initTableRows(final DataTable table) {
setDataList(table.allRows());
}
private SafeHtml createHeader(final String columnHeaderHtml, HasHorizontalAlignment.HorizontalAlignmentConstant columnHeaderAlignment) {
columnHeaderAlignment = columnHeaderAlignment == null ? HasHorizontalAlignment.ALIGN_RIGHT : columnHeaderAlignment;
final String ch = replaceCrWithBr(columnHeaderHtml);
final SafeHtmlBuilder sb = new SafeHtmlBuilder();
sb.appendHtmlConstant("<div align=\"" + columnHeaderAlignment.getTextAlignString() + "\">")
.appendHtmlConstant(ch).appendHtmlConstant("</div>");
final SafeHtml header = sb.toSafeHtml();
return header;
}
private String replaceCrWithBr(final String value) {
String result = value;
if (value.contains(App.INSTANCE.carriage_return())) {
result = value.replace(App.INSTANCE.carriage_return(), App.INSTANCE.separator());
}
return result;
}
}
并且PagingDataTableDataGrid
...
public abstract class PagingDataTableDataGrid<T extends DataRow> extends Composite {
private DataGrid<T> dataGrid;
private ListDataProvider<T> dataProvider;
private ResizeLayoutPanel wrapper;
// default, use in UiBinder template when you just want to @UiField inject a subclass
public PagingDataTableDataGrid() {
this(new DefaultDataGridConfiguration());
}
// alternate, use in UiBinder template when you want to create an instance
// yourself, user to inject w/ @UiField(provided=true)
public PagingDataTableDataGrid(final DataGridConfiguration config) {
wrapper = new ResizeLayoutPanel();
wrapper.setWidth("100%");
wrapper.setStyleName(UiResources.INSTANCE.style().gridWrapper());
initWidget(wrapper);
initGrid(config);
}
private void initGrid(final DataGridConfiguration config) {
dataGrid = new DataGrid<T>(config.getPageSize(), config.getResources());
final HTML noResults = new HTML(UiMessages.INSTANCE.no_results());
dataGrid.setEmptyTableWidget(noResults);
wrapper.clear();
wrapper.add(dataGrid);
}
public void setInput(final DataTable table) {
resetTableColumns();
dataProvider = new ListDataProvider<T>();
dataProvider.setList(new ArrayList<T>());
final ListHandler<T> sortHandler = new ListHandler<T>(dataProvider.getList());
dataGrid.addColumnSortHandler(sortHandler);
initTableColumns(dataGrid, table, sortHandler);
initTableRows(table);
dataProvider.addDataDisplay(dataGrid);
// trigger sort client-side
ColumnSortEvent.fire(dataGrid, dataGrid.getColumnSortList());
GridUtil.setStyles(this.dataGrid);
}
protected abstract Set<Column<T, ?>> allColumns();
// see
// http://stackoverflow.com/questions/3772480/remove-all-columns-from-a-celltable
// concrete classes are forced to maintain a handle on all columns added
private void resetTableColumns() {
for (final Column<T, ?> column : allColumns()) {
dataGrid.removeColumn(column);
}
allColumns().clear();
}
/**
* Add columns to this grid
*
* @param dataGrid
* a GWT DataGrid implementation
* @param table
* the model data
* @param sortHandler
* a GWT ListHandler implementation
*/
protected abstract void initTableColumns(DataGrid<T> dataGrid, DataTable table, ListHandler<T> sortHandler);
/**
* Add rows to this grid
* @param table the model data
*/
protected abstract void initTableRows(DataTable table);
protected void setDataList(final List<T> dataList) {
final List<T> list = dataProvider.getList();
list.addAll(dataList);
dataProvider.refresh();
}
public DataGrid<T> getDataGrid() {
return dataGrid;
}
}