I have a standard tomcat webapp that is querying a MySQL database and sending results back from the servlet to a JSP. The JSP displays the results without a traditional data table, but rather by using a scriplet on load to fill in the results into their respective HTML tables (one table per result row).
Here is a rough code snippet to show how I'm populating the JSP page:
<html>
<body>
...
<%
String[][] results = ((String[][])session.getAttribute("results"));
for (int i = 0; i < results.length; i++)
{
String[] rowResult = (String[])results[i];
String tableId = "table_" + i;
String attribOne = rowResult[0];
String attribTwo = rowResult[1];
%>
<table id="<%= tableId %>" >
<script>
var table = document.getElementById("<%= tableId %>");
table.setAttribute("attribOne", "<%= attribOne %>");
table.setAttribute("attribTwo", "<%= attribTwo %>");
</script>
...
</table>
<%
}
%>
</body>
</html>
I'd like to know the best way to re-order (sort) my results after they have been initially populated on the page. There is a drop-down menu at the top of the page whose values correspond to an attribute on each table by which to sort. In effect, I need to move the tables around based on the user's sort selection.
I have considered the following options:
Pass multiple result sets from the Servlet to the JSP page, and redisplay (refresh?) the page if a different sort attribute is selected. The downsides to this approach are numerous. I have to run multiple database queries in the database (costly), return multiple copies of the same result set to the JSP (inefficient) and possibly have to refresh the page (annoying) while only wanting to rearrange the results
Use JavaScript/jQuery to rearrange the tables on the page. This is potentially very difficult to implement and may take a long time to process/redisplay. I don't want to make the user wait a long time just for results to re-order.
If any of you could comment on these or provide any other suggestions on how to handle this, I would greatly appreciate it.
-Bob