Thursday, October 3, 2013

JSF Generic Searchable Table with Primefaces

Just because http://www.primefaces.org/showcase/ui/datatableDynamicColumns.jsf is just "a little" over for me.

<?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:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Search</title>
</h:head>
<h:body>
    <h:form id="sm">
        <p:spacer height="10" />
        <p:fieldset legend="Search">
        <p:panel>
            <p:dataTable
                var="dataRow"
                value="#{MB.data}"
                paginator="true" rows="10" 
                paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}" 
                rowsPerPageTemplate="5,10,15"   
                   emptyMessage="No sites found with given criteria"
                   filteredValue="#{MB.filteredData}"
                   sortBy="#{MB.defaultColumnSort}"
                id="rowsTable">
                
                <p:columns value="#{MB.columns}" var="column" columnIndexVar="colIndex" sortBy="#{column}" filterBy="#{column}" filterMatchMode="contains"> 
                    <f:facet name="header">#{column}</f:facet> 
                    #{dataRow[column]} 
                   </p:columns>
                    
           </p:dataTable>
        </p:panel>
        </p:fieldset>
           
    </h:form>
</h:body>
</html>


and the MB goes like this

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class MB implements Serializable {
   
    private static final long serialVersionUID = -3964302189254255157L;
   
    private List<String> columns = new ArrayList<String>();
   
    private List<Map<String,String>> data;
   
    private List<Map<String,String>> filteredData;
   
    private String defaultColumnSort;

    public void clear() {
        data = null;
        setFilteredData(null);
    }

    @EJB
    private EJB ejb;
   
    @PostConstruct
    public void init() {
        try {
            open("xyz");
        } catch (Exception e) {
            e.printStackTrace();
            JSFUtil.addErrorMessage("Could not list");
        }
    }
   

    /**
     * entry point
     *
     * @return
     */
    public String open(String table){
        try {
            columns = ejb.getTableColumns(table);
            data = ejb.getTable(table);
            filteredData = data;
            defaultColumnSort = columns.get(0);
            return "thispage";
        } catch (Exception e) {
            e.printStackTrace();
            JSFUtil.addErrorMessage("Could not open");
        }
        return null;
    }
   
    public List<Map<String, String>> getData() {
        return data;
    }

    public void setData(List<Map<String, String>> data) {
        this.data = data;
    }


    public List<Map<String,String>> getFilteredData() {
        return filteredData;
    }


    public void setFilteredData(List<Map<String,String>> filteredData) {
        this.filteredData = filteredData;
    }


    public List<String> getColumns() {
        return columns;
    }


    public void setColumns(List<String> columns) {
        this.columns = columns;
    }


    public String getDefaultColumnSort() {
        return defaultColumnSort;
    }


    public void setDefaultColumnSort(String defaultColumnSort) {
        this.defaultColumnSort = defaultColumnSort;
    }
}

No comments:

Post a Comment