Click here to Skip to main content
Licence CPOL
First Posted 28 Aug 2008
Views 138,400
Downloads 688
Bookmarked 92 times

JavaScript Expandable / Collapsible Panel Control

By | 21 Sep 2008 | Article
This article describes how to create an Expandable / Collapsible Panel Control using JavaScript.
panel.gif

Introduction

JavaScript Expandable / Collapsible Panel Control is a cross-browser JavaScript control.

Background

Frankly speaking, I'm inspired to develop this control from the Microsoft Web site. If you surf the Microsoft Web site, then you will find a beautiful Panel Control at the right top corner of the default page. Some time ago, it had also smooth expand / collapse functionality but not now. In my Panel Control I've implemented this smooth expand / collapse functionality through JavaScript animation.

Class

class.jpgPanel: It is a public cross-browser JavaScript class.

Constructor

Panel Control has the following constructor:

  • Panel(Arguments): The constructor of the Panel class takes an argument of the type Object Literal. The definition of the argument Object Literal is given below:

    var Args = {
       Base: _Base, //Base reference where ListBox to be displayed.
       HeaderText: _ HeaderText, // Header Text.
       Width: _Width, // Width of the Control.
       ClickEventHandler: _ClickEventHandler // Reference of the click event handler. 
    };

    Example:

    var Args = {
       Base: document.getElementById('parent'),
       HeaderText: 'Items',
       Width: 200,
       ClickEventListener: OnClick
    };

    In case you assign each Property of the argument Object Literal equal to null, then each property will acquire its default value.

    Properties and their default values have been tabulated below:

    Property Default Value
    Base document.documentElement
    HeaderText Header Text
    Width 200
    ClickEventHandler Anonymous Method

Methods

Panel Control has the following public methods:

  • InsertItem(Text, Value): Used to insert an Panel Item. It takes two arguments:
    • Text: Item Text
    • Value: Item Value
  • GetItems(): Used to get collection of all Panel Items
  • GetItem(Index): Used to get a Panel Item at a given Item index. Returns null in case Item isn't found. It takes one argument:
    • Index: Item Index
  • RemoveItems(): Used to delete all the Panel Items. Returns number of Items deleted
  • RemoveItem(Index): Used to delete a Panel Item at a given Item index. Returns true on successful deletion, else false. It takes one argument
    • Index: Item Index
  • GetTotalItems(): Used to get total number of Panel Items
  • Contains(Index): Used to check whether a Panel Item exists at a given Item index or not. Returns true if Item exists, else false. It takes one argument:
    • Index: Item Index
  • Dispose(): Used to destroy Panel object

Note: The Panel Item is an Object Literal and has the following definition:

var PItem = {
   IsSelected: _IsSelected, // true/false.
   Text: _Text, // Item Text.
   Value: _Value, // Item Value.
}; 

Property

Panel Control has only one property:

  • Version: Used to get the current version of the Panel Control

Event

Panel Control has only one event:

  • Click: Fires when any one Panel Item is clicked

The local anonymous method that responds to the click event (i.e. event handler) has the following signature:

var EventHandler = function(Sender, EventArgs) {}

Here, Sender is the reference of the element (in this case, the Panel Item) that raises the click event and EventArgs is a Object Literal that contains necessary information regarding the event. EventArgs Object Literal has the following definition:

var EventArgs = {
   Text: _Text, // Item Text.
   Value: _Value, // Item Value.
}; 

Using the Control

Add the reference of the Panel.js file in your Web page:

<script type=&quot;text/javascript&quot; src=&quot;JS/Panel.js&quot;></script>

Create an div element in the Web page.

<div id=&quot;parent&quot;></div>

Now create a script tag in the head section of the Web page and put the following code in the window.onload event as:

<script type=&quot;text/javascript&quot;>
   var oPanel = null;
   window.onload = function()
   { 
       var Args = {
             Base: document.getElementById('parent'),
             HeaderText: 'Items',
            Width: 200,
            ClickEventListener: OnClick
         };
 
       oPanel = new Panel(Args); 
       oPanel.InsertItem('MSDN', 'Insert');
       oPanel.InsertItem('HotMail', 'Delete');
       oPanel.InsertItem('Yahoo', 'Update');
       oPanel.InsertItem('Gmail', 'Cancel');
       oPanel.InsertItem('Rediff', 'Truncate');
       oPanel.InsertItem('AOL', 'Select');
       oPanel.InsertItem('Hi5', 'Between');
} </script>

