7/10/13

Primefaces Datatable with header Check-box Selection

Hi folks,

I got a chance to work with JSF, it was an interesting requirement. Its about a custom component which would be of more use to our product.

Component Description : 
 The display of the component (Datatable) will be row wise check-box and top header check-box.  Functionality will be by clicking each individual row check-box should select or unselect the row. By clicking the top header Check-box should select  or unselect all the rows within the page.

Prime Faces Datatable offers a wide verity of features.

PROS of Prime Faces MultiSelection:

PrimeFaces offers the Multiselection feature with ease.

CONS of Prime Faces MultiSelection:

MultiSelection Feature with LazyDataModel works well within the page of DataTable.

Work Around :

Implement the single selection with LazyDataModel in DataTable and customize to make it work of Multi Selection feature with PrimeFace-Ajax script and a ArrayList.

Code Snippets : 

DbClassCheck.Java :

This class is a simple db interaction class, here we are creating db connection and fetching records from a table with limit and offset. It should self Explanatory.

package beanclass;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class DbClassCheck {

    List<BeanforCheckbox> selectRecords(int lim,int off) throws SQLException{
        Connection con=null;
         Statement st = null;
         ResultSet rs=null;
         List<BeanforCheckbox> d=null;
        try{
         Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testing?zeroDateTimeBehavior=convertToNull", "root", "root");
            st=con.createStatement();
            rs=st.executeQuery("select name,description,status,id from testnew limit "+lim+" offset "+off);
            d = new ArrayList<BeanforCheckbox>();
             while (rs.next()) {
            //    System.out.println("dddd");
                d.add(new BeanforCheckbox(rs.getString(1), rs.getString(2), rs.getString(3), rs.getInt(4),false));
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
         finally{
             if(con!=null){
                 con.close();
             }
         }
         return d;
    }
    List<BeanforCheckbox> selectRecordstrue(int lim,int off) throws SQLException{
        Connection con=null;
         Statement st = null;
         ResultSet rs=null;
         List<BeanforCheckbox> d=null;
        try{
         Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testing?zeroDateTimeBehavior=convertToNull", "root", "root");
            st=con.createStatement();
            rs=st.executeQuery("select name,description,status,id from testnew limit "+lim+" offset "+off);
            d = new ArrayList<BeanforCheckbox>();
             while (rs.next()) {
             //   System.out.println("dddd");
                d.add(new BeanforCheckbox(rs.getString(1), rs.getString(2), rs.getString(3), rs.getInt(4),true));
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
         finally{
             if(con!=null){
                 con.close();
             }
         }
         return d;
    }
}

MultiBean.Java :

This Class is just a sample class which i used to store header checkbox value. In the provided source code you will find irrelevant snippets because i needed some class so made use of one of my existing class.

Important Snippets :

   private  boolean chk=false;

    public boolean isChk() {
        return chk;
    }

    public void setChk(boolean chk) {
        this.chk = chk;
    }

Full class :- 

package beanclass;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;
import org.primefaces.model.*;
@ManagedBean
@SessionScoped
public class MultiBean implements Serializable {

    private String name;
    private DataTableClass[] dtc;
    private List<columnModel> columns;
private  boolean chk=false;

    public boolean isChk() {
        return chk;
    }

    public void setChk(boolean chk) {
        this.chk = chk;
    }
    public SelectionModel getSmodel() {
        return smodel;
    }
    private String columnName;
    private SelectionModel smodel;

    public DataTableClass[] getDtc() {
        return dtc;
    }

    public void setDtc(DataTableClass[] dtc) {
        this.dtc = dtc;
    }
    private ArrayList<DataTableClass> alist = new ArrayList<DataTableClass>();

    public ArrayList<DataTableClass> getAlist() {
        return alist;
    }

    @PostConstruct
    public void onload() {
        alist.add(new DataTableClass("1", "raghu", "jsf", 1));
        alist.add(new DataTableClass("2", "gopal", "jsf", 2));
        alist.add(new DataTableClass("3", "noorul", "flex", 3));
        alist.add(new DataTableClass("4", "raja", "html", 4));
        alist.add(new DataTableClass("5", "udhay", "html", 5));
         alist.add(new DataTableClass("11", "raghu1", "jsf1", 11));
        alist.add(new DataTableClass("12", "gopal1", "jsf1", 12));
        alist.add(new DataTableClass("13", "noorul1", "flex1", 13));
        alist.add(new DataTableClass("14", "raja1", "html1", 14));
        alist.add(new DataTableClass("15", "udhay1", "html1", 15));
         alist.add(new DataTableClass("21", "raghu2", "jsf2", 21));
        alist.add(new DataTableClass("22", "gopal2", "jsf2", 22));
        alist.add(new DataTableClass("23", "noorul2", "flex2", 23));
        alist.add(new DataTableClass("24", "raja2", "html2", 24));
        alist.add(new DataTableClass("25", "udhay2", "html2", 25));
         alist.add(new DataTableClass("31", "raghu3", "jsf", 31));
        alist.add(new DataTableClass("32", "gopal3", "jsf", 32));
        alist.add(new DataTableClass("33", "noorul3", "flex", 33));
        alist.add(new DataTableClass("34", "raja3", "html", 34));
        alist.add(new DataTableClass("35", "udhay3", "html", 35));
         alist.add(new DataTableClass("41", "raghu4", "jsf4", 41));
        alist.add(new DataTableClass("42", "gopal4", "jsf4", 42));
        alist.add(new DataTableClass("43", "noorul4", "flex4", 43));
        alist.add(new DataTableClass("44", "raja4", "html4", 44));
        alist.add(new DataTableClass("45", "udhay4", "html4", 45));
        smodel = new SelectionModel(alist);
        //createDynamicColumns();
    }

    public String getNamenew() {
        return namenew;
    }

    public void setNamenew(String namenew) {
        this.namenew = namenew;
    }
    private String namenew;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

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

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public MultiBean(String name) {
        this.name = name;
    }

    public MultiBean() {
    }

    public void sayHello(String s) {
        System.out.println("added parameter.......>>>>> " + s + getName());

    }

    public void sayHelloNew() {
        System.out.println("say Hello world New......" + getNamenew());


    }

    public void onRowSelect(SelectEvent event) {
        try {
            //  setDtc(((DataTableClass) event.getObject()));
            //   DataTableClass d = getDtc();
            //   System.out.println("d---->" + d.getName());
            FacesContext.getCurrentInstance().getExternalContext().redirect("faces/page1.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void onRowUnselect(UnselectEvent event) {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void createDynamicColumns() {

        columns = new ArrayList<columnModel>();
        columns.add(new columnModel("Name", "name"));
        columns.add(new columnModel("Role", "roll"));
        columns.add(new columnModel("Gender", "gender"));
        columns.add(new columnModel("ID", "id"));
    }

    public void redirectInput() {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("inputdisplay.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

BeanforCheckbox.java:

This is again self explanatory bean class, we are using this class as our Data class to populate the Datatable.

package beanclass;
import java.io.Serializable;
public class BeanforCheckbox implements Serializable {

    private String name;
    private String roll;
    private String gender;
    private int id;
    private boolean check = false;

    public int getId() {
        return id;
    }

    public boolean isCheck() {
        return check;
    }

    public void setCheck(boolean check) {
        this.check = check;
    }

    public void setId(int id) {
        this.id = id;
    }

    public BeanforCheckbox(String name, String roll, String gender, int id, boolean check) {
        this.name = name;
        this.roll = roll;
        this.gender = gender;
        this.id = id;
        this.check = check;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRoll() {
        return roll;
    }

    public void setRoll(String roll) {
        this.roll = roll;
    }
}

CustomCheckBox.xhtml:

This is a simple jsf page with primefaces Datatable.

Important Snippets:

<p:dataTable lazy="true"  id="multiCars" var="fet" value="#{myCheckboxBean.lazyModel}" paginator="true" rows="10"
                                 selection="#{myCheckboxBean.dtc}"  selectionMode="single" rowKey="#{fet.name}" rowsPerPageTemplate="10,20,25"
                                 paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport}
                                {NextPageLink}  {LastPageLink}" >
                        <p:ajax event="rowSelect" listener="#{myCheckboxBean.onRowSelect}">

                        </p:ajax>
                        <p:ajax event="rowUnselect" listener="#{myCheckboxBean.onRowUnselect}" />
             
                        <p:column headerText="Name" >
                            #{fet.name}
                        </p:column>

                        <p:column headerText="Roll" >
                            #{fet.roll}
                        </p:column>

                        <p:column headerText="Gender">
                            #{fet.gender}
                        </p:column>

                        <p:column >
                            <f:facet name="header">
                                <h:selectBooleanCheckbox id="all_transactionIdChkb" value="#{myCheckboxBean.head}"  >
                                    <p:ajax update="multiCars" event="click" listener="#{myCheckboxBean.selectAll}"/>
                                </h:selectBooleanCheckbox>
                            </f:facet>
                            <p:selectBooleanCheckbox value="#{fet.check}">
                                <p:ajax update="multiCars" listener="#{myCheckboxBean.view(fet)}"/>
                            </p:selectBooleanCheckbox>
                        </p:column>

     </p:dataTable>

The Datatable property lazy="true" sets the lazy data model operative by populating the "myCheckboxBean.lazyModel" . Propery selectionMode="single" enables the Datattable row selecting option.

Below lines are required to enable row selection along with the SelectionMode property. Our Row-Wise checkbox works because of this.

<p:ajax event="rowSelect" listener="#{myCheckboxBean.onRowSelect}" />
<p:ajax event="rowUnselect" listener="#{myCheckboxBean.onRowUnselect}" />

Below lines sets the checkbox at the header and provides select all feature.

                            <f:facet name="header">
                                <h:selectBooleanCheckbox id="all_transactionIdChkb" value="#{myCheckboxBean.head}"  >
                                    <p:ajax update="multiCars" event="click" listener="#{myCheckboxBean.selectAll}"/>
                                </h:selectBooleanCheckbox>
                            </f:facet>
Below lines sets the row-wise checkbox ,

  <p:selectBooleanCheckbox value="#{fet.check}">
                                <p:ajax update="multiCars" listener="#{myCheckboxBean.view(fet)}"/>
                            </p:selectBooleanCheckbox>

Full Code : 

<?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"
      xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title>Custom Header Selection</title>
    </h:head>
    <h:body>
        <ui:composition template="/commonLayout.xhtml">
            <ui:define name="content">

                <br></br><br></br>


                <h:form id="form">
                    <p:dataTable lazy="true"  id="multiCars" var="fet" value="#{myCheckboxBean.lazyModel}" paginator="true" rows="10"
                                 selection="#{myCheckboxBean.dtc}"  selectionMode="single" rowKey="#{fet.name}" rowsPerPageTemplate="10,20,25"
                                 paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}" >
                        <p:ajax event="rowSelect" listener="#{myCheckboxBean.onRowSelect}">

                        </p:ajax>
                        <p:ajax event="rowUnselect" listener="#{myCheckboxBean.onRowUnselect}" />
                        <f:facet name="header">
                            Checkbox Based Selection
                        </f:facet>




                        <p:column headerText="Name" >

                            #{fet.name}

                        </p:column>
                        <p:column headerText="Roll" >


                            #{fet.roll}

                        </p:column>
                        <p:column headerText="Gender">
                            #{fet.gender}


                        </p:column>


                        <f:facet name="footer">
                            <p:commandButton id="multiViewButton" value="View" icon="ui-icon-search"
                                             update=":form:displayMulti" oncomplete="multiCarDialog.show()"/>
                        </f:facet>
                        <p:column >
                            <f:facet name="header">
                                <h:selectBooleanCheckbox id="all_transactionIdChkb" value="#{myCheckboxBean.head}"  >
                                    <p:ajax update="multiCars" event="click" listener="#{myCheckboxBean.selectAll}"/>
                                </h:selectBooleanCheckbox>
                            </f:facet>

                            <p:selectBooleanCheckbox value="#{fet.check}">
                                <p:ajax update="multiCars" listener="#{myCheckboxBean.view(fet)}"/>
                            </p:selectBooleanCheckbox>


                        </p:column>
                    </p:dataTable>
                    <p:dialog id="multiDialog" header="Detail" widgetVar="multiCarDialog"
                              height="300" showEffect="fade" hideEffect="explode">

                        <p:dataList id="displayMulti"
                                    value="#{myCheckboxBean.multipledtc}" var="fet">
                            Name #{fet.name}, Roll #{fet.roll}
                        </p:dataList>

                    </p:dialog>
                </h:form>

            </ui:define>
        </ui:composition>
    </h:body>
</html>

MyCheckboxBean.java :
This is the class which is going to provide the required feature.

Important Snippets :

 private ArrayList<BeanforCheckbox> multipledtc = new ArrayList<BeanforCheckbox>();

The variable multipledtc  is arraylist which is going to store the selected values in the Datatable.

    private boolean head = false;
    private MultiBean mb = null;
    public boolean glb = false;

Variable head,mb,glb are required to maintain the Header Selection and pass on the value to row-wise checkbox.

The getter and setter of head variable, is must one to watch over. When the Datatable page is navigated the head variable becomes false even if we set it to checked status in the previous page. It means the PrimeFaces renders the headers as well while Page-navigation. SO we mocked the getter of head variable. While selecting the header checkbox we will call selectAll method, there we will set the value for glb and mb variable. In the same select all method based on the head value we will  the reload datatable by populating re-lazydatamodel.

  public boolean isHead() {
        if (glb) {
            head = mb.isChk();
        }
        System.out.println("inside ishad---");
        return head;
    }

    public void setHead(boolean head) {
        this.head = head;
    }

 public void selectAll() {

        System.out.println("inside select all updated........");
        boolean bl = true;
        if (mb.isChk()) {
            mb.setChk(false);
        } else if (mb.isChk() == false) {
            mb.setChk(true);
        }
        mb.setChk(isHead());
        glb = true;
        lazyModel = new LazyDataModel<BeanforCheckbox>() {

            public List<BeanforCheckbox> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
          
                List<BeanforCheckbox> lazyCars = populateLazyvaluecheck(pageSize, first);
          
                return lazyCars;
            }
        };

        lazyModel.setRowCount(30);

    }

List<BeanforCheckbox> populateLazyvaluecheck(int limit, int offset) {
        System.out.println("inside populateLazyvaluecheck--->" + limit + "--offset---->" + offset);
        System.out.println("flg value updated----->" + isHead());
        List<BeanforCheckbox> d = null;
        try {
            if (isHead()) {
                d = new DbClassCheck().selectRecordstrue(limit, offset);
                multipledtc = (ArrayList<BeanforCheckbox>) d;
            } else {
                d = new DbClassCheck().selectRecords(limit, offset);
                multipledtc = (ArrayList<BeanforCheckbox>) d;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return d;
    }


Below lines are from view method , those lines updates the value of multipledtc variable.

    if (multipledtc.size() > 0) {
            for (int i = 0; i < multipledtc.size(); i++) {
                BeanforCheckbox d1 = multipledtc.get(i);
                if (d.getName().equalsIgnoreCase(d1.getName())) {
                    flag = false;
                    if (d1.isCheck() == false) {
                        multipledtc.remove(i);
                    }
                    break;
                } else {
                    flag = true;
                }
            }
        } else {
            flag = false;
            if (d.isCheck()) {
                multipledtc.add(d);
            }
        }
        if (flag) {
            multipledtc.add(d);
        }

Full Code : 

package beanclass;
import java.io.IOException;
import java.io.Serializable;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;
import org.primefaces.model.*;

@ManagedBean
@ViewScoped
public class MyCheckboxBean implements Serializable {

    private String name;
    private BeanforCheckbox dtc;
    private List<columnModel> columns;
    private ArrayList<BeanforCheckbox> multipledtc = new ArrayList<BeanforCheckbox>();
    private String columnName;
    private boolean head = false;
    private MultiBean mb = null;
    public boolean glb = false;

    public boolean isHead() {
        if (glb) {
            head = mb.isChk();
        }
        System.out.println("inside ishad---");
        return head;
    }

    public void setHead(boolean head) {
        this.head = head;
    }

    public BeanforCheckbox getDtc() {
        return dtc;
    }
    private LazyDataModel<BeanforCheckbox> lazyModel;

    public void setLazyModel(LazyDataModel<BeanforCheckbox> lazyModel) {
        this.lazyModel = lazyModel;
    }

    public LazyDataModel<BeanforCheckbox> getLazyModel() {

        return lazyModel;
    }

    public void setDtc(BeanforCheckbox dtc) {
        this.dtc = dtc;
    }
    private ArrayList<DataTableClass> alist = new ArrayList<DataTableClass>();

    public ArrayList<DataTableClass> getAlist() {
        return alist;
    }

    public ArrayList<BeanforCheckbox> getMultipledtc() {
        return multipledtc;
    }

    public void setMultipledtc(ArrayList<BeanforCheckbox> multipledtc) {
        this.multipledtc = multipledtc;
    }

    @PostConstruct
    public void onload() {
        mb = new MultiBean();
        alist.add(new DataTableClass("1", "raghu", "jsf", 1));



    }

    public String getNamenew() {
        return namenew;
    }

    public void setNamenew(String namenew) {
        this.namenew = namenew;
    }
    private String namenew;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

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

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public MyCheckboxBean(String name) {
        this.name = name;
    }

    public MyCheckboxBean() {

        lazyModel = new LazyDataModel<BeanforCheckbox>() {

            /**
             * Dummy implementation of loading a certain segment of data.
             * In a real application, this method should load data from a datasource
             */
            public List<BeanforCheckbox> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
                // logger.log(Level.INFO, "Loading the lazy car data between {0} and {1}", new Object[]{first, (first+pageSize)});

                //Sorting and Filtering information are not used for demo purposes just random dummy data is returned
                //lazyModel.setRowCount(0);
                List<BeanforCheckbox> lazyCars = populateLazyvalue(pageSize, first);
                System.out.println("lazyCars-----" + lazyCars.size());
                return lazyCars;
            }
        };
        lazyModel.setRowCount(30);
    }

    public void onRowSelect(SelectEvent event) {
        try {
            //  setDtc(((DataTableClass) event.getObject()));
            //   DataTableClass d = getDtc();
            //   System.out.println("d---->" + d.getName());
            //   FacesContext.getCurrentInstance().getExternalContext().redirect("faces/page1.xhtml");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void onRowUnselect(UnselectEvent event) {
        try {
            //   FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void view(BeanforCheckbox d) {
        //  FormBean d = getDtc();
        System.out.println("d--view-->" + d.isCheck());
//        setName(d.getName());
//        setGender(d.getGender());
//        setId(d.getId());
//        setRoll(d.getRoll());
        boolean flag = false;
        if ((d.isCheck() == false) && (mb.isChk())) {
            multipledtc = new ArrayList<BeanforCheckbox>();
            mb.setChk(false);

            lazyModel = new LazyDataModel<BeanforCheckbox>() {

            /**
             * Dummy implementation of loading a certain segment of data.
             * In a real application, this method should load data from a datasource
             */
            public List<BeanforCheckbox> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
                // logger.log(Level.INFO, "Loading the lazy car data between {0} and {1}", new Object[]{first, (first+pageSize)});

                //Sorting and Filtering information are not used for demo purposes just random dummy data is returned
                //lazyModel.setRowCount(0);
                List<BeanforCheckbox> lazyCars = populateLazyvalue(pageSize, first);
                System.out.println("lazyCars-----" + lazyCars.size());
                return lazyCars;
            }
           @Override
            public void setRowIndex(int rowIndex) {
                /*
                 * The following is in ancestor (LazyDataModel): this.rowIndex =
                 * rowIndex == -1 ? rowIndex : (rowIndex % pageSize);
                 */
               System.out.println("row index..............");
                if (rowIndex == -1 || getPageSize() == 0) {
                    super.setRowIndex(-1);
                } else {
                    super.setRowIndex(rowIndex % getPageSize());
                }
            }
            @Override
            public BeanforCheckbox getRowData(String url) {
                for (BeanforCheckbox list : lazyModel) {
                    if (url.equals(list.getName())) {
                        return list;
                    }
                }
                return null;
            }
        };
        lazyModel.setRowCount(30);

        }

        if (multipledtc.size() > 0) {
            for (int i = 0; i < multipledtc.size(); i++) {
                BeanforCheckbox d1 = multipledtc.get(i);
                if (d.getName().equalsIgnoreCase(d1.getName())) {
                    flag = false;
                    if (d1.isCheck() == false) {
                        multipledtc.remove(i);
                    }
                    break;
                } else {
                    flag = true;
                }
            }
        } else {
            flag = false;
            if (d.isCheck()) {
                multipledtc.add(d);
            }
        }
        if (flag) {
            multipledtc.add(d);
        }

    }

    private void createDynamicColumns() {

        columns = new ArrayList<columnModel>();
        columns.add(new columnModel("Name", "name"));
        columns.add(new columnModel("Role", "roll"));
        columns.add(new columnModel("Gender", "gender"));
        columns.add(new columnModel("ID", "id"));
    }

    public void redirectInput() {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("inputdisplay.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    List<BeanforCheckbox> populateLazyvalue(int limit, int offset) {
        System.out.println("inside populate--->" + limit + "--offset---->" + offset);

        List<BeanforCheckbox> d = null;
        try {
            d = new DbClassCheck().selectRecords(limit, offset);
            for (int i = 0; i < d.size(); i++) {
                BeanforCheckbox bc = d.get(i);
                for (int j = 0; j < multipledtc.size(); j++) {
                    System.out.println("inside j loop");
                    BeanforCheckbox bc1 = multipledtc.get(j);
                    if (bc1.getName().equalsIgnoreCase(bc.getName())) {
                        System.out.println("inside j's if-----" + bc1.isCheck());
                        d.remove(i);
                        d.add(i, bc1);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return d;
    }

    public void selectAll() {

        System.out.println("inside select all updated........");
        boolean bl = true;
        if (mb.isChk()) {
            mb.setChk(false);
        } else if (mb.isChk() == false) {
            mb.setChk(true);
        }
        mb.setChk(isHead());
        glb = true;
        lazyModel = new LazyDataModel<BeanforCheckbox>() {

            /**
             * Dummy implementation of loading a certain segment of data.
             * In a real application, this method should load data from a datasource
             */
            public List<BeanforCheckbox> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
                // logger.log(Level.INFO, "Loading the lazy car data between {0} and {1}", new Object[]{first, (first+pageSize)});

                //Sorting and Filtering information are not used for demo purposes just random dummy data is returned
                //lazyModel.setRowCount(0);
                List<BeanforCheckbox> lazyCars = populateLazyvaluecheck(pageSize, first);
                // System.out.println("lazyCars-----" + lazyCars.size());
                return lazyCars;
            }
        };

        lazyModel.setRowCount(30);

    }

    List<BeanforCheckbox> populateLazyvaluecheck(int limit, int offset) {
        System.out.println("inside populateLazyvaluecheck--->" + limit + "--offset---->" + offset);
        System.out.println("flg value updated----->" + isHead());
        List<BeanforCheckbox> d = null;
        try {
            if (isHead()) {
                d = new DbClassCheck().selectRecordstrue(limit, offset);
                multipledtc = (ArrayList<BeanforCheckbox>) d;
            } else {
                d = new DbClassCheck().selectRecords(limit, offset);
                multipledtc = (ArrayList<BeanforCheckbox>) d;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return d;
    }
}

Try it and let me know.... Have a great day guys...

No comments:

Post a Comment

Popular Posts