tags:

views:

39

answers:

2

Hey,
I have a html table in my page that I populate with items from a database with jquery. Now i was wondering what's the best place to store your item Id? I'll need it later to do delete and update.

Here are some of the options I was thinking about:

  • TR tag
  • Hidden field
  • Checkbox (In my page you can do a bulk delete by checking the checkboxes and then clicking on the delete button).
  • ...

Also what naming should I take. Do I just do id="1", id="2", ... or is it better to put some text infront of the record id? Like id="ItemRow1", id="ItemRow2", ...

Or is there some other better technique for storing ID's?

Thanks in advance.

+2  A: 

id="1" is a bad idea because id's have to be unique across the whole page. Storing it in the checkbox sounds like a good idea though, without using id's.

<input type="checkbox" ... value="1">
Rusky
I like the checkbox idea.
Pickels
+2  A: 

I generally store an encoded ID on the main element, e.g. TR in your case but prefix them with something to keep them unique. Your database IDs should be unique anyway, I'd hope.

If I need IDs for the row and a checkbox, I'd use "row-{id}" and "chk-{id}". To get the DB ID back, just do id.replace(/^[^-]*-/, ''). I've used this quite a lot with GUID-based IDs and it works dandy.

Norman
Sounds like a good technique to handle ID's.
Pickels