In the above code, first an argument Object Literal with necessary properties has been created. Then Panel Control has been instantiated. Finally some Panel Items have been added to the Panel Object. Don't forget the click event handler wire up in the argument Object Literal as:

ClickEventListener: OnClick

Here, OnClick is the reference of the click event handler that is created as a local anonymous method:

var OnClick = function(Sender, EventArgs)
{
   //Code 
} 

Example:

var OnClick = function(Sender, EventArgs)
{
   var Message = new Array();
   Message.push('Text: ' + EventArgs.Text);
   Message.push('Value: ' + EventArgs.Value);

   document.getElementById('DivMessage').innerHTML = Message.join('<br />');
}

The above method will get called whenever you click on any one Panel Item.

Invoke the Dispose method in the window.onunload event in order to destroy Panel Object as:

window.onunload = function(){ oPanel.Dispose(); }

Conclusion

So this is the approach that I have adopted to develop Expandable / Collapsible Panel Control through JavaScript. Please report bugs, errors and suggestions to improve this control.

Browser Compatibility

I have tested this Panel Control on a number of Web browsers:

Browsers.png

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Samir NIGAM

Team Leader

India India

Member

SAMIR NIGAM is a CodeProject MVP, a Microsoft Certified Technology
Specialist (MCTS)
as well as a Microsoft Certified Professional Developer (MCPD)
in C# for web-based applications. He is an insightful IT professional with
results-driven comprehensive technical skill having rich, hands-on work experience
in web-based applications using ASP.NET, C#, AJAX, Microsoft
Enterprise Library
, MS SQL Server 2005.
He has earned his master degree (MCA) from U.P. Technical University, Lucknow,
INDIA, his post graduate dipoma (PGDCA ) from Institute of Engineering and
Rural Technology, Allahabad, INDIA and his bachelor degree (BSc - Mathematics)
from University of Allahabad, Allahabad, INDIA.
He has good knowledge of Object Oriented Programming, 3-Tier Architecture
and Algorithm Analysis & Design as well as good command over cross-browser
client side programming using JavaScript.
Awards:



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionMy vote of 5 PinmemberFilip D'haene4:52 8 Aug '11  
GeneralCollapsible Panel : Expand and Collapse using Javascript in ASP.NET Pingroupelizas0:32 10 Feb '10  
GeneralWow...good job PinmemberOren Gatgno23:16 2 Jun '09  
GeneralRe: Wow...good job PinmvpSamir NIGAM23:29 2 Jun '09  
GeneralThere is a bug in your new version yet!!! PinmemberJLKEngine00817:32 22 Sep '08  
Generalmore flexible Pinmemberricardp3:28 18 Sep '08  
GeneralRe: more flexible PinmemberSAMir Nigam18:41 21 Sep '08  
QuestionHow can you modify the bug?? PinmemberJLKEngine00818:25 17 Sep '08  
GeneralThere is some bug!!! PinmemberJLKEngine00816:29 6 Sep '08  
GeneralTake a look at Extjs.com PinmemberMember 6600958:22 5 Sep '08  
GeneralRe: Take a look at Extjs.com PinmemberThe Wizard of Doze10:32 6 Sep '08  
GeneralGreat work! PinmemberHelbrax2:11 5 Sep '08  
GeneralRe: Great work! PinmemberSAMir Nigam18:05 7 Sep '08  
AnswerRe: Great work! PinmemberHelbrax1:59 8 Sep '08  
GeneralOutstanding work Samir Nigam Pinmembersarzzaidi23:41 4 Sep '08  
GeneralRe: Outstanding work Samir Nigam PinmemberSAMir Nigam17:55 7 Sep '08  
Generalvery nice Pinmembervegeta4ss6:26 3 Sep '08  
GeneralRe: very nice PinmemberSAMir Nigam19:33 3 Sep '08  
GeneralVery Nice Article PinmemberSandeep Shekhar Upadhyay22:05 31 Aug '08  
GeneralRe: Very Nice Article PinmemberSAMir Nigam17:51 1 Sep '08  
Generalmind blowing Pinmembersan_geit20:25 31 Aug '08  
GeneralRe: mind blowing PinmemberSAMir Nigam23:19 2 Sep '08  
Generalmind blowing!!! PinmemberJHON HOVE0:51 30 Aug '08  
GeneralRe: mind blowing!!! PinmemberSAMir Nigam19:11 31 Aug '08  
Thanks.
 
SAMir Nigam
My Articles

GeneralNice Pinmembersteve_19700:49 30 Aug '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120604.1 | Last Updated 22 Sep 2008
Article Copyright 2008 by Samir NIGAM
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid