Apply Expand All and Collapse All to Collapsible Panels in ASP.NET AJAX and jQuery

by mosessaur| 22 December 2008| 15 Comments

I got many feedbacks in my previous posts "Building a grouping Grid with GridView and ASP.NET AJAX toolkit CollapsiblePanel" & "GridView Grouping Master/Detail Drill Down using AJAX and jQuery"  asking to for toggle all (Expand All or Collapse All) feature in CollapsiblePanelExtender as well as to in jQuery. Actually I didn't have much time to walkthrough this and provide a demo about it.

Today I got a chance to write few code snippet to run this feature. It was very simple for both jQuery also for ASP.NET AJAX.

I'll start with ASP.NET AJAX [View Demo]. If you wish to apply Toggle All functionality that will collapse or expand all panels on your page you can write code close to this and get it working perfectly:

function toggleAll(collapse) {
	var components = Sys.Application.getComponents()
	for (var i in components) {
		if (AjaxControlToolkit.CollapsiblePanelBehavior.isInstanceOfType(components[i])) {
				components[i].set_Collapsed(collapse);
		}
	}
}

At line 2 I am getting all component created on the page. In line 3 I am looping through all these components. In line 4 I am checking if these component are of Type CollapsiblePanelBehavior. If yes, in line 5 I'm expanding or collapsing each panel based on the passed parameter.

Very simple! but has one issue. What if you want to toggle group of panels on the page and leave the other panels! Then you need some kind of grouping. In my case I don't have this need, however I applied grouping by checking ID of the collapsible element for certain string pattern using regular expression

function toggleAll(collapse) {
    var components = Sys.Application.getComponents()
    for (var i in components) {
        var exp = new RegExp("pnlOrders");
        var match = exp.exec(components[i].get_element().id)
        if (AjaxControlToolkit.CollapsiblePanelBehavior.isInstanceOfType(components[i])) {
            if (match && match.length > 0) {
                components[i].set_Collapsed(collapse);
            }
        }
    }
}

In the above code I am checking if the collapsible element has id that contains "pnlOrders". This way If there are any other collapsible panel in the page that doesn't have a collapsible element that contains "pnlOrders" string in its id will apply this toggle feature. [View Demo]

In jQuery It was very simple too [View Demo], but required some customization in the code as my demo wasn't based on pre-built plug-in or widget on jQuery, it was purely customized jQuery code to apply sliding collapse & expand behaviour. Unlike ASP.NET AJAX where I already had a component of Type CollapsiblePanelBehavior.

My jQuery code was simple anyway:

function toggleAll(collapse) {
    $('.group').each(function(i) {
        var $this = $(this);
        var $details = $this.next();
        if ($details.html() == "") {
            if (!collapse) {
                $this.click();
            }
        }
        else {
            var src = $this.children()[0].src;
            if (collapse) {
                $details.slideUp();
                if (src.endsWith("minus.png"))
                    $this.children()[0].src = src.replace('minus.png', 'plus.png');
            }
            else {
                $details.slideDown();
                if (src.endsWith("plus.png"))
                    $this.children()[0].src = src.replace('plus.png', 'minus.png');
            }
        }
    })
}

At line 2 I get all elements with css class "group". This is equivalent to Expand/Collapse Control -The element that trigger expand or collapse on collapsible element- referenced here as $this. In line 5 I check if the element was expanded before or not by checking if it contains any HTML. If the collapsible element $details does not contain any HTML I check if I am triggering to expand all then I just call "click" function of the Expand/Collapse control to expand the collapsible element. If the element was already expanded before I just check if I am triggering expand all or collapse all and slideUp or slideDown the collapsible element. Also I manage to display the correct image on each case. [View Demo]

You can check out the demos and download the code for more clarification.

Comments (15) -

Janko
Janko Serbia on 12/18/2008 11:26 AM Very nice! I like the examples you do with jQuery and ASP.NET.
mosessaur
mosessaur Egypt on 12/18/2008 12:56 PM @Janko Thank you so much buddy, it is the result of learning from all of you around this amazing community!
Mcbeev
Mcbeev United States on 12/18/2008 1:05 PM Would this work on accordians as well ? I think they use the same Collapsible Panel, I think....
Suprotim Agarwal
Suprotim Agarwal India on 12/18/2008 3:20 PM You can use slideToggle(). I did it for a single panel and the code was as simple as :

<script type="text/javascript">
        $(document).ready(function() {
            $('#pHeader').click(function() {
                $('#pBody').slideToggle('slow');
            });
        });
    
    </script>

http://www.dotnetcurry.com/ShowArticle.aspx?ID=240
mosessaur
mosessaur Egypt on 12/18/2008 5:27 PM @Suprotim Agarwal I know that I can use slideToggle, in this sample I just couldn't use it. However it is used inside Click event Handler of the item header.
mosessaur
mosessaur Egypt on 12/18/2008 5:30 PM @Mcbeev Not sure but I guess no because accordion panel is working on different manner.
csharpsean
csharpsean United States on 12/19/2008 9:28 PM Great post! Keep up the excellent work.
Alo
Alo United Kingdom on 1/3/2009 2:00 PM Please can I have the source code?
mosessaur
mosessaur Egypt on 1/3/2009 7:22 PM @[Alo] Actually the code provided on the post is the only reference now! I'll manage to upload samples
rene
rene United States on 1/10/2009 4:54 AM Hello,

I am very new to this. Can you post the files for the collapse/expand all? I'm having trouble with the code you provided, I don't work with C# and I'm not sure where to place it so that the button functions properly when clicked.

Thanks,
Rene
mattd
mattd Canada on 1/17/2009 5:23 PM Wicked post! This is just what I'm looking for.  Quick question though...
Is it possible to have a single toggle button instead or separate expand & collapse buttons

Matt
FaHaD
FaHaD Saudi Arabia on 2/7/2009 10:41 AM Hi moseesof egypt

it is grat work and I will take time to read all your article
in this article I found what I'm looking for (grouping Gview)

I have a question ? I add select command
and it doesn't not work Frown
I know there is big issue in the postback and
I will search your tips again, but realy I'm harry to finish the page that use
your code.

I'm sorry moseesof if it is stupid Qustion.
mosessaur
mosessaur Egypt on 2/7/2009 1:51 PM @Fahad this is not stupid at all bro. Actually select command won't work here as I guess. I think you might need to find a away to do it on client side instead of using select command with postback option
FaHaD
FaHaD Saudi Arabia on 2/8/2009 6:20 PM Thank you Mohammed for kind words.
I will read more and learn from you bro.

Thank you and God bless you for sharing this great informations.
Torben
Torben Denmark on 2/19/2009 2:40 AM @mosessaur

Thank you for a nice page. Keep up the good work.

@FaHaD

I don't know if this is any good, but here is what I did:

In the CustomerOrders.ascx:

I added:

                            value="" onclick="openmodal('<%# DataBinder.Eval(Container.DataItem, "orderno") %>'" />

In the page (where CustomerOrders.ascx is used) .aspx

I add:

Comments are closed