How to programatically add p:rowExpansion.

UI Components for JSF
Post Reply
k.satou
Posts: 4
Joined: 22 Apr 2016, 19:43

25 Apr 2016, 18:21

Hi, all.

I trying to create custom dataTable. My dataTable will display p:rowExpansion, I want to add p:rowExpansion programatically.

I created the renderer, here. DefactColunmDataTable is the datatable extended primefaces p:dataTable.
I successfully display p:rowExpansion, but I don't know how to conditionally add rowExpansion.

Code: Select all

@SuppressWarnings("unchecked")
@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException
{
	DefactColunmDataTable dcDataTable = (DefactColunmDataTable)component;

	// get value
	List<DefectRecordContainer> containers = (List<DefectRecordContainer>)dcDataTable.getValue();

	for (int i=0; i< containers.size(); i++)
	{
		DefectRecordContainer container = containers.get(i);
		List<DefectRecord> drList = container.getDefectList();
                // set p:rowExpansion to dataTable.
		setRowExpansions(context, dcDataTable, drList);
	}
        // render dataTable contents.
	super.encodeEnd(context, dcDataTable);
}

private void setRowExpansions(FacesContext context, DefactColunmDataTable component, List<DefectRecord> defectList) throws IOException
{
        // create rowExpansion.
	RowExpansion rowEx = new RowExpansion();

	HtmlPanelGroup outerDivPanelGroup = new HtmlPanelGroup();
	outerDivPanelGroup.setLayout("block");

	for (DefectRecord dr : defectList)
	{
		HtmlPanelGroup innerDivPanelGroup = new HtmlPanelGroup();
		// div
		innerDivPanelGroup.setLayout("block");
		innerDivPanelGroup.setStyleClass("i-grid-row");

		HtmlOutputText outputTxt = new HtmlOutputText();
		outputTxt.setValue(dr.getDefectMessage());
		innerDivPanelGroup.getChildren().add(outputTxt);
		outerDivPanelGroup.getChildren().add(innerDivPanelGroup);
	}
	if (outerDivPanelGroup.getChildCount() > 0)
	{
		rowEx.getChildren().add(outerDivPanelGroup);
	}

	// add rowExpansion to DataTable.
	component.getChildren().add(rowEx);
}

k.satou
Posts: 4
Joined: 22 Apr 2016, 19:43

27 Apr 2016, 19:22

I successfully and conditionally display p:rowExpansion! I override boolean encodeRow() method.
However, another problem which my dataTable sort not working occurred.

Code: Select all

@SuppressWarnings("unchecked")
	@Override
	public boolean encodeRow(FacesContext context, DataTable table, String clientId, int rowIndex, int columnStart, int columnEnd) throws IOException
	{
		DefectColunmDataTable dcDataTable = (DefectColunmDataTable)table;
		// get value
		List<DefectRecordContainer> containers = (List<DefectRecordContainer>)dcDataTable.getValue();
		DefectRecordContainer container = containers.get(rowIndex);
		List<DefectRecord> drList = container.getDefectList();
		// set p:rowExpansion
		configRowExpansions(context, table, drList);

		// get styleClass
		String rowStyleClass = getRowStyle(container);
		dcDataTable.setRowStyleClass(rowStyleClass);
		return super.encodeRow(context, dcDataTable, clientId, rowIndex, columnStart, columnEnd);
	}

private void configRowExpansions(FacesContext context, DataTable table, List<DefectRecord> defectList) throws IOException
	{
		// get rowExpansion
		RowExpansion rowExpansion = table.getRowExpansion();

		if (defectList == null || defectList.isEmpty())
		{
			//  add new rowExpansion
			if (rowExpansion == null)
			{
				RowExpansion rowEx = (RowExpansion)context.getApplication().createComponent(RowExpansion.COMPONENT_TYPE);
				rowEx.setRendered(false);
				table.getChildren().add(rowEx);
			}
			else
			{
				rowExpansion.setRendered(false);
			}
			return;
		}

		if (rowExpansion == null)
		{
			//add new rowExpansion
			RowExpansion newRE = createRowExpansion(context, defectList);
			table.getChildren().add(newRE);
		}
		else
		{
			// clear rowExpansion contents
			RowExpansion updateRE = createRowExpansion(context, defectList);
			rowExpansion.getChildren().clear();
			rowExpansion.getChildren().addAll(updateRE.getChildren());
		}

	}

	private RowExpansion createRowExpansion(FacesContext context, List<DefectRecord> defectList)
	{
		RowExpansion rowEx = (RowExpansion)context.getApplication().createComponent(RowExpansion.COMPONENT_TYPE);

		HtmlPanelGroup outerDivPanelGroup = new HtmlPanelGroup();
		outerDivPanelGroup.setLayout("block");

		for (DefectRecord dr : defectList)
		{
			HtmlPanelGroup innerDivPanelGroup = new HtmlPanelGroup();
			// div
			innerDivPanelGroup.setLayout("block");
			innerDivPanelGroup.setStyleClass("ui-grid-row");

			addSpacer(innerDivPanelGroup);

			HtmlPanelGroup spanPanelGroup = new HtmlPanelGroup();
			if (dr.getDefectLevel() == DefectLevel.ERROR)
			{
				HtmlPanelGroup icon = new HtmlPanelGroup();
				icon.setStyleClass("icon_error-triangle_alt");
				spanPanelGroup.getChildren().add(icon);
				addOutputText(spanPanelGroup, "ERROR");
			}
			else if (dr.getDefectLevel() == DefectLevel.WARNING)
			{
				// add icon
				HtmlPanelGroup icon = new HtmlPanelGroup();
				icon.setStyleClass("icon_blocked");
				spanPanelGroup.getChildren().add(icon);
				addOutputText(spanPanelGroup, "WARNING");
			}
			innerDivPanelGroup.getChildren().add(spanPanelGroup);

			addSpacer(innerDivPanelGroup);

			// add message
			addOutputText(innerDivPanelGroup, dr.getDefectMessage());

			outerDivPanelGroup.getChildren().add(innerDivPanelGroup);
		}
		rowEx.getChildren().add(outerDivPanelGroup);
		return rowEx;
	}


Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 32 guests