views:

446

answers:

1

I have table enclosed by a div that looks similar to this.......

<div id="records">

10 | text1 | Delete
23 | test2 | Delete
24 | text3 | Delete
32 | text4 | Delete

</div>

I'd like to make the "Delete"s clickable which calls an ajax script to remove them from the database.

Each of the 'Delete's has a class of "delete_record"

The table is populated by an ajax script, so to avoid the rebinding issue, I'd like to use event bubbling on "records", so I added a click event on the div "records", and here I check for "delete_record" and currently just have an alert, which seems to work..

$('#records').click(function(event)  {
 if ($(event.target).is('.delete_record'))  {
  alert("clicked on CELL");
 }
});

But what I really want is to access a record_id which the ajax script knows about, and pass this to the ajax script to delete the record. I know how to call this ajax script with the correct values, what I don't know how to do is work out how to get this record _id

10 | text1 | Delete(20389)
23 | test2 | Delete(37474)
24 | text3 | Delete(2636)
32 | text4 | Delete(83731)

So when I click on the second line, the script will call....

$.getJSON("/ajax/delete_record.php",{id: 37474, ajax: 'true'}, function(j){ stuff }
+1  A: 

You said the table is populated from ajax, so, you can actually store your metadata into the table, assuming the following html is generated:

<tr>
    <td>23</td>
    <td>test2</td>
    <td><a>Delete</a></td>
    <td class='id'>37474</td> <!-- Hide the id column in css if needed -->
</tr>

So, just to follow what you did, you if the 'cell' is clicked, then you just go back to parent of it and then look for the td.hasClass('id') and get the text within. Like:

var parentTR = $(event.target).parent('tr'); // Get the parent row
var id = $("td[class='id']", parentTR).html(); // Retrieve the id content

Now you have the id, you can just ajax it to server to delete it.

One more thing to add, I don't really agree on the event binding on the table or the parent div to locate the delete button, it sounds too indirect to me. JQ actually provide a good method to live binding events, try looking at Live Function in JQuery. It actually do a good job for your situation.


xandy
I wouldn't do it this way. If you're going to store metadata, use the .data() method instead.
cdmckay
Or, if you want to use this method (let's say you have no control over the AJAX table maker) then I'd probably use the id property of the tr (if it's not being used) as in <tr id="recordid-37474"> or a custom attribute <tr record-id="37474">.
cdmckay
I would agree with cdmckay here. Use of the .live function to bind to the delete class, along with storing the record's ID in some sort of hyphenated element ID, has worked really really well for me in the past.
Brian Arnold
I agree with all of you above. The name of td[class=hidden] isn't a good one thought (just an bad example :) Since what I did is, my markup (and also data) are to be pumped from server into html directly, to visually "hide" the id column seems make sense to me (since 'record-id' is not valid in terms of HTML validation). True, in this scenario, the HTML markup are generated from AJAX-jQuery that whether it is valid or not doesn't matter, so it is alright to store id:1) in record-id attr,2) in an variable or data()
xandy
xandy
Agreed with you on using an anchor tag to provide the AJAX path. You could also automatically hide all tags with "ajaxDelete" when JS is enabled.
cdmckay
Thank you to all that answered. I now have it working, and I've learnt a lot in the process.I found the easiest way was to use the "live" method, and pick up the ID containing the record number (using this.id). Quite simple code in the end. Much simpler than I was expecting.Thank you all.