Click here to Skip to main content
11,647,810 members (62,162 online)
Click here to Skip to main content

JavaScript Expandable / Collapsible Panel Control

, 21 Sep 2008 CPOL 194.8K 1.2K 97
Rate this:
Please Sign up or sign in to vote.
This article describes how to create an Expandable / Collapsible Panel Control using JavaScript.
panel.gif

Introduction

JavaScript Expandable / Collapsible Panel Controlis 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 Controlat the right top corner of the default page. Some time ago, it had also smooth expand / collapse functionality but not now. In my Panel ControlI've implemented this smooth expand / collapse functionality through JavaScript animation.

Class

class.jpgPanel: It is a publiccross-browser JavaScript class.

Constructor

Panel Control has the following constructor:

  • Panel(Arguments): The constructor of the Panelclass takes an argument of the type Object Literal. The definition of the argument Object Literalis 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 Propertyof the argument Object Literalequal to null, then each propertywill acquire its default value.

    Propertiesand their default values have been tabulated below:

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

Methods

Panel Controlhas the following publicmethods:

  • 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 Itemat a given Item index. Returns nullin case Itemisn't found. It takes one argument:
    • Index: Item Index
  • RemoveItems(): Used to deleteall the Panel Items. Returns number of Itemsdeleted
  • RemoveItem(Index): Used to deletea Panel Itemat a given Item index. Returns trueon 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 Itemexists at a given Item indexor not. Returns trueif Itemexists, else false. It takes one argument:
    • Index: Item Index
  • Dispose(): Used to destroy Panel object

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

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

Property

Panel Controlhas only one property:

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

Event

Panel Controlhas only one event:

  • Click: Fires when any one Panel Itemis 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, Senderis the reference of the element (in this case, the Panel Item) that raises the click eventand EventArgsis a Object Literalthat contains necessary information regarding the event. EventArgs Object Literalhas 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 divelement in the Web page.

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

Now create a scripttag in the headsection of the Web page and put the following code in the window.onload eventas:

<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 Literalwith necessary properties has been created. Then Panel Controlhas been instantiated. Finally some Panel Itemshave been added to the Panel Object. Don't forget the click event handler wire up in the argument Object Literalas:

ClickEventListener: OnClick

Here, OnClickis 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 Disposemethod in the window.onunload eventin order to destroy Panel Objectas:

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)

Share

About the Author

Samir NIGAM
Technical Lead Infogain India Pvt Ltd
India India

Samir NIGAM is a Microsoft Certified Professional. He is an insightful IT professional with results-driven comprehensive technical skill having rich, hands-on work experience n web-based applications using ASP.NET, C#, AJAX, Web Service, WCF, jQuery, Microsoft Enterprise Library , LINQ, MS Entity Framework, nHibernate, MS SQL Server & SSRS.

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, n-Tier Architecture, SOLID Principle, and Algorithm Analysis & Design as well as good command over cross-browser client side programming using JavaScript & jQuery,.

Awards:


You may also be interested in...

