javascript - How to get number of elements with certain class that occur before current element in a table? -
i trying index tr
of table class slot
, slineitem
lineitem comes under lot in hierarchy.
<table style="width:100%;" id="summarytable"> <tr class="slot"> <td>lot1</td> </tr> <tr class="slineitem"> <td>lineitem1</td> </tr> <tr class="slot"> <td>lot2</td> </tr> </table>
i want :
1 lot1 lot1 desc 1.1 lineitem1 2 lot2 2.1 lineitem1 under lot2
assume, using table option.
i think work
//add column rows $('tr').prepend($('<td />', { class: 'srno' })); $('.slot').each(function(i, el) { //get index of current slot element var index = + 1; //set current slot rows index text $(el).find('.srno').text(index); //find rows in between , next slot .slineitem , set text $(el).nextuntil('.slot').filter('.slineitem').each(function(j, el2) { $(el2).find('.srno').text(index + '.' + (j + 1)); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table style="width:100%;" id="summarytable"> <tr class="slot"> <td>lot1</td> </tr> <tr class="slineitem"> <td>lineitem1</td> </tr> <tr class="slot"> <td>lot2</td> </tr> </table>
Comments
Post a Comment