Good day, the site has a search and there is a table in which when you click a button, records from the DB are added based on the selected parameter in the search. It is necessary that the records in the table are not added, but updated.

JQuery code itself

$('body').on('click', '#search_appartament', function(event) { event.preventDefault(); $.ajax({ url: "/index/index/", type: "post", data: $('#form_appartament').serialize(), global: false, dataType: "json", response:'text', cache: false, async: false, error: function(req, err){ console.log(err); }, success: function(data) { $('#del').remove(); for ( var i=0; i<data.length; i++ ) { var img = ''; for ( var j=0; j<data[i].id_level; j++ ) { if ( j<5 ) { img = img + '<img src="/images/star.png" alt="Star" />'; } else { img = img + '<strong style="font-size:18px;"> DLX</strong>'; break; } } var strPost = '<tr>' +'<td>' +'<a href="/catalog/nedvizhimost/'+data[i].alias+'">' +'<img src="/images/catalog/tmb/'+data[i].preview+'" alt="" width="193" height="147" />' +'</a>' +'</td>' +'<td valign="top" style="padding-left: 16px;">' +'<div class="ar_levels">'+img+'</div>' +'<div class="ar_title">' +'<a href="/catalog/nedvizhimost/'+data[i].alias+'">'+data[i].name+'</a>' +'</div>' +'<div class="ar_desc">'+data[i].textarea.substr(0,232)+'...</div>' +'<div class="ar_more">' +'<a href="/catalog/nedvizhimost/'+data[i].alias+'">Подробнее &gt;</a>' +'</div>' +'</td>' +'</tr>'; $('#tabl > tbody').append(strPost); } } }); 

});

Here is the search itself

  • You need to empty the container as I understand it. At the beginning of the success, $('#tabl > tbody').empty(); - Artem Gorlachev
  • Or instead of append use html $ ('# tabl> tbody'). Html (strPost); - Saveliy
  • Thanks, earned !!!! - m3xTa1nes
  • @Saveliy, in the example loop, replacing with html() here will only show the last result. In general, insert each object in the loop load. It is more correct to create strPosts before a cycle, in its cycle to add it, and after a cycle already .append() - Artem Gorlachev
  • @Artem Gorlachev Not very carefully looked at the context. Of course it is better to concatenate the string and then make a one-time insert or with preliminary cleaning through empty and then append as you wrote above or html. And not inside the loop. - Saveliy

0