Generating tables with filter from java (I have an issue)

Community Driven Extensions Project
Post Reply
MarcoAlonso1024
Posts: 4
Joined: 26 Oct 2012, 00:53

17 Nov 2012, 01:10

Hi

I made an XHTML file that displays a PRIMEFACES data table generated by code (with java objects) from a java bean.

I included the filter option, the filter worked fine the first time over the table and it worked again over the result of the first filter.

However the table never recover the original data, if I delete the text from the filter box it did not display the original data again.

Can someone help me please.

---------------------------------------------------------------------------
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
>
<h:head>
<title>Tabla dinamica 7 9</title>
</h:head>
<h:body>
<!-- ************************************************** -->

<h:form styleClass="jsfcrud_list_form">
<p:panel binding="#{tablaDinamica7_9.panel}" />
</h:form>


<!-- ************************************************** -->
</h:body>
</html>
---------------------------------------------------------------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Ejemplo07;

import java.io.Serializable;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIOutput;
import javax.faces.component.html.HtmlForm;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import javax.faces.model.ArrayDataModel;
import org.primefaces.component.column.Column;
import org.primefaces.component.datatable.DataTable;
import org.primefaces.component.panel.Panel;


@ManagedBean(name="tablaDinamica7_9")
@SessionScoped
public class TablaDinamica7_9 implements Serializable{
private Panel panel;
private String[] header;
private String[][] rows;
private ArrayDataModel model;
private ArrayDataModel filterModel;

public TablaDinamica7_9(){
header = retornarEncabezado(iniciarEncabezado());
rows = retornarDatos(iniciarDatos());
model = new MiModeloTabla(rows);
//filterModel = new MiModeloTabla();

}


public ArrayDataModel getFilterModel() {
return filterModel;
}

public void setFilterModel(ArrayDataModel filterModel) {
this.filterModel = filterModel;
}

public ArrayDataModel getModel() {
return model;
}

public void setModel(ArrayDataModel model) {
this.model = model;
}

public Panel getPanel() {
return panel;
}

public void setPanel(Panel panel) {
this.panel = panel;
}

@PostConstruct
public void init() {
FacesContext fc = FacesContext.getCurrentInstance();
Application application = fc.getApplication();
panel = (Panel) application.createComponent(Panel.COMPONENT_TYPE);
panel.setHeader("Panel");
refreshChildren();
}

private void refreshChildren(){
panel.getChildren().clear();
FacesContext fc = FacesContext.getCurrentInstance();
Application application = fc.getApplication();
ExpressionFactory ef = application.getExpressionFactory();
ELContext elc = fc.getELContext();


HtmlForm myForm = new HtmlForm();


DataTable table = (DataTable) application.createComponent(DataTable.COMPONENT_TYPE);
table.setId("tabla1");
ValueExpression laTabla = ef.createValueExpression(elc, "#{tablaDinamica7_8.model}", ArrayDataModel.class);
table.setValueExpression("value", laTabla);

table.setVar("item");

ValueExpression elFiltro = ef.createValueExpression(elc, "#{tablaDinamica7_8.filterModel}", ArrayDataModel.class);
table.setValueExpression("filteredValue", elFiltro);

UIOutput tableTitle = (UIOutput) application.createComponent(UIOutput.COMPONENT_TYPE);
table.getFacets().put("header", tableTitle);
if((header != null) && (rows != null)){
if(header.length == rows[0].length){
for(int i = 0;i<header.length;i++){
Column nameColumn = (Column) application.createComponent(Column.COMPONENT_TYPE);
nameColumn.setId("columna" + i);
UIOutput nameColumnTitle = (UIOutput) application.createComponent(UIOutput.COMPONENT_TYPE);
nameColumnTitle.setValue(header);
nameColumn.getFacets().put("header", nameColumnTitle);
table.getChildren().add(nameColumn);
ValueExpression nameValueExp = ef.createValueExpression(elc, "#{item[" + i + "]}", String.class);
HtmlOutputText nameOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
nameOutput.setValueExpression("value", nameValueExp);

nameColumn.getChildren().add(nameOutput);


ValueExpression filterByValueExp = ef.createValueExpression(elc, "#{item[" + i + "]}", String.class);
nameColumn.setValueExpression("filterBy", filterByValueExp);
nameColumn.setFilterMatchMode("contains");
}
}
}
////////////////////////////////////////
myForm.getChildren().add(table);
panel.getChildren().add(myForm);
}





private String[] retornarEncabezado(ArrayList<String> datos){
String retorno[] = new String[datos.size()];

for(int i = 0;i < retorno.length;i++){
retorno = datos.get(i);
}

return retorno;
}

private String[][] retornarDatos(ArrayList<ArrayList<String>> datos){
String retorno[][] = null;

retorno = new String[datos.size()][datos.get(0).size()];

for(int i = 0;i < datos.size();i++){

ArrayList<String> aux = datos.get(i);

for(int j = 0;j < aux.size();j++){
retorno[j] = aux.get(j);
}
}
return retorno;
}

private ArrayList<String> iniciarEncabezado(){
ArrayList<String> retorno = new ArrayList<String>();

retorno.add("Columna 1");
retorno.add("Columna 2");
retorno.add("Columna 3");
retorno.add("Columna 4");

return retorno;
}

private ArrayList<String> iniciarCeldas(String datos){
ArrayList<String> retorno = new ArrayList<String>();

String elementos[] = datos.split("__");

for(int i = 0; i < elementos.length;i++){
retorno.add(elementos);
}

return retorno;
}

private ArrayList<ArrayList<String>> iniciarDatos(){
ArrayList<ArrayList<String>> retorno = new ArrayList<ArrayList<String>>();

for(int i = 1;i <= 64;i++){
retorno.add(iniciarCeldas("celda " + i + ".1__celda " + i + ".2__celda " + i + ".3__celda " + i + ".4"));
}

return retorno;
}
}
---------------------------------------------------------------------------

User avatar
Oleg
Expert Member
Posts: 3803
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

17 Nov 2012, 21:14

Try to post the question in the core forum. It's not related to the PF Extensions.
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

Post Reply

Return to “Extensions”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest