1
I’m doing an ajax crud where user chooses a result coming from a data-list to be added to a table(html).
data-list
<div class="list_details">
<ul>
<li id="valor"><img src="..."><p>Nome</p></li>
<hr>
<li id="valor"><img src="..."><p>Nome</p></li>
<hr>
<li id="valor"><img src="..."><p>Nome</p></li>
<hr>
</ul>
</div>
By clicking on one of the li
an event is triggered:
$('.list_details li').on('click',function(){
if(! $('.tab_aluno').find('tr').attr('id') == $(this).attr('id') ){
$('.tab_aluno').before('<tr class='+$(this).attr("id")+'><td><img src="'+$(this).find('img').attr('src')+'"></td><td>'+$(this).find('p').text()+'</td><td class="remove">X</td></tr>');
}
$('.list_details').hide();
});
The code to do Insert in the tables works, but I want to check in the table itself if the id has already been included. There where the bug picks up, at the time to do this "IF THE ID EXISTS IN THE TABLE"
The code above will generate something like:
<table class="tab_aluno">
<tr id="id">
<td><img src="..."></td>
<td>Nome</td>
<td class='remove'>X</td>
</tr>
<tr id="id">
<td><img src="..."></td>
<td>Nome</td>
<td class='remove'>X</td>
</tr>
</table>
Therefore, I am looking for a logic for the if, so that it searches on all <tr>
already existing in the <table>
and make sure the id has already been included to avoid duplicate results
not exactly that, I forgot to mention that in the code there is a
<table>
empty, which is populated as the user clicks on the data-list. this data-list is dynamically mounted with word search. what I want is to prevent the same result found in the date-list from being inserted more than once in the table– Adriano Luz
I got it, see if that’s it. I edited my answer
– Miguel
Not yet. By clicking on the data list I can get the id value without problems. I’m trying to get this id and do a search inside the
<table>
to see if the value has already been assigned to one of its<tr>
– Adriano Luz
I edited. I changed the entries to go through. Instead of
li
we will walk thetable
(.tab_aluno
)– Miguel
I also added an id to
<tr>
, you were just adding a class. You really need this class in the new<tr>
generated?– Miguel
My goodness, look at this class
<tr>
, very likely that was the problem, I went through the code about 20 times and I didn’t see the class– Adriano Luz
was really the issue of the class, but the proposed structure also helped to optimize the code, thanks!
– Adriano Luz
Glad you could help
– Miguel