views:

28

answers:

1

Hello,

I want to have something similar to the sortable accordian in JQuery UI, that automatically sorts itself i.e. the active accordion panel (the open one - last one clicked) automatically moves to the top of the accordion.

Any ideas?

Here's the link to the JQ UI page: http://jqueryui.com/demos/accordion/#sortable

Thanks!

Here's the code I have (in reply to Thomas) :

$(function() {
    var stop = false;
    $("#ccaccordion h3").click(function( event ) {
        if ( stop ) {
            event.stopImmediatePropagation();
            event.preventDefault();
            stop = false;
        }
    });

    $("#ccaccordion")
        .accordion({
            header: "> div > h3",
            autoHeight: false,
            "option",
            "change",
            function(event, ui){
              ui.newHeader.parent().prependTo(this);
            }
        })
        .sortable({
            axis: "y",
            handle: "h3",
            stop: function() {
                stop = true;
            }
        });

});
+1  A: 

Add this code to the demo example:

$('#accordion')
  .accordion(
    'option',
    'change',
    function(event, ui){
      ui.newHeader.parent().prependTo(this);
    }
  );

Edit:

Modification of your code:

$(function() {
    var stop = false;
    $("#ccaccordion h3").click(function( event ) {
        if ( stop ) {
            event.stopImmediatePropagation();
            event.preventDefault();
            stop = false;
        }
    });

    $("#ccaccordion")
        .accordion({
            header: "> div > h3",
            autoHeight: false,
            change:
              function(event, ui){
                ui.newHeader.parent().prependTo(this);
              }
        })
        .sortable({
            axis: "y",
            handle: "h3",
            stop: function() {
                stop = true;
            }
        });

});
Thomas
Hi Thomas! Thanks for your reply. I've updated my post with the code I have, not sure if that's right - my js isn't so great. I'm getting this error in Firebug console 'missing : after property id[Break on this error] function(event, ui){\n' and the accordion is broken?
Robimp
Not sure if you get a notification on my edited answer, so I'm posting this comment to notify you.
Thomas
Beautiful! That works perfectly now, and thanks for the notification, I was pulling my hair out here trying to figure it out. I really must get better at javascript.
Robimp
Glad it worked for you. ;)
Thomas