0

In a jsp, I have generated a table consisting of records from the database - trivially, using

<c:forEach var="activity" items="activities" varStatus="loopCounter">
   ...<td><c:out...
</c:forEach>

This is shown in a modal. When the user selects one from the items and clicks 'OK', I want the selected item to load it's details in the description div on the same page.

<dl>
    <dt>Title</dt>
    <dd>{Title of selected item here}</dd>
    ....
</dl>

An approach I think of is to get the selected item's id and load it from the database, then display - although it'll be superfluous, since the record is already loaded. I am using Spring to return the list.

@RequestMapping(method = RequestMethod.GET)
public String index(Model model){
    List<Activity> activities = activityService.listActivities();
    model.addAttribute("activities", activities);    
    return "index";
}

Not all columns are displayed in the modal table, so using javascript to "set text" from modal to desc div is not a solution (I think).

Any ideas on how can I display the details from the list without requesting it again from the database?

Or more importantly, is there a better approach in achieving my requirement?

Gx.Zero
  • 407
  • 1
  • 4
  • 10

1 Answers1

0

How about loading the description in your JSP directly in hidden divs (display:none;) and then make it visible (dispaly:block;) on user action via only JavaScript, so you don't need to make another request to the server to load the data again from the database

fujy
  • 5,168
  • 5
  • 31
  • 50
  • That's one.. although, won't it make the "page source" dirty? -- considering one of the columns is a TEXT (i.e.:400+ chars), and printing them all (i.e: times NumOfRecords) – Gx.Zero Sep 09 '13 at 02:01
  • mmm ... you are right. However, It will be just only text. right?, and you have already retrieved it from the database, also I would prefer to use this approach as it saves the redundant server and database requests. – fujy Sep 09 '13 at 02:11
  • One other approach is to have the retrieved data - somehow - binded to the session, and only make an Ajax request on user action to return the description of one specific record as JSON format then display, but you will have the overhead of redundant server request – fujy Sep 09 '13 at 02:14
  • It's think it's tolerable to to have another server request (compared to server request + database request), although I don't have ideas about that "bind to session" thing. Hahaha. :) – Gx.Zero Sep 09 '13 at 02:27
  • Check this http://stackoverflow.com/questions/10034993/spring-store-object-in-session – fujy Sep 09 '13 at 02:33
  • And for Spring Ajax JSON issue, you could check this http://codetutr.com/2013/04/09/spring-mvc-easy-rest-based-json-services-with-responsebody/ or http://blog.springsource.org/2010/01/25/ajax-simplifications-in-spring-3-0/ or http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/ – fujy Sep 09 '13 at 02:52