<rich:extendedDataTable> 确实不能很好地处理水平滚动。事实上,开发人员似乎着手让水平滚动几乎不可能。
您可以在启用水平滚动的情况下将<rich:extendedDataTable> 放入<div>,但如果您留在那里,它将不起作用。 <rich:extendedDataTable> (div.extdt-innerdiv) 中嵌套的 <div>s 之一是绝对定位的,将其从文档流中移除。
作为参考,这是<rich:extendedDataTable>的基本输出结构,假设三个<rich:column>元素,宽度为100px,两条记录:
<div id="form_id:edt_id" class="extdt-maindiv rich-extdt-maindiv">
<div id="form_id:edt_id:od" class="extdt-outerdiv">
<div id="form_id:edt_id:innerd" class="extdt-innerdiv">
<table id="form_id:edt_id:tu" class="extdt-table-layout">
<colgroup id="form_id:edt_id:colgroup:header">
<col width="100" />
<col width="100" />
<col width="100" />
</colgroup>
<thead id="form_id:edt_id:header" class="extdt-thead">
<tr class="extdt-subheader rich-extdt-subheader">
<th id="form_id:edt_id:column_1_id" class="extdt-menucell extdt-subheadercell rich-extdt-subheadercell">Column Header 1</th>
<th id="form_id:edt_id:column_2_id" class="extdt-menucell extdt-subheadercell rich-extdt-subheadercell">Column Header 2</th>
<th id="form_id:edt_id:column_3_id" class="extdt-menucell extdt-subheadercell rich-extdt-subheadercell">Column Header 3</th>
</tr>
<tr class="extdt-table-filterrow rich-extdt-subheader"> <!-- only if filtering is enabled -->
<th class="extdt-subheadercell rich-extdt-subheadercell"><!-- omitted for example purposes --></th>
<th class="extdt-subheadercell rich-extdt-subheadercell"><!-- omitted for example purposes --></th>
<th class="extdt-subheadercell rich-extdt-subheadercell"><!-- omitted for example purposes --></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="3">
<div id="form_id:edt_id:sd" class="extdt-content">
<table id="form_id:edt_id:n" class="extdt-table-layout">
<colgroup id="form_id:edt_id:colgroup:body">
<col width="100" />
<col width="100" />
<col width="100" />
</colgroup>
<tbody id="form_id:edt_id:tb">
<tr id="form_id:edt_id:n:0" class="extdt-firstrow rich-extdt-firstrow">
<td id="form_id:edt_id:0:column_1_id" class="extdt-cell rich-extdt-cell">
<div class="extdt-cell-div">Column 1, Row 1</div>
</td>
<td id="form_id:edt_id:0:column_2_id" class="extdt-cell rich-extdt-cell">
<div class="extdt-cell-div">Column 2, Row 1</div>
</td>
<td id="form_id:edt_id:0:column_3_id" class="extdt-cell rich-extdt-cell">
<div class="extdt-cell-div">Column 3, Row 1</div>
</td>
</tr>
<tr id="form_id:edt_id:n:1" class="extdt-firstrow rich-extdt-firstrow">
<td id="form_id:edt_id:1:column_1_id" class="extdt-cell rich-extdt-cell">
<div class="extdt-cell-div">Column 1, Row 2</div>
</td>
<td id="form_id:edt_id:1:column_2_id" class="extdt-cell rich-extdt-cell">
<div class="extdt-cell-div">Column 2, Row 2</div>
</td>
<td id="form_id:edt_id:1:column_3_id" class="extdt-cell rich-extdt-cell">
<div class="extdt-cell-div">Column 3, Row 2</div>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
<tfoot id="form_id:edt_id:footer">
<tr class="extdt-footer rich-extdt-footer">
<td class="extdt-footercell rich-extdt-footercell" scope="colgroup" colspan="3">
<!-- table footer goes here if defined -->
</td>
</tr>
</tfoot>
</table>
</div>
</div>
<div id="form_id:edt_id:column_1_idmenu">
<script type="text/javascript">
// context menu script snipped for example purposes
</script>
</div>
<div id="form_id:edt_id:column_2_idmenu">
<script type="text/javascript">
// context menu script snipped for example purposes
</script>
</div>
<div id="form_id:edt_id:column_3_idmenu">
<script type="text/javascript">
// context menu script snipped for example purposes
</script>
</div>
</div>
您可以将水平滚动添加到 div.extdt-innerdiv,但 <rich:extendedDataTable> 的列自动调整大小功能 (ExtendedDataTable.DataTable_formId_edtId.calculateWidthsFromRatios()) 基本上会被此混淆,调整从组件的 maxWidth 开始的所有列的大小(源自div.extdt-maindiv) 的初始宽度为 20px 宽。
我试过了……
- 用
<div> 元素包装<rich:extendedDataTable> 并设置以下内容:
position: relative;
width: 100%;
overflow-x: auto;
- 所需的高度,否则由于下一个要点,您只会看到滚动条。
- 使
div.extdt-maindiv绝对定位
- 同时提供
div.extdt-outerdiv 和 div.extdt-innerdiv 静态定位和自动宽度
...但这似乎没有任何效果。
我认为这可能是因为我在 Firebug 中进行了大部分更改,而mainDiv.getWidth()(来自calculateWidthsFromRatios())正在检索缓存值mainDiv.element.boxWidth。该值在ClientUI.common.box.Box.setWidth() (common-scrollable-data-table.js) 中设置,并且只调用一次;如果我调整浏览器窗口的大小(在我的情况下,<rich:extendedDataTable> 的宽度为 100%),它不会再次被调用。
我将尝试在我的 CSS 文件中进行这些更改,看看是否一切都可以神奇地工作。不过,<rich:extendedDataTable> 的 JavaScript 相当复杂,而且没有记录在案,所以我可能在某处遗漏了一些东西。我会跟进我的结果。
编辑:在我的 CSS 中进行更改后,我仍然遇到了列缩短问题。
因此,为了避免创建包装 div,我向 div.extdt-innerdiv 添加了水平滚动:
.extdt-innerdiv { overflow-y: hidden; overflow-x: auto !important; }
然后,在<rich:extendedDataTable> 的页脚,我禁用了calculateWidthsFromRatios() 功能:
<rich:extendedDataTable id="edtId">
<!-- rest of extended data table -->
<f:facet name="footer">
<script type="text/javascript">
jQuery(function() {
// Disable ratio-based column resizing.
ExtendedDataTable.DataTable_formId_edtId.calculateWidthsFromRatios = function() {};
});
</script>
</f:facet>
</rich:extendedDataTable>
我使用表格的页脚来强制每次重新渲染组件时执行此 JavaScript。
即使使用此解决方案,用户也无法手动扩展列的宽度,因为 extended-data-table.js 中的 JavaScript 会阻止将列的大小调整为比 mainDiv.element.boxWidth 更宽。为了能够像这样调整大小,不妨向 JBoss 提交一个补丁来修复 <rich:extendedDataTable>,因为目前没有计划在 RichFaces 3.X 中修改其行为(根据 JBoss 社区 JIRA 中的 #RF-4871 )。
祝你好运。