I am using display tag to show user list on my JSP. I have a delete button in each row to delete the particular row/ user from the list. For this I am using ajax.
The user is getting deleted from the database using ajax. But since I am using ajax. Untill I refresh the page the user is still there in the display tag list.
Is there any way i can delete the row from display table using javascript?
As far as I think row can be delted from table using javascript but you need to know row id at least.
So, how do I give unique row ids to each row in the display table?
please help.
Below is my code
<display:table name="employeeUpdateList" pagesize="50"
class="listingTable" keepStatus="true" cellpadding="0px"
cellspacing="0px" id="employeeUpdate" export="true" requestURI="">
<display:setProperty name="export.decorated" value="true" />
<display:setProperty name="export.excel.filename"
value="Employee Update List.xls" />
<display:column title="What is changed" property="columnName"></display:column>
<display:column title="From" property="oldValue"></display:column>
<display:column title="To" property="newValue"></display:column>
<display:column title="Effective Date">
<fmt:formatDate value="${employeeUpdate.effectiveDate}"
pattern="dd MMMM yyyy" />
</display:column>
<display:column title="Changed By" property="changedBy.fullname"></display:column>
<display:column title="Changed On">
<fmt:formatDate value="${employeeUpdate.changedOn }"
pattern="dd MMMM yyyy hh:mm:ss" />
</display:column>
<display:column media="html"
style="text-align :center!important;padding:0px!important">
<input class="input-button-small" type="button"
value="<spring:message code='button.delete' />"
onclick="deleteEmployeeUpdate('${deleteEmployeeUpdateUrl}','${employeeUpdate.id}')" />
</display:column>
<display:setProperty name="paging.banner.item_name"
value="Employee Update" />
<display:setProperty name="paging.banner.items_name"
value="Employee updates " />
</display:table>
Update – now that you’ve posted your code, I see you’re using DOM0
onclick="..."handlers. I don’t recommend doing that (see below, this is a classic use case for doing event delegation — watching for clicks at the table level rather than on individual buttons), but..You’ll want to modify your
deleteEmployeeUpdatefunction to accept another argument, which will be the button that was clicked. So:and
Then to remove the row containing the button, within that function:
See below for a solution using event delegation, and below that, some handy reading.
You don’t need to give your rows IDs. When you process the
clickevent on the delete button, normally you have a reference to that delete button element asthis. It has aparentNodeproperty which is the node (element) that contains it. That’s probably a table cell (td). It also has aparentNodeproperty, which is presumably the row (tr), which also has a parent which is usually atbody, etc.You can remove a row by calling its parent node’s
removeChildfunction and passing in thetrelement.Here’s a complete example: Live copy | source
HTML:
JavaScript:
Reading:
FWIW, I’d strongly recommend using a good JavaScript library like jQuery, Prototype, YUI, Closure, or any of several others. The example gets dramatically shorter, which shows how you can focus on what you’re trying to build rather than mucking about with the details of the low-level DOM API.
For example, the above using jQuery: Live copy | source
HTML:
(No change)
JavaScript:
That’s for jQuery 1.7 and later. If you’re using something earlier, you’d use
delegaterather thanon(note the order of arguments changes):It would be similarly simple with just about any of the other libraries.