Hello! I have code that removes a photo from the database dynamically via Ajax. And there is a table in which photos from the database are displayed.

<table class="table" cellspacing="0" cellpadding="0"> <tbody><tr> <th class="number">β„–</th> <th class="thumb">Π€ΠΎΡ‚ΠΎ</th> <th class="album_name">НазваниС Ρ„ΠΎΡ‚ΠΎ</th> <th class="album_action">ДСйствиС</th> </tr> <tr> <td>2.</td> <td class="thumb centr"><img class="delalbumimg" src="http://20dord.loc/gallery/35_2015-08-18/35_2015-08-18_9.jpg" rel="1" alt="35_2015-08-18_9.jpg"></td> <td class="album_title centr">35_2015-08-18_9.jpg</td> <td class="photo_action centr"> <span class="small howtodelbase">Для удалСния ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ ΠΊΠ»ΠΈΠΊΠ½ΠΈΡ‚Π΅ Π½Π° Π½Π΅ΠΉ</span></td> </tr><tr style="display: none;"> <td>3.</td> <td class="thumb centr"><img class="delalbumimg" src="http://20dord.loc/gallery/35_2015-08-18/35_2015-08-18_10.jpg" rel="1" alt="35_2015-08-18_10.jpg"></td> <td class="album_title centr">35_2015-08-18_10.jpg</td> <td class="photo_action centr"> <span class="small howtodelbase">Для удалСния ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ ΠΊΠ»ΠΈΠΊΠ½ΠΈΡ‚Π΅ Π½Π° Π½Π΅ΠΉ</span></td> </tr> 

As you can see, each line is numbered. In this case, I deleted the first photo. I need the numbering to automatically change when I delete a photo. I made the line numbers output:

 <script> var l = $("table tr").length; for(var i=1; i<=l; i++){ $('.table tbody tr').each(function(i) { $(this).find('td:first').text(i+"."); }); } </script> 

But this code only works when the page loads.

  • The same code needs to be done after successful ajax . ps. Why so tricky? just $('.table tbody tr').each(function(i) { ... won't it work? - xaja
  • Works. According to my idea, the script should count the number of lines and add numbering. So I wrote. Thank. - Genghis
  • I mean that for(var i=1; i<=l; i++){ not necessary - xaja

1 answer 1

And doesn't it work like that?

 function updateTableNumeration() { $('.table tbody tr').each(function(i) { $(this).find('td:first').text(i+"."); }); } $.ajax({ success: function (res) { updateTableNumeration(); } }); 
  • Thank. It worked. I tried it. But when loading did not display line numbers. Slightly changed and everything fell into place. - Genghis