0

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

Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94
Bob .
  • 521
  • 9
  • 18
  • I would still recommend using some JavaScript/jQuery librarys/plugins (like [tablesorter](http://tablesorter.com/docs/) for example) to sort results on the page. This way it would be a minimum impact on the speed. **They are not really hard to use, they are fast and configurable**. But, of course, you then need to use HTML tables. – informatik01 Apr 08 '13 at 11:19

1 Answers1

0

Since you had already listed 2 most obvious solution , here is an alternative worth to try.

Instead of passing to JSP as String[][] you can define a POJO class to house the data.

Example

 Class Row{
  String attribOne;
  String attribTwo
}

so that you can pass it to the jsp as List< list >

If the data is not very huge you can have it in a Session Variable , and use java.util.Comparator to sort it multiple ways .

The only downside is you will have multiple Comparators for each sortable column in the table.

Certainly not THE optimal solution, i present you an alternative , but it certainly avoids the extra Db calls / browser stalling if you do it in JS.

Well just my 2 cents

Sudhakar
  • 4,823
  • 2
  • 35
  • 42