first at all, I'm very impressed with Primefaces components.
And sorry for my English
My problem:
I want to create programmatically a dataTable from the backing Bean like BalusC do in his post http://balusc.blogspot.com/2006/06/usin ... cDatatable, but with the primefaces library. That is, instead HtmlInputText using InputText from primefaces, etc.
I want to use the primefaces library to use the in-cell editing and pagination facilities among others.
This is the code I use (not all code) to create de datatable from the backing bean (create the datatable programmatically):
The xhtml:
Code: Select all
...
<h:form id="formulario1">
<p:dataTable id="dataTable"
binding="#{backingBean.dynamicDataTable}">
</p:dataTable>
</h:form>
...
Code: Select all
public DataTable getDynamicDataTable() {
dynamicDataTable = new DataTable();
//dynamicDataTable.setId("id_datatble");
UIOutput cabec = new UIOutput();
cabec.setValue("El Detalle");
dynamicDataTable.getFacets().put("header", cabec);
dynamicDataTable.setValueExpression("binding",
createValueExpression("#{backingBeab.dynamicDataTable}", HtmlDataTable.class));
dynamicDataTable.setValueExpression("value",
createValueExpression("#{"BackingBean.resultList}", List.class));
dynamicDataTable.setVar("dataItem");
Class<?>[] parametros = new Class<?>[1] ;
parametros[0] = RowEditEvent.class;
dynamicDataTable.setRowEditListener(
createActionExpression("#{"backingBean.editListener}", String.class, parametros));
dynamicDataTable.setPaginator(true);
dynamicDataTable.setPaginatorPosition("bottom");
dynamicDataTable.setRows(5);
dynamicDataTable.setRowsPerPageTemplate("5,10,15,20,50,100");
dynamicDataTable.setPaginatorTemplate("{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}");
dynamicDataTable.setTransient(true);
// Iterate over columns.
for (int i = 0; i < DBColumns.length; i++) {
// Create <h:column>.
Column column = new Column();
column.setHeaderText(colAlias[i]);
dynamicDataTable.getChildren().add(column);
CellEditor cellEditor = new CellEditor();
column.getChildren().add(cellEditor);
ValueExpression valueExp = null;
HtmlInputText input = new HtmlInputText();
cellEditor.getFacets().put("input", input);
String convClass = (resultList.get(0).getLista().get(i).getClass().getName());
try {
valueExp = createValueExpression("#{dataItem.lista["+i+"]}", Class.forName(convClass));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
input.setValueExpression("value", valueExp);
convClass = convClass.substring(convClass.lastIndexOf(".")+1);
if (convClass.equals("Integer")){
input.setConverter(integerC);
}else{
input.setConverter(floatC);
}
HtmlOutputText output = new HtmlOutputText();
cellEditor.getFacets().put("output", output);
output.setValueExpression("value", valueExp);
}
//The editing column
Column column2 = new Column();
column2.setHeaderText("Editar");
column2.getChildren().add(new RowEditor());
dynamicDataTable.getChildren().add(column2);
}
I click on the pencil icon and it shows the input fields in this row, then I change one field value and click the V mark. But in this moment I see that the datatable sends the same AJAX request as columns is displaying. This could be because when I see the html code with firebug the rows of the table are created with the id, and I don't know why (the encodeAll method from UIComponentBase possibly??), and the javascript use the id of the row to send the AJAX requests. The other problem that comes it that the send value of the fields correspond with the value of the previous field, unless from the first that is the correct value.
May you know why happens this behauvior?
If I do it all from the xhtml the components works without problems. But I want to do it programmmatically beacause I don't know the number of columns it will to have, and I want to apply converters an valitadors dynamically.
Which is the difference from doing it from the backing bean (programmatically) and doing directly from the xhtml with tags?
Why do the datatable works when is created using tags and not when is creeated from the backing bean?
Somebody can help me with this issue?