rich:dataScroller on custom RichLazyDataModel does not paginate

239 Views Asked by At

I am trying to make pagination with JSF 2 and RichFaces 4.3.7. I look through example in richface4 about pagination in this link: http://showcase.richfaces.org/richfaces/component-sample.jsf?demo=dataScroller&sample=simpleScrolling&skin=blueSky

and As You can see for <rich:dataScroller for="table" maxPages="5" fastStep="3"> it doesn't add any value I wonder How it control the database for Next page? I implement Class that Store data from database and I try To handle pagination But when I click on next page Nothing will happen I suppose I miss something in java Class and RichLazyDataModel class I found in one question in stackoverflow JSF, RichFaces, pagination

So RichLazyDataModelImpl extend that class Any help Appreciate

public class RichLazyDataModelImpl extends RichLazyDataModel<User> {

    int firstRow = 0;
    int numRows = 5;

    @Override
    public List<User> getDataList(int firstRow, int numRows) {
        return getDataList() ;
    }

    public List<User> getDataList() {
        try {
            Connection conn =  DriverManager.getConnection(configure.myUrl, "root",
                    "pass");
            String sql = "select * from messages order by id_messages"; // limit ? offset ? ";
            PreparedStatement stmt = (PreparedStatement) conn.prepareStatement(sql);
            //stmt.setInt(2, new Integer(firstRow));
            //stmt.setInt(1, new Integer(numRows));
            ResultSet rs = stmt.executeQuery();



        List<User> result = new ArrayList<User>();
        while(rs.next()) {
            // read entry no. n
            User user = new User();
            String mobile =rs.getString("msg_msisdn");
            user.setMsg_mobile(mobile);
            String direction = rs.getString("msg_direction");
            user.setDirection(direction);
            String msg = rs.getString("msg_text");
            user.setMessage(msg);
            String status = rs.getString("msg_status");
            user.setMsg_status(status);
            Date time = rs.getDate("msg_time");
            user.setMsg_time((java.sql.Date) time);
            result.add(user);
            }
        return result;
    } catch (SQLException e) {


        e.printStackTrace();
    }
    return null;

    }

    @Override
    public Object getKey(User user) {
        // TODO Auto-generated method stub
        //return null;
        return user;
    }

    @Override
    public int getTotalCount() {

        String sql = " select count(*) as cnt from messages";
        PreparedStatement stmt;
        Integer result = 0;
        try {
            Connection conn =  DriverManager.getConnection(configure.myUrl, "pegah",
                    "pass");
            stmt = (PreparedStatement) conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            result = rs.getInt("cnt");
            rs.close();
            conn.close();
            System.out.println("The number of dataBase is "+result);

        } catch (SQLException e) {

            e.printStackTrace();
        }

        return result;

    }

    public void setCurrentPage(int page) {
        int row = (page-1) * numRows;
        firstRow = row;
    }

    public int getCurrentPage() {
        int page = firstRow / numRows;
        return page;
    }

}

table.xhtml

 <rich:dataScroller id="sc2" for="table" maxPages="3" fastStep="3" >

</rich:dataScroller>
 <rich:dataTable value="#{RichLazyDataModelImpl.getDataList()}" var="cate" id="table" rows="5">


                <rich:column >
                    <f:facet name="header">
                        <h:outputText value="Mobile " />
                    </f:facet>
                    <h:outputText value="#{cate.getMsg_mobile()}" />
                </rich:column>
                  .
                  .
                  .
1

There are 1 best solutions below

0
On

Everything were Fine in RichLazyModelImpl.java The problem was that I should Add <head> which consist of bootstrap part to xhml file which wasn't mentioned in example on richFace website and Now everything works Fine. Richface will take care of pagination.