Check if an ID exists on a data-list

Asked

Viewed 166 times

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

1 answer

1


Is this what you want?

$('.list_details li').on('click',function(){
   var cliked = $(this);
   var found = false;
   $('.tab_aluno tr').each(function(){
     if($(this).attr('id') == cliked.attr('id')) {
         found = true;
         return false; // sair do loop
     }
   });
   if(found) {
      alert('id existe');
   }
   else {
       $('.tab_aluno').before('<tr id="'+cliked.attr("id")+'" class="'+cliked.attr("id")+'"><td><img src="'+cliked.find('img').attr('src')+'"></td><td>'+cliked.find('p').text()+'</td><td>X</td></tr>');  
   }
   $('.list_details_a').hide();
});
  • 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

  • I got it, see if that’s it. I edited my answer

  • 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>

  • I edited. I changed the entries to go through. Instead of li we will walk the table (.tab_aluno)

  • I also added an id to <tr>, you were just adding a class. You really need this class in the new <tr> generated?

  • 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

  • was really the issue of the class, but the proposed structure also helped to optimize the code, thanks!

  • Glad you could help

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.