Thursday, July 3, 2014

Multiple Pagination using Grails

How to multiple pagination in Grails


Step1: In GSP


<!doctype html>
<html>
    <head>
        <meta name="layout" content="main">
        <g:set var="entityName" value="${message(code: 'employees.label', default: 'Employees')}" />
        <title><g:message code="default.list.label" args="[entityName]" /></title>
       
        <style>
table, td, th {
    border: 1px solid green;
    padding: 1px;
    font-size:88%;
}

th {
    background-color: green;
    color: white;
    padding: 1px;
    font-size:85%;
}
</style>
       
    </head>
    <body>   
    <h1>Welcome to Dashboard</h1>
        <g:if test="${employeesInstanceList}">
        <div class="box2">       
        <h1>Employee List</h1>
            <table>
                <thead>
                    <tr>
                   
                        <g:sortableColumn property="firstName" title="${message(code: 'employees.firstName.label', default: 'First Name')}" />
                   
                        <g:sortableColumn property="lastName" title="${message(code: 'employees.lastName.label', default: 'Last Name')}" />
                   
                        <g:sortableColumn property="dateOfbirth" title="${message(code: 'employees.dateOfbirth.label', default: 'Date Ofbirth')}" />                   
                   
                    </tr>
                </thead>
                <tbody>
                <g:each in="${employeesInstanceList}" status="i" var="employeesInstance">
                    <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
                   
                        <td><g:link action="show" id="${employeesInstance.id}">${fieldValue(bean: employeesInstance, field: "firstName")}</g:link></td>
                   
                        <td>${fieldValue(bean: employeesInstance, field: "lastName")}</td>
                   
                        <td><g:formatDate date="${employeesInstance.dateOfbirth}" /></td>
                                       
                    </tr>
                </g:each>
                </tbody>
            </table>
            <div class="pagination">               
                <g:paginate total="${employeesInstanceTotal}" max="5" offset="${session.empPagination?.offset}" params="${[paginate:'Employee']}"/>
            </div>
        </div>
        </g:if>
       
       
        <g:if test="${payrollInstanceList}">
        <div class="box1">
        <h1>Payroll List</h1>
            <table>
                <thead>
                    <tr>
                                       
                        <g:sortableColumn property="payDate" title="${message(code: 'payroll.payDate.label', default: 'Employeess')}" />
                       
                        <g:sortableColumn property="payDate" title="${message(code: 'payroll.payDate.label', default: 'Components')}" />
                   
                        <g:sortableColumn property="payDate" title="${message(code: 'payroll.payDate.label', default: 'Pay Date')}" />
                   
                        <g:sortableColumn property="allowanceAmt" title="${message(code: 'payroll.allowanceAmt.label', default: 'Allowance')}" />
                   
                        <g:sortableColumn property="deductionAmt" title="${message(code: 'payroll.deductionAmt.label', default: 'Deduction')}" />
                       
                        <g:sortableColumn property="deductionAmt" title="${message(code: 'payroll.deductionAmt.label', default: 'Companys')}" />                                                               
                   
                    </tr>
                </thead>
                <tbody>
                <g:each in="${payrollInstanceList}" status="i" var="payrollInstance">
                    <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
                   
                        <td><g:link action="show" id="${payrollInstance.id}">${fieldValue(bean: payrollInstance, field: "employeess")}</g:link></td>
                   
                        <td>${fieldValue(bean: payrollInstance, field: "allowanceTypes")}</td>                                           
                                                                                   
                        <td><g:formatDate date="${payrollInstance.payDate}" /></td>
                   
                        <td>${fieldValue(bean: payrollInstance, field: "allowanceAmt")}</td>
                   
                        <td>${fieldValue(bean: payrollInstance, field: "deductionAmt")}</td>
                       
                        <td>${fieldValue(bean: payrollInstance, field: "companys")}</td>
                   
                    </tr>
                </g:each>
                </tbody>
            </table>
            <div class="pagination">
            <g:paginate total="${payrollInstanceTotal}" max="5" offset="${session.payPagination?.offset}" params="${[paginate:'Payroll']}"/>
            </div>
        </div>
        </g:if>
    </body>
</html>



Step2: In controller


class IndexController {
    def index() {  // index is the default action for any controller
        redirect(action:"list")
    }
   
    def list() {
       
        if (params.paginate == 'Employee') {
            def empPagination = [max: params.max, offset: params.offset]
            session.empPagination = empPagination
          } else if (params.paginate == 'Payroll') {
            def payPagination = [max: params.max, offset: params.offset]
            session.payPagination = payPagination   
          }
          def empList = Employees.list(session.empPagination ?: [max: 5, offset: 0])
          def payList = Payroll.list(session.payPagination ?: [max: 5, offset: 0])

          params.offset = null
          params.max = null
          [employeesInstanceList: empList, employeesInstanceTotal: Employees.count(), payrollInstanceList: payList, payrollInstanceTotal: Payroll.count()]
        }  

    }





No comments: