好的,所以现在我找到了解决方案。
首先,该解决方案意味着对 Primefaces 进行一些调整。事实上,您需要启用将属性“colspan”添加到包含在 DataTable 中的“column”标签的可能性。
为此,您需要创建以下包:“org.primefaces.component.datatable”并创建一个名为“MyDataTableBeanRenderer”的类,该类将扩展实际的“DataTableBeanRenderer”。
在此类中,您将需要“覆盖”处理 DataTable 的正文单元格呈现 (encodeRegularCell)、行扩展呈现和分页器呈现(如果使用其中之一)的方法。
您还必须添加一个方法来检索标题列组的第二行中包含的行数。如果数据表的第二级包含的行多于第二级,则将调整当前的“td”或“th”列跨度(取决于方法)。在每种方法中,检查“getMaximumNumberOfColumns”的位置
现在,这是相应的 Java 代码:
@Override
protected void encodeRegularCell(FacesContext context, DataTable table, Column column, String clientId,
boolean selected) throws IOException {
final ResponseWriter writer = context.getResponseWriter();
final boolean selectionEnabled = column.getSelectionMode() != null;
final String style = column.getStyle();
String styleClass = "";
if (selectionEnabled) {
styleClass = DataTable.SELECTION_COLUMN_CLASS;
} else if (column.getCellEditor() != null) {
styleClass = DataTable.EDITABLE_COLUMN_CLASS;
}
styleClass = column.getStyleClass() == null ? styleClass : styleClass + " " + column.getStyleClass();
writer.startElement("td", null);
writer.writeAttribute("role", "gridcell", null);
if (column.getColspan() != 1) {
writer.writeAttribute("colspan", column.getColspan(), null);
}
if (style != null) {
writer.writeAttribute("style", style, null);
}
if (!isValueBlank(styleClass)) {
writer.writeAttribute("class", styleClass.trim(), null);
}
if (selectionEnabled) {
writer.startElement("div", null);
writer.writeAttribute("class", DataTable.COLUMN_CONTENT_WRAPPER, null);
encodeColumnSelection(context, table, clientId, column, selected);
writer.endElement("div");
} else {
writer.startElement("div", null);
writer.writeAttribute("class", DataTable.COLUMN_CONTENT_WRAPPER, null);
column.encodeAll(context);
writer.endElement("div");
}
writer.endElement("td");
}
@Override
protected void encodePaginatorMarkup(FacesContext context, DataTable table, String position, String tag,
String styleClass) throws IOException {
final ResponseWriter writer = context.getResponseWriter();
if (!table.isPaginatorAlwaysVisible() && table.getPageCount() <= 1) {
return;
}
final String id = table.getClientId(context) + "_paginator_" + position;
writer.startElement("tr", null);
writer.startElement(tag, null);
writer.writeAttribute("id", id, null);
writer.writeAttribute("class", styleClass, null);
final Integer rowsCount = getMaximumNumberOfColumns(table);
if (rowsCount == null || (rowsCount != null && rowsCount < table.getColumnsCount())) {
writer.writeAttribute("colspan", table.getColumnsCount(), null);
} else {
writer.writeAttribute("colspan", rowsCount, null);
}
final String[] elements = table.getPaginatorTemplate().split(" ");
for (final String element : elements) {
final PaginatorElementRenderer renderer = paginatorElements.get(element);
if (renderer != null) {
renderer.render(context, table);
} else {
writer.write(element + " ");
}
}
writer.endElement(tag);
writer.endElement("tr");
}
@Override
protected void encodeRowExpansion(FacesContext context, DataTable table) throws IOException {
final ResponseWriter writer = context.getResponseWriter();
final Map<String, String> params = context.getExternalContext().getRequestParameterMap();
final int expandedRowIndex = Integer.parseInt(params.get(table.getClientId(context) + "_expandedRowIndex"));
final String rowIndexVar = table.getRowIndexVar();
table.setRowIndex(expandedRowIndex);
if (rowIndexVar != null) {
context.getExternalContext().getRequestMap().put(rowIndexVar, expandedRowIndex);
}
writer.startElement("tr", null);
writer.writeAttribute("style", "display:none", null);
writer.writeAttribute("class", DataTable.EXPANDED_ROW_CONTENT_CLASS + " ui-widget-content", null);
writer.startElement("td", null);
final Integer rowsCount = getMaximumNumberOfColumns(table);
// Fix the number of columns of the second level with its appropriate
// number of rows
if (rowsCount == null || (rowsCount != null && rowsCount < table.getColumnsCount())) {
writer.writeAttribute("colspan", table.getColumnsCount(), null);
} else {
writer.writeAttribute("colspan", rowsCount, null);
}
table.getRowExpansion().encodeAll(context);
writer.endElement("td");
writer.endElement("tr");
table.setRowIndex(-1);
}
// get the maximum number of rows if the datatable can be expanded
private Integer getMaximumNumberOfColumns(DataTable table) {
Integer count = null;
// If the datatable contains a column group it means it can be expanded
// and contains 2 levels
if (table.getChildren().get(0) != null && table.getChildren().get(0) instanceof ColumnGroup) {
final ColumnGroup columnGroup = (ColumnGroup) table.getChildren().get(0);
if (columnGroup.getChildren().get(1) != null && columnGroup.getChildren().get(1) instanceof Row) {
final Row row = (Row) columnGroup.getChildren().get(1);
if (row.getChildren() != null) {
count = row.getChildren().size();
}
}
}
return count;
}
这是我的视图中包含的“dataTable”部分,它适合被覆盖的函数所处理的行为。
<p:columnGroup type="header">
<p:row>
<p:column >
</p:column>
<p:column headerText="#{bundle.form_name}" filterBy="#{item.name}"
sortBy="#{item.name}" colspan="3">
</p:column>
<p:column headerText="#{bundle.form_designation}"
style="width:140px;" filterBy="#{item.designation}"
sortBy="#{item.designation}" colspan="2">
</p:column>
<p:column headerText="#{bundle.form_status}"
filterBy="#{item.status}"
filterOptions="#{itemStatusListBean.options}">
</p:column>
<p:column headerText="#{bundle.form_date}"
filterBy="#{item.statusDate}" sortBy="#{item.statusDate}">
</p:column>
</p:row>
<p:row>
<p:column />
<p:column headerText="Status" />
<p:column headerText="Type" />
<p:column headerText="Marché" />
<p:column headerText="Groupe" />
<p:column headerText="Produit" />
<p:column headerText="Désignation Produit" />
<p:column headerText="EAN" />
</p:row>
</p:columnGroup>
<p:column style="width:4%">
<p:rowToggler/>
</p:column>
<p:column colspan="3">
<h:outputText value="#{item.name}" />
</p:column>
<p:column colspan="2">
<h:outputText value="#{item.designation}" />
</p:column>
<p:column>
<h:outputText value="#{item.status.label}" />
</p:column>
<p:column>
<h:outputText value="#{item.statusDate}">
<f:convertDateTime pattern="dd/MM/yyyy" timeZone="Europe/Paris" />
</h:outputText>
</p:column>
<p:rowExpansion>
<p:dataTable id="relationDataTable"
widgetVar="relationDataTable" var="relation"
value="#{relationDataTableBean.dataModel}"
rows="10">
<p:column></p:column>
<p:column><h:outputText value="#{relation.status}"/></p:column>
<p:column><h:outputText value="#{relation.type}"/></p:column>
<p:column><h:outputText value="#{relation.market}"/></p:column>
<p:column><h:outputText value="#{relation.group}"/></p:column>
<p:column><h:outputText value="#{relation.produit}"/></p:column>
<p:column><h:outputText value="#{relation.desiProduit}"/></p:column>
<p:column><h:outputText value="#{relation.ean}"/></p:column>
</p:dataTable>
</p:rowExpansion>
所以基本上,只需将 colspan 值放入必须从第二级打包的标题列的第一行标题列。
在这种情况下,请检查处理“名称”和“名称”的列。
<p:column headerText="#{bundle.form_name}" filterBy="#{item.name}"
sortBy="#{item.name}" colspan="3">
</p:column>
<p:column headerText="#{bundle.form_designation}"
style="width:140px;" filterBy="#{item.designation}"
sortBy="#{item.designation}" colspan="2">
</p:column>
然后,将相同的“colspan”属性和值添加到相应的列索引中,但在 dataTable 主体中(旨在显示对象数据的列):
<p:column colspan="3">
<h:outputText value="#{item.name}" />
</p:column>
<p:column colspan="2">
<h:outputText value="#{item.designation}" />
</p:column>
我的实现,在某些情况下可能会出现一些默认值,但它在大多数情况下都能正常工作。如果有一天你遇到同样的问题,我希望它会有所帮助。