home » articles » JavaScript Expandable / Collapsible Panel Control

JavaScript Expandable / Collapsible Panel Control

change text size: A A A

12/4/2009 by samir.nigam

Download the sample application - 13.45 KB

 panal

  

 

 

 

 

 

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

Panel: 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:

 pp

 

 

  

 

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="text/javascript" src="JS/Panel.js"></script>


Create an div element in the Web page.

<div id="parent"></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="text/javascript">
   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. 

 

“This article has been re-published from - Original Article

To rate this article please register or login

Author

samir.nigam samir.nigam (Member since:10/22/2009)

Comments (no comments yet)

Post a comment

Comment (No HTML)  

Type the characters:
 *
 
   

Related articles

Join CodeAsp.Net for FREE Today!

It's fast, easy and free! Submit articles, get your own blog, ask questions & give answers in the forums, and become a better developer, faster.

enter your email address:

Latest Articles RSS Feed

Latest Articles

  • Hi Friends, Many times we came cross the requirement when we need to access the dropdownlist's selectedindexchanged event inside the gridview, like in a shopping cart changing the item's quantity or binding the other dropdown based on the first dropdown.
  • 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.
  • This is my approach to develop custom JavaScript ListBox control. Although it is only a subset of existing HTML ListBox element, it is more user friendly than the existing one. It can be further customized for different requirements. Please let me know about bugs and/or errors & give suggestions to improve this ListBox control.
  • I have tried my best to make this user control code error free. I will most welcome suggestions for further improvement in this user control. I have tested this user control on various browsers and it works fine.
  • So, this is my approach to implement an ASP.NET slide show using the DataList. I have tried my best to keep it bug free. I will most welcome suggestions and criticism for further improvements of this user control. I have tested this user control on various browsers and it works fine.
  • As we all knows that Repeater and DataList does not have auto paging support technique like Gridview or Datagrid, but we can achieve this through PagedDataSource. By using PagedDataSource we can implement paging in Repeater or DataList. Now in our mind there is question arise what is PagedDataSource. PagedDataSource is a class which encapsulates
  • So this is my approach. I was working for a long time to create C# like event handlers for JavaScript classes and finally, I’ve done it. Please let me know of any bugs and suggestions to improve this context menu control.
  • So this is my solution. If you have some other ideas about this functionality, please share them with me.
  • I have tried my best to make this code error free. Suggestions and criticism for further improvements of this code are most welcome.
  • So this is the approach that I've adopted to solve the Hover Delay problem. Although originally I developed Hover Delay to deactivate the click event for 1 second on a GridView row, later I also used Hover Delay to deactivate Drag n Drop of GridView rows. Kindly let me know if any one has some other better or different solution.
  • In this article I will explain how to import data from EXCEL to SQL in ASP.NET . In many situations we have data in the form of excel sheet but we have the requirement to have that data in SQL SERVER DB. I have explained importing data both from Excel 97-2003 as well as Excel 2007 format.
  • The source code shows how to use Regular Expressions in C#. The code Functions written for Validation Alphabet, AlphaNumeric, Integer, Postive Integer, Floating point numbers. You just cut copy these functions and use in any program.
  • That’s all about this technique. Just download the sample application and happy CSS! I have tested this application on various browsers and it worked fine.
  • This script is cross-browser compatible and fast as it iterates elements of a specific tag inside a target element [GridView] rather than iterating in a whole form. It searches the elements of a specific type in a particular column of the target element [GridView].
  • This Article is used to insert a numeric value on the sever control(text box) This is a java script code for the the client side validation. On Page Load Event You can change the events in txtNoOfQuestion.Attributes.Add("onkeypress", "return numericOnly(this);"); like onfocus events like other according to needs.
  • I have toggled visibility of all TD elements of a GridView column in order to create an illusion of smooth dynamic effect with the help of setTimeout method through recursion. Different browsers have different effects of Expanding / Collapsing GridView Columns. In Internet Explorer 7/8, Safari, Google Chrome and Opera, it seems that columns are Exp
  • In this article, I've used the setTimeout method in order to achieve a smooth expand/collapse functionality.
  • Introduction I am going to present here a functionality that selects / deselects all checkboxes of a particular column inside a GridView control, provided the header checkbox of that column is checked or unchecked using JavaScript. This functionality also has a feature that when all checkboxes of a particular column inside the GridView are check
  • This article describes how to toggle the states of all CheckBoxes inside a particular DataGridView column.
  • This article describes how to apply client-side mouse over & mouse out effects on the GridView’s rows.