parent children from the second tr

1

How can I use the following code to change the ids from the second tr or omit the first tr, since the table has an initial tr that shows the names of the columns which do not handle data.

parent.children("tbody").children("tr").each(function(e){
       $(this).attr('id', (e));
});

Any help is welcome.

    
asked by user2820116 19.01.2018 в 15:21
source

4 answers

3

Take the names of the tbody columns and put them in the thead

Example

<table>
    <thead>
         Tr's nombres de columna (cabecera)
    </thead>
    <tbody>
        Tr's cuerpo de la tabla
    </tbody>

This way you use the tags provided by html for this purpose and you avoid having to skip the first tr

    
answered by 19.01.2018 / 16:06
source
1

Try using gt (), this allows you to indicate which one to start with:

parent.children("tbody").children("tr:gt(0)").each(function(e){
       $(this).attr('id', (e));
});
    
answered by 19.01.2018 в 16:08
1

Look, you could do the following, to the tbody put a id ,

 <tbody id="tb">
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
</tbody>

Then with javascript , using jquery you can style the tr or do with them what you want

$("#tb").children("tr").each(function(index,obj){ // index te da las cant 0 1 2 3 ... y obj te da el objeto en si <tr>bbb</tr>  <tr>aaaa</tr>  

     if(index > 0 ){ //trabajas con los que index > 0 para omitir el primero
         $(obj).css('color','red')
    }

});
    
answered by 19.01.2018 в 16:12
1

The same method of .each has a built-in parameter to know in what index we are going over the elements that it finds.

Remember that the .each method runs through an array of matching elements.

It would simply be to put as an index parameter, and a conditional validation that only takes from the second tr.

link

$(document).ready(function(){

  $('table').children("tbody").children("tr").each(function(index, e){
     if( index >= 1){
        console.log($(this).attr('id'));
     }
  });
});
table tr td{
  border:1px solid black;
}
<table>
  <tbody>
    <tr id="firstRow">
      <td>Primer fila</td>
    </tr>
    <tr id="secondRow">
      <td>Segunda fila</td>
    </tr>
    <tr id="thirdRow">
      <td>Tercer fila</td>
    </tr>
    <tr id="fourthRow">
      <td>Cuarta fila</td>
    </tr>
  </tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
answered by 19.01.2018 в 16:17