How to extend the jQuery accordion plugin
Asked Answered
G

4

10

How to extend a jQuery plugin?

currently I am using multiopen accordion plugin.

I need to add new feature like once the expand/collapse is finished I need to callback a function as like change event in jquery ui accordion plugin.

How to add this feature in this plugin.

Grill answered 7/12, 2012 at 18:51 Comment(0)
D
2
$.extend($.ui.multiAccordion, {    
    // private helper method that used to show tabs
    _showTab: function($this) {
        var $span = $this.children('span.ui-icon');
        var $div = $this.next();
        var options = this.options;
        $this.removeClass('ui-state-default ui-corner-all').addClass('ui-state-active ui-corner-top');
        $span.removeClass('ui-icon-triangle-1-e').addClass('ui-icon-triangle-1-s');
        // MODIIFICATION
        bindThis = this;
        var ui = {
            tab: $this,
            content: $this.next('div')
        }
        $div.slideDown('fast', function(){
            $div.addClass(options._classes.divActive);
            // MODIFICATION
            bindThis._trigger('tabShownComplete');
        });
        this._trigger('tabShown', null, ui);
    },

    // private helper method that used to show tabs 
    _hideTab: function($this) {
        var $span = $this.children('span.ui-icon');
        var $div = $this.next();
        var options = this.options;
        $this.removeClass('ui-state-active ui-corner-top').addClass('ui-state-default ui-corner-all');
        $span.removeClass('ui-icon-triangle-1-s').addClass('ui-icon-triangle-1-e');
        // MODIIFICATION
        bindThis = this;
        var ui = {
            tab: $this,
            content: $this.next('div')
        }
        $div.slideUp('fast', function(){
            $div.removeClass(options._classes.divActive);
            // MODIFICATION
            bindThis._trigger('tabHiddenComplete', null, ui);
        });
        this._trigger('tabHidden', null, ui);
    }
});
Desimone answered 20/12, 2012 at 3:39 Comment(2)
You were right ;). It was late and I didnt test my code. I deleted my answer and upvoted yours. Cheers.Esperanzaespial
@Nick thanks for your answer.. initially Nupur's answer helped meGrill
S
5

you dont need the accordion widget for that. you can do this with a few lines of jQuery.

html:

<h3 class="header"> Title 1 </h3>
<div class="content"> Content 1 </div>
<h3 class="header"> Title 2 </h3>
<div class="content"> Content 2 </div>

javascrpt/jQuery:

( function( $ ){ // closure to make sure jQuery = $
  $( function(){ // on document load
    $( ".header" ).click( function( e ){ // select headers and set onClick event handler
      // here you can maybe add a class to an opened header like this
      $( this ).toggleClass( "open" );
      $( this ).next().toggle( "slow", function(){ // toggle visibility
        // what you write here will be executed after the animation
        // "this" will refer to the hidden/revealed div element
        // if you want to call a function depending on if the 
        // panel was opened or closed try this
        if ( $( this ).is( ":visible" ) ) {
          tabOpened( e, this );
        } else {
          tabClosed( e, this );
        }
      }) 
    }).next().hide()
  })
})(jQuery)

and the whole thing working on jsfiddle http://jsfiddle.net/qpqL9/

Staggard answered 17/12, 2012 at 21:43 Comment(0)
M
3

You can easily call your function in the callback functions of the animation done on the tabs. Slight changes in jquery.multi-accordion-1.5.3.js

$div.slideDown('fast', function(){
    $div.addClass(options._classes.divActive);
    //function to be called after expanding the tabs. 
});

$div.slideUp('fast', function(){
    $div.removeClass(options._classes.divActive);
    //function to be called after collapsing the tabs
});
Mise answered 14/12, 2012 at 14:55 Comment(2)
okay,where should i include the above lines and how to call my user defined javascript function as like tabShown: function(event, ui) { }Grill
unfortunately no parameters are sent to the function. But you can easily call any function from in there.Mise
V
2

Have you try the tabHidden and tabShown methods?

 // when tab is shown, ui here hold the same as in click event above
  tabShown: function(event, ui) {}

  // when tab is hidden, ui here hold the same as in click event above
  tabHidden: function(event, ui) {}
Voodoo answered 7/12, 2012 at 18:59 Comment(0)
D
2
$.extend($.ui.multiAccordion, {    
    // private helper method that used to show tabs
    _showTab: function($this) {
        var $span = $this.children('span.ui-icon');
        var $div = $this.next();
        var options = this.options;
        $this.removeClass('ui-state-default ui-corner-all').addClass('ui-state-active ui-corner-top');
        $span.removeClass('ui-icon-triangle-1-e').addClass('ui-icon-triangle-1-s');
        // MODIIFICATION
        bindThis = this;
        var ui = {
            tab: $this,
            content: $this.next('div')
        }
        $div.slideDown('fast', function(){
            $div.addClass(options._classes.divActive);
            // MODIFICATION
            bindThis._trigger('tabShownComplete');
        });
        this._trigger('tabShown', null, ui);
    },

    // private helper method that used to show tabs 
    _hideTab: function($this) {
        var $span = $this.children('span.ui-icon');
        var $div = $this.next();
        var options = this.options;
        $this.removeClass('ui-state-active ui-corner-top').addClass('ui-state-default ui-corner-all');
        $span.removeClass('ui-icon-triangle-1-s').addClass('ui-icon-triangle-1-e');
        // MODIIFICATION
        bindThis = this;
        var ui = {
            tab: $this,
            content: $this.next('div')
        }
        $div.slideUp('fast', function(){
            $div.removeClass(options._classes.divActive);
            // MODIFICATION
            bindThis._trigger('tabHiddenComplete', null, ui);
        });
        this._trigger('tabHidden', null, ui);
    }
});
Desimone answered 20/12, 2012 at 3:39 Comment(2)
You were right ;). It was late and I didnt test my code. I deleted my answer and upvoted yours. Cheers.Esperanzaespial
@Nick thanks for your answer.. initially Nupur's answer helped meGrill

© 2022 - 2024 — McMap. All rights reserved.