Folding panel with bootstrap and knockout

I can't seem to make an accordion with KnockoutJS and Bootstrap to work properly. I defined it like this:

<div class="panel-group" id="accordion" data-bind="foreach: Advertisers()">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h4 class="panel-title">
                <span data-toggle="collapse" data-bind="html: $data, attr: { 'data-target': '#' + $data }"></span>
            </h4>
        </div>
    </div>
    <div class="panel-collapse collapse" data-parent="#accordion" data-bind="attr: { id: $data }">
        <div class="panel-body">
             ...content...

      

Advertisers is an observable array of strings, and therefore $ data is a string. I get one "row" for each advertiser.

All lines are collapsed first, and pressing a line expands the content below. So far so good.

The problem is that when I click on another line, I expect the previous one to expand, but it doesn't. (I couldn't get the script to work, with Bootstrap and KnockoutJS ...)

Edited code.

+3


source to share


2 answers


How about a simple custom binding that also allows you to break up your view a bit:

ko.bindingHandlers.bootstrapAccordion = {
  init: function(elem, value, allBindings) {
    var options = ko.utils.unwrapObservable(value()),
        handleClass = '[data-toggle]',
        contentClass = '.collapse',
        openItem = ko.utils.unwrapObservable(options.openItem) || false,
        itemClass = '.' + ko.utils.unwrapObservable(options.item) || '.accordion-group',
        items = $(elem).find(contentClass);

    // toggle: false required to hide items on load
    items.collapse({ parent: elem, toggle: false });
    if (openItem > -1) items.eq(openItem).collapse('show');

    // if the array is dynamic, the collapse should be re-initiated to work properly
    var list = allBindings.get('foreach');
    if (ko.isObservable(list)) {
      list.subscribe(function() { 
        $(elem).find(contentClass).collapse({ parent: elem, toggle: false });              
      });
    }

    $(elem).on('click', handleClass, function() {
        $(elem).find(contentClass).collapse('hide');
        $(this).closest(itemClass).find(contentClass).collapse('show');
    });
  }
};

      



This binding takes 2 parameters (className for the container and optionally the element to open on load), for example:, bootstrapAccordion: {item: 'panel-group', openItem: 0}

and must be set to the same element that has the binding foreach

. Collapsible sections are assumed to have a class collapse

and toggle knobs are assumed to have an attribute data-toggle

.

See it here: http://jsfiddle.net/pkvn79h8/22/

+4


source


I've extended the example Tibet above to enable support for changing the icon (e.g. +/-, up / down arrow) and support for navigating to the next panel by applying the data-open-next attribute to what should go to the next panel on click.

ko.bindingHandlers.bootstrapAccordion = {
    init: function (elem, value, allBindings) {
        var options = ko.utils.unwrapObservable(value()),
            handleClass = '[data-toggle]',
            contentClass = '.collapse',
            openedClass = ko.utils.unwrapObservable(options.openedClass) || 'fa-minus',
            closedClass = ko.utils.unwrapObservable(options.closedClass) || 'fa-plus',
            openCloseToggleClasses = openedClass + ' ' + closedClass,
            openItem = ko.utils.unwrapObservable(options.openItem) || false,
            itemClass = '.' + (ko.utils.unwrapObservable(options.item) || 'accordion-group'),
            items = $(elem).find(contentClass);

        var initializeItems = function(items) {
            // toggle: false required to hide items on load
            items.collapse({ parent: elem, toggle: false });
            if (openItem > -1) {
                items.eq(openItem).collapse('show');
                items.eq(openItem).closest(itemClass).find('.panel-heading').find('i').toggleClass(openCloseToggleClasses);
                items.eq(openItem).closest(itemClass).find('.panel-heading').addClass('active');
            }
        }

        initializeItems(items);

        // if the array is dynamic, the collapse should be re-initiated to work properly
        var list = allBindings.get('foreach');
        if (ko.isObservable(list)) {
            list.subscribe(function () {
                initializeItems($(elem).find(contentClass));
            });
        }

        $(elem).on('click', handleClass, function () {
            $(elem).find(contentClass).collapse('hide');
            $(this).closest(itemClass).find(contentClass).collapse('show');
            $(this).closest(itemClass).parent().find('.panel-heading i').removeClass(openCloseToggleClasses);
            $(this).closest(itemClass).parent().find('.panel-heading i').addClass(closedClass);
            $(this).closest(itemClass).parent().find('.panel-heading').removeClass('active');
            if ($(this).closest(itemClass).find('.panel-collapse').attr('aria-expanded') === "true") {
                $(this).closest(itemClass).find('.panel-heading i').toggleClass(openCloseToggleClasses);
                $(this).closest(itemClass).find('.panel-heading').addClass('active');
            }
        });

        $(elem).on('click', '[data-open-next]', function () {
            $next = $(this).closest(itemClass).next(itemClass).find(handleClass);
            if ($next.length) {
                $next.click();
            } else {
                $same = $(this).closest(itemClass).find(contentClass);
                $same.collapse('hide');
                $same.parent().find('.panel-heading i').removeClass(openCloseToggleClasses);
                $same.parent().find('.panel-heading i').addClass(closedClass);
                $same.parent().find('.panel-heading').removeClass('active');
            }
        });
    }
};

      

An example of markup for use with this binding:



<div data-bind="foreach: listOfThings, bootstrapAccordion: { openItem: 0 }">
    <div class="accordion-group">
        <div class="panel panel-default" style="cursor: pointer;" data-toggle>
            <div class="panel-heading">
                <i class="fa fa-plus fa-pull-left fa-2x"></i>
                <h3 data-bind="text: name">Title of expander</h3>
            </div>
        </div>
        <div class="panel-collapse collapse">
            <div class="panel-body">
                <div class="clearfix" data-accordion-content>
                <!-- content goes here -->

                <!-- ko if: $index() < $parent.listOfThings().length -1 -->
                <button data-open-next>Next Thing</button>
                <!-- /ko -->
                </div>
            </div>
        </div>
    </div>
</div>

      

It would be bad for me if it didn't help :)

0


source







All Articles