Comments and Discussions

 
QuestionMy vote of 5 Pin
Filip D'haene8-Aug-11 4:52
memberFilip D'haene8-Aug-11 4:52 
GeneralCollapsible Panel : Expand and Collapse using Javascript in ASP.NET Pin
elizas10-Feb-10 0:32
groupelizas10-Feb-10 0:32 
GeneralWow...good job Pin
Oren Gatgno2-Jun-09 23:16
memberOren Gatgno2-Jun-09 23:16 
GeneralRe: Wow...good job Pin
Samir NIGAM2-Jun-09 23:29
mvpSamir NIGAM2-Jun-09 23:29 
GeneralThere is a bug in your new version yet!!! Pin
JLKEngine00822-Sep-08 17:32
memberJLKEngine00822-Sep-08 17:32 
Generalmore flexible Pin
ricardp18-Sep-08 3:28
memberricardp18-Sep-08 3:28 
GeneralRe: more flexible Pin
SAMir Nigam21-Sep-08 18:41
memberSAMir Nigam21-Sep-08 18:41 
QuestionHow can you modify the bug?? Pin
JLKEngine00817-Sep-08 18:25
memberJLKEngine00817-Sep-08 18:25 
GeneralThere is some bug!!! Pin
JLKEngine0086-Sep-08 16:29
memberJLKEngine0086-Sep-08 16:29 
GeneralTake a look at Extjs.com Pin
Member 6600955-Sep-08 8:22
memberMember 6600955-Sep-08 8:22 
GeneralRe: Take a look at Extjs.com Pin
The Wizard of Doze6-Sep-08 10:32
memberThe Wizard of Doze6-Sep-08 10:32 
GeneralGreat work! Pin
Helbrax5-Sep-08 2:11
memberHelbrax5-Sep-08 2:11 
GeneralRe: Great work! Pin
SAMir Nigam7-Sep-08 18:05
memberSAMir Nigam7-Sep-08 18:05 
AnswerRe: Great work! Pin
Helbrax8-Sep-08 1:59
memberHelbrax8-Sep-08 1:59 
GeneralOutstanding work Samir Nigam Pin
sarzzaidi4-Sep-08 23:41
membersarzzaidi4-Sep-08 23:41 
GeneralRe: Outstanding work Samir Nigam Pin
SAMir Nigam7-Sep-08 17:55
memberSAMir Nigam7-Sep-08 17:55 
Generalvery nice Pin
vegeta4ss3-Sep-08 6:26
membervegeta4ss3-Sep-08 6:26 
GeneralRe: very nice Pin
SAMir Nigam3-Sep-08 19:33
memberSAMir Nigam3-Sep-08 19:33 
GeneralVery Nice Article Pin
Sandeep Shekhar Upadhyay31-Aug-08 22:05
memberSandeep Shekhar Upadhyay31-Aug-08 22:05 
GeneralRe: Very Nice Article Pin
SAMir Nigam1-Sep-08 17:51
memberSAMir Nigam1-Sep-08 17:51 
Generalmind blowing Pin
san_geit31-Aug-08 20:25
membersan_geit31-Aug-08 20:25 
GeneralRe: mind blowing Pin
SAMir Nigam2-Sep-08 23:19
memberSAMir Nigam2-Sep-08 23:19 
Generalmind blowing!!! Pin
JHON HOVE30-Aug-08 0:51
memberJHON HOVE30-Aug-08 0:51 
GeneralRe: mind blowing!!! Pin
SAMir Nigam31-Aug-08 19:11
memberSAMir Nigam31-Aug-08 19:11 
GeneralNice Pin
steve_197030-Aug-08 0:49
membersteve_197030-Aug-08 0:49 
GeneralRe: Nice Pin
SAMir Nigam31-Aug-08 19:10
memberSAMir Nigam31-Aug-08 19:10 
GeneralGood but JQuery already does this! Pin
azamsharp29-Aug-08 9:03
memberazamsharp29-Aug-08 9:03 
GeneralRe: Good but JQuery already does this! Pin
SAMir Nigam31-Aug-08 19:10
memberSAMir Nigam31-Aug-08 19:10 
AnswerRe: Good but JQuery already does this! Pin
Kanedogg0816-Sep-08 4:25
memberKanedogg0816-Sep-08 4:25 
GeneralRe: Good but JQuery already does this! Pin
SAMir Nigam16-Sep-08 4:43
memberSAMir Nigam16-Sep-08 4:43 
GeneralNice article Pin
knptech29-Aug-08 3:47
memberknptech29-Aug-08 3:47 
GeneralRe: Nice article Pin
SAMir Nigam30-Aug-08 0:21
memberSAMir Nigam30-Aug-08 0:21 
GeneralNice Article Pin
Abhijit Jana28-Aug-08 19:14
memberAbhijit Jana28-Aug-08 19:14 
GeneralRe: Nice Article Pin
SAMir Nigam30-Aug-08 0:20
memberSAMir Nigam30-Aug-08 0:20 
GeneralRe: Nice Article Pin
Abhijit Jana30-Aug-08 1:28
memberAbhijit Jana30-Aug-08 1:28 
GeneralRe: Nice Article Pin
SAMir Nigam31-Aug-08 19:08
memberSAMir Nigam31-Aug-08 19: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.

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