views:

87

answers:

2

Hi,

Sorry for a bunch of Struts2 / JSP questions, but I have a table generated with Display tag:

<display:table name="table" pagesize="10" id="row" requestURI="">
<display:column title="Action">
    <s:form theme="simple">
        <s:submit action="remove" value="Remove" 
        onclick="return confirm('Are you sure you want to delete this item?');"/>
        <s:submit action="displayEdit" value="Edit"/>
    </s:form>
</display:column>
<display:column property="cpc" title="CPC"/>
<display:column property="companyName" title="Company Name"/>
<display:column property="eventType" title="Event Type"/>
<display:column property="industryType" title="Industry Type"/>
<display:column property="previousEvents" />
<display:column property="creditNotifications" />
<display:column property="interimNotifyEnterprise" />
</display:table>

Now I want to be able to delete or edit a certain row (I already have the actions written out), but how would I get the specific data for each row so I can tell my Action class which row to edit or delete?

A: 

Hi, It seems that your table does not have unique identifier field in class Object per Row. You can add field -id in you bean-class that you are going to display. (companyId in Company bean class). So that every row in the table will display unique 'company' object and depending upon 'companyId' field you may edit/delete selected object by passing 'companyId' to Action class.

You may no like to display 'companyId' column in your UI layout to end-user. In that case you can just create hyperlink of 'edit' and 'delete' column using 'companyId' as parameter.

See displaytag tutorial at this site for more detail.

lucentmind
Hi lucentmind,could you elaborate a little bit more on how I would use the id field? I'm still pretty new to web development, so maybe a simple example might help. Thanks!
Raymond
A: 

Hi, Raymond.

I mentioned this in the previous question you asked the other day. You just need to something in your row, possibly a hidden field, that can be used to uniquely identify the item.

I suggested using an <s:hidden key="rowID" /> in the row, which would get submitted with the action. As long as you had a property like that on your object, and in the remove action, you had a setter to receive that value, you could use that to uniquely identify the row.

Shawn D.
Hi Shawn,I tried the hidden field option, but when I tried to retrieve the value (which I named row), I instead get a null value. It worked before I tried to implement the display tag library, so I'm just not sure what I should do to get it to work with display tag.
Raymond
Sure, since Taglibs are all processed to generate the actual HTML, you should be able to take a look at the HTML source once the page has loaded. See if there's an element `<input type="hidden" name="rowID">`or whatever you called it. It would hold the value. If it's not there, it means that the value isn't getting retrieved properly during the 'get' phase, for lack of a better term. If it is present, that means the request would contain that value, and so it may be an issue with your setter.
Shawn D.
Hi Shawn, I check my source code for the generated HTML, and it does contain the hidden fields. Lets say the List I used to generate the table is an ArrayList<TableRow>, where TableRow is a class I defined myself. In this case, would rowID have to be of type TableRow? Right now, it shows that I am getting a null value when I have rowID of type TableRow.
Raymond
No, if it's generated `<input type="hidden" name="rowID" value="FFR34123">` where FFR34123 would be something you could use to look up that row in the action. I'm not sure what you have in your table, but it looks like some sort of event list. All you'd need on your Remove or DisplayEdit action then is a normal setter, like `setRowID( String id )`, which receives this value, and in your 'execute' call, do the lookup and act on the value.
Shawn D.
Hi Shawn,Looking at my HTML code, the hidden tag looks like this:<input type="hidden" name="rowID" value="" id="displayResults_rowID"/>So there is no value for me to retrieve. Is the rowID a field I should have in my TableRow class (that I set individually for each object)? I'm sorry if I sound really confused, because I am...
Raymond
Yes, so the idea is that in your ... um, `TableRow` class, do you have something that uniquely identifies the row/entry (other than position in the list)? It doesn't have to be `rowID`, it just should be something unique for the row, like `eventID` or something that exists (or can be exposed) in your TableRow class as a property. You get that value, so that when the page is resubmitted to your Remove/DisplayEdit page, the identifier is passed, set, and then in your subsequent action class, you know which row the button was clicked for.
Shawn D.
Note: I did a repost of my question in a more clearer form (so it's easier to read).
Raymond