javascript - How to limit the HTML table records in each page - Stack Overflow

I am populating a table in my jsp which has more than 20 records. Now, If I want to display each 10 rec

I am populating a table in my jsp which has more than 20 records. Now, If I want to display each 10 records in a page and then the user clicks on next link to go to the other 20 records.

How can I do that in my jsp?

This is my current code

<table>
    <tr>
        <td>id</td>
        <td>name</td>
        <td>type</td>
    </tr>
<%
    while(resultSet.next())
    {
        %>
        <tr>
        <td><%=resultSet.getString(1)%></td>
        <td><%=resultSet.getString(2)%></td>
        <td><%=resultSet.getString(3)%></td>
    </tr>
<%
    }
%>
</table>

I am populating a table in my jsp which has more than 20 records. Now, If I want to display each 10 records in a page and then the user clicks on next link to go to the other 20 records.

How can I do that in my jsp?

This is my current code

<table>
    <tr>
        <td>id</td>
        <td>name</td>
        <td>type</td>
    </tr>
<%
    while(resultSet.next())
    {
        %>
        <tr>
        <td><%=resultSet.getString(1)%></td>
        <td><%=resultSet.getString(2)%></td>
        <td><%=resultSet.getString(3)%></td>
    </tr>
<%
    }
%>
</table>
Share Improve this question asked Aug 2, 2010 at 16:04 maasmaas 1,2235 gold badges14 silver badges13 bronze badges 1
  • 1 Duplicate of ResultSet to Pagination. Just a warning: you really don't want to do this in "raw" Java code in JSP. It'll on long term only give trouble in all colors. – BalusC Commented Aug 2, 2010 at 16:47
Add a ment  | 

3 Answers 3

Reset to default 1

You can use 'display' tag which provides pagination, sorting, exporting. In your current code, you are iterating a result set and creating table rows. Having Java code in JSP is not the best practice. Move that into a servlet and create a collection of beans by iterating the result set and set the collection in request scope or session scope(If you want to avoid multiple DB hits).

Sample Usage (From display tag site)

<display:table name="sessionScope.test" pagesize="10">
    <display:column property="id" title="ID" />
    <display:column property="name" />
    <display:column property="email" />
    <display:column property="status" />
</display:table>

Here the 'test' is the name of the attribute that stores a collection. The 'property' attributes for column tag represent the properties of your bean contained in the collection. 'pagesize' specifies the pagination size that the number of items to be displayed in a page.

For more info: http://displaytag.sourceforge/1.2/index.html

I know that you are looking for a back-end solution, but how about making it easier for the user (by not forcing page refresh on each request) and have a js solution (a js pagination solution)?

A list of jQuery pagination solutions

  • List of jQuery pagination plugins
  • 15 more jQuery pagination plugins
  • Another one (includes pagination + sorting)
  • Another 20 Table pagination plugins

Again, this is only a suggestion, go for whatever works for you (and your users) best.

As requested: see a good beginner tutorial or Another one.

What you want is a pagination code example. Here's one I found online: http://www.easywayserver./blog/jsp-pagination/ There are lots of examples of this and you'll have to find which one formats your nav links the way you like them

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745627060a4636871.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信