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+'">Подробнее ></a>' +'</div>' +'</td>' +'</tr>'; $('#tabl > tbody').append(strPost); } } }); });
$('#tabl > tbody').empty();- Artem Gorlachevhtml()here will only show the last result. In general, insert each object in the loop load. It is more correct to createstrPostsbefore a cycle, in its cycle to add it, and after a cycle already.append()- Artem Gorlachev