JSP: sort HTML table on serverside
I am working on one legacy application. In JSP there is a html table. I need to write functionality to sort the table based on the column the users clicks.
The table display 20 rows in each page. Some tables have 3000 rows. So, there can be about 150 pages. Upon clicking each page#, servlet is called and gets the next rows. So each click is a new request.
My question is: When the page displays, 开发者_开发百科the columns are in ascending order by default. Next click on the column, should sort it desc order (should sort all 3000 rows) and vice-versa. There are about 10 columns that they can sort data. How can I do this.
An array of objects is easy to sort: you create a custom Comparator
that will compare different fields depending on a constructor parameter. Then you call Arrays.sort()
, passing it your array and your custom comparator. This is discussed in the Java tutorial: http://download.oracle.com/javase/tutorial/collections/interfaces/order.html
To identify field to sort, you would simply pass a request parameter. You'd construct the table such that each header has an onClick
event that sends a new request with the given parameter (I'm sure there's a more modern approach, someone else might answer that).
But none of that will work if you're correct when you say that the array is stored as a request attribute. Are you sure it's not stored as a session attribute?
精彩评论