Javascript 在树表不工作的情况下全部展开/全部折叠?

Javascript 在树表不工作的情况下全部展开/全部折叠?,javascript,html,jquery,Javascript,Html,Jquery,我试图将“全部展开/全部折叠”功能应用于嵌套树表,但得到了所需的输出。我也跟着。这是 | 提特罗 英勇 项目1 123 项目2 123 项目3 123 项目十 莱特拉 项目4 123 项目4x 123 项目5 123 项目6 123 剧本 $(函数(){ $('#mytable')。打开('单击','.toggle',函数(){ //获取更深入的所有 //下面是表中的元素 var findChildren=函数(tr){ var深度=tr数据(“深度”); 返回tr.nextUntil($

我试图将“全部展开/全部折叠”功能应用于嵌套树表,但得到了所需的输出。我也跟着。这是

|
提特罗
英勇
项目1
123
项目2
123
项目3
123
项目十
莱特拉
项目4
123
项目4x
123
项目5
123
项目6
123

剧本


$(函数(){
$('#mytable')。打开('单击','.toggle',函数(){
//获取更深入的所有
//下面是表中的元素
var findChildren=函数(tr){
var深度=tr数据(“深度”);
返回tr.nextUntil($('tr').filter(函数)(){

return$(this).data('depth')如下所示使用….u r在AD中缺少tr标记

<thead>
        <tr>
            <th class="firstCol">Titulo</th>
            <th class="LastCol">Valor</th>
        </tr>
    </thead>

你能提供一个提琴吗?提琴已经在那里提供了…我已经提供了一个提琴,请检查上面提到的链接mysample js。上面的解决方案对于折叠效果很好,但是当我单击“展开”时,所有图像的加号都没有变为负号(折叠)符号。单击“全部展开”链接时,我尝试了一种逻辑来管理展开和折叠图像,但我遇到了一些问题。@Kundan您的解决方案帮助我进行了折叠,可以为exapad All提供更多信息。
$(function() {
    $('#mytable').on('click', '.toggle', function () {
        //Gets all <tr>'s  of greater depth
        //below element in the table
        var findChildren = function (tr) {
            var depth = tr.data('depth');
            return tr.nextUntil($('tr').filter(function () {
                return $(this).data('depth') <= depth;
            }));
        };

        var el = $(this);
        var tr = el.closest('tr'); //Get <tr> parent of toggle button
        var children = findChildren(tr);

        //Remove already collapsed nodes from children so that we don't
        //make them visible. 
        //(Confused? Remove this code and close Item 2, close Item 1 
        //then open Item 1 again, then you will understand)
        var subnodes = children.filter('.expand');
        subnodes.each(function () {
            var subnode = $(this);
            var subnodeChildren = findChildren(subnode);
            children = children.not(subnodeChildren);
        });

        //Change icon and hide/show children
        if (tr.hasClass('collapse')) {
            tr.removeClass('collapse').addClass('expand');
            children.hide();
        } else {
            tr.removeClass('expand').addClass('collapse');
            children.show();
        }
        return children;
    });
   $('.toggle').trigger('click');

   $(".myexpand").click(function () {
            $("#mytable tr").show("slow");
   });
    $(".mycollapse").click(function () {
            $("#mytable tr").hide("fast");
   });


});
<thead>
        <tr>
            <th class="firstCol">Titulo</th>
            <th class="LastCol">Valor</th>
        </tr>
    </thead>
 $(".mycollapse").click(function () {
            $("#mytable tr").hide("fast");
            $("#mytable thead tr").show("fast");
            $("#mytable tr.level0").show("fast");
        });