JSP:在服务器端对 HTML 表格进行排序
我正在开发一个遗留应用程序。在JSP中有一个html表。我需要编写功能来根据用户单击的列对表进行排序。
该表每页显示 20 行。有些表有 3000 行。因此,大约可以有 150 页。单击每个页面# 后,将调用 servlet 并获取下一行。所以每次点击都是一个新的请求。
我的问题是:当页面显示时,列默认按升序排列。接下来单击该列,应按降序顺序对其进行排序(应对所有 3000 行进行排序),反之亦然。他们可以对大约 10 列数据进行排序。我该怎么办呢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对象数组很容易排序:您创建一个自定义的比较器,它将根据构造函数参数比较不同的字段。然后调用 Arrays.sort(),向其传递您的数组和自定义比较器。 Java 教程中对此进行了讨论: http://download.oracle.com /javase/tutorial/collections/interfaces/order.html
要识别要排序的字段,您只需传递一个请求参数即可。您可以构建表,使每个标头都有一个
onClick
事件,该事件使用给定参数发送新请求(我确信有一种更现代的方法,其他人可能会回答这个问题)。但是,如果您说的数组存储为请求属性是正确的,那么所有这些都不起作用。您确定它没有存储为会话属性吗?
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 callArrays.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.htmlTo 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?