views:

26

answers:

1

Hello,

I'd like to add a new section to a Jquery UI accordion when someone clicks a link, either on the same page or a different page. I've looked at the append() function but the content I'd be pulling in would contain PHP as well as HTML, so it'd need to be parsed.

The link the user would be clicking would reference a database record to pull e.g. /site/index/record/3 (I'm building in Code Igniter).

What is the best way to do this?

I was thinking of using prependTo (if that's right), then pulling the content in via ajax? Not sure how that would work, clicking through from another page though.

Any ideas, suggestions, pointers or comments gratefully appreciated, as I've no idea how to do this.

Here's the accordion js I have:

$(function() { // jquery ui accordion with mods for sorting
    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); // move the active panel to the top of the accordion
              }
        })
        .sortable({
            axis: "y",
            handle: "h3",
            stop: function() {
                stop = true;
            }
        });

        $(".new_accordian").click(function( event ) {
        $('#ccaccordion').prepend("<div><h3><a href=\"#\">Section X</a></h3><div><p>Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In suscipit faucibus urna.</p></div></div>");
    });

});

Thanks!

+1  A: 

I've accomplished this by altering the HTML to add the new Accordion section then calling something like the following:

$("#days").accordion('destroy').accordion({
    autoHeight: false
    ,navigation: true
    ,collapsible: true
    ,active: false
});

Where <div id="days">...</div> is the container that holds all the accordion sections. Essentially you must destroy then re-initialize the accordion when adding new elements.

So you would get your new section via AJAX, append() the HTML to the accordion container then re-create the accordion.

methodin
Hi Methodin! Thanks for your reply, I've updated my code above for the prepend function I've got working. This now adds the html shown when a link on the page is clicked with class new_accordian. I follow what you're saying about it needing to be recreated, as the new html is just tacked on, it doesn't function with the rest of the accordion. Sorry for the stupid question but, how do I add in your code above? Thanks!
Robimp
So if you have something like <div id="days"><div class="accordion"><h3>Accordion1</h3></div><div class="accordion"><h3>Accordion2</h3></div></div> Then an onclick event somewhere on the page, you fetch the HTML using AJAX. On the callback for that Ajax function to call $("#container").append(responseText); - where responseText is the AJAX response. After that you would then add the code I originally posted. Obviously you have to make sure the HTML returned from AJAX is exactly what is needed for a block in the accordion. If it contains extra HTML it probably won't work.
methodin
Excellent, that makes sense, thanks again for your help! +1
Robimp