2015-08-31 30 views
0

我想表的索引trsLot類和sLineitem如何獲取表中當前元素之前出現的具有某個類的元素的數量?

LINEITEM談到地塊下層次結構。

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

我想這個樣子:

1 Lot1 
    Lot1 desc 
1.1 Lineitem1 
2 Lot2 
2.1 Lineitem1 under lot2 

假設,使用表是唯一的選擇。

+0

我希望我的咖啡:) – Satpal

+4

它很難理解的問題,因爲你的「我要這個樣子塊「與給出的代碼不匹配 - 請澄清,以便我們幫助 – Josh

+0

'Lineitem進入批次層次結構 - 錯誤狀態。沒有這樣的班級,類似的班級在層次上處於同一級別。 「我希望這看起來像'你試過了什麼? – Justinas

回答

0

我認爲這會爲你工作

//Add the extra column to all rows 
 
$('tr').prepend($('<td />', { 
 
    class: 'srno' 
 
})); 
 

 
$('.sLot').each(function(i, el) { 
 
    //get the index of the current sLot element 
 
    var index = i + 1; 
 
    //set the current sLot rows index text 
 
    $(el).find('.srno').text(index); 
 
    //find all rows in between this and the next sLot that are .sLineitem and set their 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>

https://jsfiddle.net/4p0sw3fy/1/

+0

在選定的行中插入td而忽略其他行可能會弄亂設計。你可以改變它在每個tr中添加class ='srno'的額外td,然後設置lot和lineitem的文本嗎? – Arbaaz

+0

是否有可能使它在http://jsfiddle.net/gx6sefr6/1/中工作 – Arbaaz

+0

在編輯中添加了所有'tr'的列 – DGS

相關問題