使用JSP中的上一个,下一个按钮分页结果视图

时间:2011-11-28 20:24:28

标签: java mysql model-view-controller jsp pagination

我在页面上显示某些分析结果。但目前显示所有结果,页面上有超过300个结果。我想用最近10个提交的分析结果对结果进行分页,并能够导航到结果底部带有页码超链接的旧结果。我正在使用MVC框架。用于在JSP中显示分析结果功能的javascript。我使用mySQL作为数据库。我该如何实现?

2 个答案:

答案 0 :(得分:1)

试试这个C:/pagination.js文件..简单的分页

这是使用java脚本

分页的简单示例

您可以将pagination.js文件链接到您的网页。

请参阅下面的pagination.js代码和示例html文件

function Pager(tableName, itemsPerPage) {
    this.tableName = tableName;
    this.itemsPerPage = itemsPerPage;
    this.currentPage = 1;
    this.pages = 0;
    this.inited = false;   
    this.showRecords = function(from, to) {       
        var rows = document.getElementById(tableName).rows;
        // i starts from 1 to skip table header row
        for (var i = 1; i < rows.length; i++) {
            if (i < from || i > to) 
                rows[i].style.display = 'none';
            else
                rows[i].style.display = '';
        }
    }   
    this.showPage = function(pageNumber) {
         if (! this.inited) {alert("not inited");
         return;
         }
         if(pageNumber=="last")
         pageNumber=this.pages;      
         if(pageNumber=="first")
         pageNumber="1";         
        var oldPageAnchor = document.getElementById('pg'+this.currentPage);
        oldPageAnchor.className = 'pg-normal';       
        this.currentPage = pageNumber;
        var newPageAnchor = document.getElementById('pg'+this.currentPage);
        newPageAnchor.className = 'pg-selected';
        var from = (pageNumber - 1) * itemsPerPage + 1;
        var to = from + itemsPerPage - 1;
        this.showRecords(from, to);
    }  
    this.incrFont = function(pageNumber) {
        if (! this.inited) {
         alert("not inited");
         return;
        }
        var newPageAnchor = document.getElementById('pg'+pageNumber);       
        var res=newPageAnchor.className=="pg-normal";       
        if(res)
           newPageAnchor.className = 'pg-focused';           
       }       
    this.decrFont = function(pageNumber) {
        if (! this.inited) {
         alert("not inited");
         return;
        }
        var newPageAnchor = document.getElementById('pg'+pageNumber);
        if(newPageAnchor.className!="pg-selected")
           newPageAnchor.className = 'pg-normal';        
       }     
    this.prev = function() {
        if (this.currentPage > 1)
            this.showPage(this.currentPage - 1);
    }   
    this.next = function() {
        if (this.currentPage < this.pages) {
            this.showPage(this.currentPage + 1);
        }
    } 
    this.init = function() {
        var rows = document.getElementById(tableName).rows;
        var records = (rows.length - 1);
        this.pages = Math.ceil(records / itemsPerPage);
        this.inited = true;
        this.addStyle();
    }
    //showPageNav = function(pagerName, div_id, default pageNumber)
    this.showPageNav = function(pagerName, positionId, pageNumber) {
        this.init();
     if (! this.inited) {
      alert("not inited");
      return;
     }
     var element = document.getElementById(positionId);
     var pagerHtml = '<span onclick="' + pagerName + '.prev();" class="pg-normal"> &#171 Prev </span> | ';
        for (var page = 1; page <= this.pages; page++)
            pagerHtml += '<span id="pg' + page + '" class="pg-normal" onclick="' + pagerName + '.showPage(' + page + ');" onmouseover="' + pagerName + '.incrFont(' + page + ');" onmouseout="' + pagerName + '.decrFont(' + page + ');">' + page + '</span> | ';
        pagerHtml += '<span onclick="'+pagerName+'.next();" class="pg-normal"> Next &#187;</span>';
        element.innerHTML = pagerHtml;
        this.showPage(pageNumber);
    }   
}

index.html文件

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Paging Table example</title>
<script type="text/javascript" src="C:/pagination.js"></script>
<style type="text/css">   
            .pg-normal {
                color: blue;font-weight: bold;text-decoration: none;cursor: pointer;font-size: 12pt;  
            }
            .pg-selected {
                color: green;font-weight: bold; text-decoration: underline;cursor: pointer;font-size: 12pt; 
            }
            .pg-focused {
                color: red;font-weight: bold;text-decoration: underline;cursor: pointer;font-size: 12pt;
            }
        </style>
<script type="text/javascript"><!--
    var pager;
    function formatTable()
    {
           //new Pager("table_page","number of rows in per page")
           pager = new Pager("tbl_AddGrid", 3);
           //showPageNav("pager object","div id for page links","default page no");        
           pager.showPageNav("pager","div_TableMIS",1);        
    }
    //--></script> 
</head>
<body>
<table id="table_page">
            <tr><th>Sl No</th><th>field</th></tr>
            <tr><td>1</td><td><input type="text" name="field-name" value="rec1"></td></tr>
            <tr><td>2</td><td><input type="text" name="field-name" value="rec2"></td></tr>
            <tr><td>3</td><td><input type="text" name="field-name" value="rec3"></td></tr>
            <tr><td>4</td><td><input type="text" name="field-name" value="rec4"></td></tr>
            <tr><td>5</td><td><input type="text" name="field-name" value="rec5"></td></tr>
            <tr><td>6</td><td><input type="text" name="field-name" value="rec6"></td></tr>
            <tr><td>7</td><td><input type="text" name="field-name" value="rec7"></td></tr>
            <tr><td>8</td><td><input type="text" name="field-name" value="rec8"></td></tr>
            <tr><td>9</td><td><input type="text" name="field-name" value="rec9"></td></tr>
            <tr><td>10</td><td><input type="text" name="field-name" value="rec10"></td></tr>
        </table>
<div id="pageNavPosition" ></div>
</body>
</html>

答案 1 :(得分:0)

我建议使用开源Display tag library。它提供了通过display:table attributes实现分页,内部或外部排序,装饰器等功能。它还消除了JSP中大量自定义scriptlet代码的需要。

如果不深入了解您的具体用例,可以从以下方面了解如何将库应用于您的需求:

<display:table name="analysisList" defaultsort="1" pagesize="10" sort="list" uid="element">
  <display:column title="ID"                sortable="true"  property="id" />
  <display:column title="Date Submitted"    sortable="true"  property="date_created" />
  <display:column title="Notes"             sortable="true">
    <c:out value="${fn:replace(element.notes, "**-3**", "")}" />
  </display:column>
  <display:column title="Status"            sortable="true">
    <c:choose>
        <c:when test="${element.id eq -1}">
            ...
        </c:when>
        <c:when test="${element.xml_path eq null and fn:contains(element.notes, "-3")}">
            ...
        </c:when>
        <c:when test="${element.xml_path eq null}">
            ...
        </c:when>
        <c:otherwise>
            Report attached
        </c:otherwise>
    </c:choose>
  <display:column title="Similarity Search" sortable="true">
    ... similar idea as above ...
  </display:column>
</display:table>