1

I have JSP where I am displaying list of data in a tabular format. One of the columns in each will have 3 different links. Have 3 different methods in Action class, so that the respective method will be triggered on click of the link. Once the method has returned the data from Action class, number of rows will be appended based on the list size returned by Action class. I am using struts iterator to iterate and add the <tr>. I have written the jQuery to perform that operation and everything works fine.

New requirement is that when I click on the first link, it should display the set of records. When I click on the first link again, then the set of records that were displayed should be removed. Its kind of toggling the set of records.

To achieve this, I created a div component, inside which the new set of records would be added and try to do it, but its not working as expected. Have attached the extract of the source code from each file. Please let me know your thoughts.

JSP File:

<s:iterator value="#mbean.myList" var="poItem">
    <tr>
        <td class="matlNumber">&nbsp;</td>
        <td><s:property value="#poItem.doc" /></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td><s:property value="#poItem.weekOne" />  </td>
        <td><s:property value="#poItem.weekFive" /></td>
    </tr>
</s:iterator>

JS File:

$(".poLink").click(function(){      
    var myData = $(this).closest('tr').find('td:first').text();
    document.forms[0].action = "poListMaterialPlanning.action?myData ="+myData ;
    document.forms[0].submit();
}

I tried to use the $.ajax(), but could not assign the data returned by the Action class to the struts variable in JSP.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Anand
  • 727
  • 3
  • 14
  • 39
  • Why don't you just show/hide this list? – Aleksandr M Sep 10 '14 at 08:49
  • So what exactly is the issue? getting the data? show/hide? what is `poLink`? – T J Sep 10 '14 at 09:26
  • @AleksandrM: I tried with show/hide, but how would you show/hide the list of records without operating on the DIV tag? I am having the rows say 1 to 20, of which I want to show/hide the rows 6 to 15 (this may vary at runtime). I tried it by assigning an ID to the list of rows from 6 yo 15, but it has hidden the 6th row only, not the rest of the rows. I know the ID should be unique and so changed to toggle the class attribute of the rows. Because I have two classes for those 6-15 rows, I could not toggle. – Anand Sep 12 '14 at 01:26
  • @TJ: Hai, poLink is the one of the links, on which I click, I get the data from DB and show it on the screen. Also, the toggling of the data is on click of poLink – Anand Sep 12 '14 at 01:27
  • @Anand: Yes you can toggle items based on some class. What do you mean by *Because I have two classes for those 6-15 rows, I could not toggle*? It shouldn't be a problem. – Aleksandr M Sep 12 '14 at 08:12
  • @AleksandrM: I am assigning the class named "1-PO" to the set of rows 6-15. As I have applied dataTable properties to the table, it assigns the odd or even class to these rows. So, when I process the table in jQuery, I am getting the class as "1-PO even" or "1-PO odd". I want to apply this toggling to the rows which has the class value of 1-PO. Hope I explained clearly – Anand Sep 14 '14 at 05:28

1 Answers1

2

Premise: the sentence

could not assign the data returned by the Action class to the struts variable in JSP.

is wrong because there is nothing like a "Struts variable" in the rendered HTML page.

That said, there are several ways to achieve your goal (although not completely clear):

  1. Load the content when rendering the page, then hide / show it through javascript; this is the fastest way, but there are cases where it is discouraged, for example :

    • the content could have been changed after the page is loaded, because of other users or softwares updating it;
    • it's too big to load it for all the records, and the memory footprint and page loading time becomes unacceptable.
  2. Add / remove the content through a standard POST / GET submit; this is the old way, you reload everything, recharge everything and have to manually handle anchors and page-related operations.

  3. Add the content through AJAX calls, and remove it through javascript (raw javascript, jQuery, struts2-jQuery-plugin, or other libraries); this is probably what you need, if solution n.1 doesn't fit your case.

To stick with the current standards / best practices / tools that will simplify your work, you should use:

Side note: if you inject a JSP snippet containing others struts2-jquery tags, you need to read this.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • 1
    @AleksandrM Thanks. This can be linked to future users asking for `struts2 + ajax + jquery + json + dunno nothing about 'em`, so I thought it was worth the data-mining work :) – Andrea Ligios Sep 10 '14 at 10:00
  • 1
    Well in that case I would also note that *raw jQuery* is probably better in the long run/not so easy cases. – Aleksandr M Sep 10 '14 at 10:02
  • Yeah, in the long run and especially if you uses other frameworks other than Struts2. But I guess this is later in the time (and then it becomes obvious to the experienced user), this answer targets users that haven't done any AJAX call yet... struts2-jquery plugin is mature and a lot easier than raw jQuery, at first. But +1 to highlight the comment :) – Andrea Ligios Sep 10 '14 at 10:30
  • @AndreaLigios: Thanks for ur suggestions. I think your 1st suggestion would fix my problem. If I got with 3rd option of making AJAX calls, how would I assign the list. The current datastructure is matBeanList has list of poBeans. When I make a call for a matBean, it makes a AJAX call and returns the list of poBean for that particular matBean. How would I fit the new list of poBeans into the original matBeansList in AJAX jQuery? – Anand Sep 12 '14 at 01:32
  • I guess you should try and eventually come back with a new question, including your code and a description of what is not working, if any. If this answer helped clearing your doubts and oroginal questions, please consider accepting and upvoting it, thanks. – Andrea Ligios Sep 12 '14 at 08:05