2013-10-07 38 views
0

我有一個像下面一樣的展開/摺疊嵌套表結構。 在這裏,當我點擊一個家長,所有其他以前擴大父母在該級別,其子必須摺疊。錶行exapnd崩潰jquery

enter image description here

最初的應用(1)擴大,有2個孩子(圖片1)。 當我點擊應用程序(2)時,應用程序(1)的孩子必須摺疊(圖2)。 這應該適用於所有級別。像找到兄弟姐妹和隱藏的東西,但除了被點擊的那個。我怎樣才能做到這一點

siblings().find().not(clicked).hide(); 

這裏是我的小提琴http://jsfiddle.net/vivekcek/rFKJc/

父行有一個類 '父' 和id = '[ID]'。子行通過追加父級id class = child- [id]而具有一個類。

$('tr.parent') 
.css("cursor", "pointer") 
.attr("title", "Click to expand/collapse") 
.click(function() { 
$(this).siblings('.child-' + this.id).toggle(); 
}); 
$('tr[class*=child-]').hide().children('td'); 
+0

'$(本).siblings('。孩子 - ')不是($ (本))隱藏();」。我試過這個,但不工作 – Vivek

+0

還點擊父母的孩子沒有崩潰 – Vivek

回答

2

嘗試

var $trs = $('tr.parent') 
.css("cursor", "pointer") 
.attr("title", "Click to expand/collapse") 
.click(function() { 
    var $sibs = $(this).siblings('.child-' + this.id).toggle(); 
    $(this).siblings().not($sibs).not('.parent').hide() 
}); 
$('tr[class*=child-]').hide().children('td'); 

演示:Fiddle

+0

但我擴大到葉。然後點擊它的第一個父母(vivek),父母將被摺疊。再次當我點擊這個父母(Vivek)。我看到以前擴展的項目並沒有被捲入到葉 – Vivek

+0

Parent1 - > Child1 - > {Parent1.1 - > Child1.1 {Parent1.1.1 -Child1.1.1}}我將展開到子1.1.1然後當我點擊到Parent1所有其他級別必須處於初始狀態 – Vivek

+0

點擊我想我可能需要使用'$ this.closest()。next()。toggle'類似的東西 – Vivek

0

這解決了所有問題

  var $trs = $('tr.parent') 
     .css("cursor", "pointer") 
     .attr("title", "Click to expand/collapse") 
     .click(function() { 
       var $sibs = $(this).siblings('.child-' + this.id).toggle(); 
       $(this).siblings().not($sibs).not('.parent').hide(); 
       $($sibs).find('tr[class*=child-]').hide(); 
     }); 

    $('tr[class*=child-]').hide().children('td');