jQuery/JavaScript: Avoid unique ID's for each row in a table? -


I'm not sure I'm doing this correctly. I have a table that I fill with rows that represent a song in every playlist. Now, I assign a unique ID per line, and also assign some jQuery.data () to each ID.

  html + = '\ & lt; Tr id = "track-'+ i +' class =" track-line "& gt; \ & Lt; Td square = "track" & gt; & Lt; A = id = "play - '+ i +' class =" game "& gt; & lt; / a & gt; & lt; an id =" play2- '+ i + "& gt;' + Song.song_track + '& Lt; span class = "mix-style" & gt; + Song_mix + '& lt; / span & gt; & lt; / a & gt; & lt; / td & gt; \ & lt; td class = "artist" & gt;' Song.song_artist + '& lt ; / Td> \ & lt; td class = "favorite-holder"> an id = "favorite- '+ +" class = "favorite">   

So you can see, each line has a track , Such as track-1, track-2 etc..

Another way to populate playlists like this is to specify unique IDs for each track, or how to do it Each track has some properties:

  $ ("#track-" + i) .data ("song_id", song.song_id); $ ("#track" I) .Data ("song_arti", song. song_artist); $ ("#track-" + i) .data ("song_track", song. song_track); $ ("#track-" + i) .data (" Song_mix ", song.song_mix); $ (" #track- "+ i) .data (" ps_id ", song.ps_id);  

... and more. Click for event, which allows the user to run, sort, drag, and so on ... It seems like I'm doing it wrong :)?

You can handle the reference of each generated row in your loop (value html Contains only HTML for one line):

  var line = $ (html) .appendTo ("# table"); Var data = row.data (); Data ["song_id"] = song.song_id; Data ["song_artist"] = song.song_artist; Data ["song_track"] = song.song_track; Data ["song_mix"] = song.song_mix; Data ["ps_id"] = song.ps_id; Row.click (function () {...}); It is not bad to have an ID for an element but definitely one reference is fast to use if you have one and again and again the jQuery selector engine. Do not use.  

In addition, if you attach the same click handler to each line, it is probably better to attach one to the table and assign it, e.g.

  $ ('# table'). Representative ('tr', 'click', function () {.. ..});  

Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -