5,659,336 members and growing! (20,374 online)
Email Password   helpLost your password?
Web Development » Client side scripting » Controls     Intermediate License: The Code Project Open License (CPOL)

JavaScript Expandable / Collapsable Panel Control

By Sameer NIGAM

This article describes how to create an Expandable / Collapsable Panel Control using JavaScript.
C# (C# 2.0, C# 3.0, C#), Javascript, CSS, HTML, XHTML, JScript .NET, .NET (.NET, .NET 2.0, .NET 3.5), ASP, ASP.NET, Visual Studio (VS2008, VS.NET2003, VS2005, Visual Studio), ADO.NET, Ajax, Design, Dev

Posted: 28 Aug 2008
Updated: 21 Sep 2008
Views: 15,960
Bookmarked: 59 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
63 votes for this Article.
Popularity: 7.35 Rating: 4.09 out of 5
16 votes, 25.4%
1
3 votes, 4.8%
2
1 vote, 1.6%
3
5 votes, 7.9%
4
38 votes, 60.3%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
panel.gif

Introduction

JavaScript Expandable / Collapsable Panel Control is a cross-browser JavaScript Contol.

Background

Frankly speaking I’ve inspired to develop this control from the Microsoft web site http://www.microsoft.com/en/us/default.aspx. if you surf Microsoft web site then you will find an 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 following constructor:

method.pngPanel(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 asign each Property of the argument Object Literal equal to null, then each property will acquire its default value.

Properties & 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 following public methods:

method.pngInsertItem(Text, Value): Used to insert an Panel Item. It takes two arguments –

  • Text: Item Text.
  • Value: Item Value.

method.pngGetItems(): Used to get collection of all Panel Items.

method.pngGetItem(Index): Used to get a Panel Item at a given Item index. Returns null in case of Item isn’t found. It takes one argument

  • Index: Item Index.

method.pngRemoveItems(): Used to delete all the Panel Items. Returns no of Items deleted.

method.pngRemoveItem(Index): Used to delete a Panel Item at a given Item index. Returns true on successfull deletion else false. It takes one argument -

  • Index: Item Index.

method.pngGetTotalItems():Used to get total no of Panel Items.

method.pngContains(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.

method.pngDispose(): Used to destroy Panel object.

Note: The Panel Item is an Object Literal & 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:

property.pngVersion: Used to get the current version of the Panel Control.

Event

Panel Control has only one event:

event.pngClick: 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) {}

where Sender is the reference of the element(in this case the Panel Item) that raises the click event & EventArgs is a Object Literal that contains necessary information regarding the event. EventArgs Object Literal has 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 pag:
<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 & 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
Where 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 gets called when ever 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 / Collapsable Panel Control through JavaScript. Please report me bugs, errors & suggestions to improve this control.

Browser Compability

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

Sameer NIGAM


Currently I'm working as a Sr. Software Engineer in SRM Techsol Pvt. Ltd., Lucknow, INDIA. I possess following degrees:

  • MCA from U.P. Technical University, Lucknow, INDIA.
  • PGDCA from Institute of Engineering and Rural Technology, Allahabad, INDIA.
  • BSc from University of Allahabad, Allahabad, INDIA.


Award: Best ASP.NET article of June 2008: JavaScript ListBox Control

Occupation: Software Developer (Senior)
Company: STPL
Location: India India

Other popular Client side scripting articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 32 (Total in Forum: 32) (Refresh)FirstPrevNext
GeneralThere is a bug in your new version yet!!!memberJLKEngine00818:32 22 Sep '08  
Generalmore flexiblememberricardp4:28 18 Sep '08  
GeneralRe: more flexiblememberSAMir Nigam19:41 21 Sep '08  
GeneralHow can you modify the bug??memberJLKEngine00819:25 17 Sep '08  
GeneralThere is some bug!!!memberJLKEngine00817:29 6 Sep '08  
GeneralTake a look at Extjs.commemberMember 6600959:22 5 Sep '08  
GeneralRe: Take a look at Extjs.commemberThe Wizard of Doze11:32 6 Sep '08  
GeneralGreat work!memberHelbrax3:11 5 Sep '08  
GeneralRe: Great work!memberSAMir Nigam19:05 7 Sep '08  
AnswerRe: Great work!memberHelbrax2:59 8 Sep '08  
GeneralOutstanding work Samir Nigammembersarzzaidi0:41 5 Sep '08  
GeneralRe: Outstanding work Samir NigammemberSAMir Nigam18:55 7 Sep '08  
Generalvery nicemembervegeta4ss7:26 3 Sep '08  
GeneralRe: very nicememberSAMir Nigam20:33 3 Sep '08  
GeneralVery Nice ArticlememberSandeep Shekhar Upadhyay23:05 31 Aug '08  
GeneralRe: Very Nice ArticlememberSAMir Nigam18:51 1 Sep '08  
Generalmind blowingmembersan_geit21:25 31 Aug '08  
GeneralRe: mind blowingmemberSAMir Nigam0:19 3 Sep '08  
Generalmind blowing!!!memberJHON HOVE1:51 30 Aug '08  
GeneralRe: mind blowing!!!memberSAMir Nigam20:11 31 Aug '08  
GeneralNicemembersteve_19701:49 30 Aug '08  
GeneralRe: NicememberSAMir Nigam20:10 31 Aug '08