简体   繁体   English

AngularJS-我需要一个手风琴,可以同时打开多个面板

[英]AngularJS - I need an accordion with the option to have multiple panels open at the time

I have this directive JS Fiddle expample with the option to open one panel at the time, I need to modify that behavior and give to the user the option to have multiple panels open. 我有该指令JS Fiddle示例 ,可以选择同时打开一个面板,我需要修改该行为,并为用户提供打开多个面板的选项。

Here below you will see the code which is the same on my JS Fiddle Expamle 在下面,您将看到与我的JS Fiddle Expamle相同的代码

    directive("btstAccordion", function () {
    return {
        restrict: "E",
        transclude: true,
        replace: true,
        scope: {},
        template:
            "<div class='accordion' ng-transclude></div>",
        link: function (scope, element, attrs) {

            // give this element a unique id
            var id = element.attr("id");
            if (!id) {
                id = "btst-acc" + scope.$id;
                element.attr("id", id);
            }

            // set data-parent on accordion-toggle elements
            var arr = element.find(".accordion-toggle");
            for (var i = 0; i < arr.length; i++) {
                $(arr[i]).attr("data-parent", "#" + id);
                $(arr[i]).attr("href", "#" + id + "collapse" + i);
            }
            arr = element.find(".accordion-body");
            $(arr[0]).addClass("in"); // expand first pane
            for (var i = 0; i < arr.length; i++) {
                $(arr[i]).attr("id", id + "collapse" + i);
            }
        },
        controller: function () {}
    };
}).
directive('btstPane', function () {
    return {
        require: "^btstAccordion",
        restrict: "E",
        transclude: true,
        replace: true,
        scope: {
            title: "@",
            category: "=",
            order: "="
        },
        template:
            "<div class='accordion-group' >" +
            "  <div class='accordion-heading'>" +
            "    <a class='accordion-toggle' data-toggle='collapse'> {{category.name}} - </a>" +

            "  </div>" +
            "<div class='accordion-body collapse'>" +
            "  <div class='accordion-inner' ng-transclude></div>" +
            "  </div>" +
            "</div>",
        link: function (scope, element, attrs) {
            scope.$watch("title", function () {
                // NOTE: this requires jQuery (jQLite won't do html)
                var hdr = element.find(".accordion-toggle");
                hdr.html(scope.title);
            });
        }
    };
})

What can I do ? 我能做什么 ?

You just need to remove the data-parent attribute ( DEMO ): 您只需要删除data-parent属性( DEMO ):

//...
// set data-parent on accordion-toggle elements
var arr = element.find(".accordion-toggle");
for (var i = 0; i < arr.length; i++) {
    //$(arr[i]).attr("data-parent", "#" + id);          <------- here
    $(arr[i]).attr("href", "#" + id + "collapse" + i);
}
//...

This question IMO is a perfect example of not using angular well. IMO这个问题是不正确使用角度的完美示例。 I would suggest removing the entire directive, and jQuery, as they are unnecessary for a simple accordion (ie angular is perfect for this type of ui). 我建议删除整个指令和jQuery,因为它们对于简单的手风琴是不必要的(即,角度对于这种类型的ui来说是完美的)。 Here is an updated version: 这是更新的版本:

http://jsfiddle.net/MTKp7/131/ http://jsfiddle.net/MTKp7/131/

Now I have left it as verbose as possible so that you can make the choice about how you should abstract it (by using ng-repeat mixed with ng-include for example). 现在,我将其尽可能详细地保留下来,以便您可以选择如何抽象它(例如,通过将ng-repeat与ng-include混合使用)。 I have also left the dependencies on the libraries so that the styles are preserved, but these are also not difficult to grab. 我还保留了对库的依赖关系,以便保留样式,但是这些也不难掌握。

Here is the basic example of how you could create this functionality. 这是如何创建此功能的基本示例。 The div structure and classes were only left to match the jQuery object. 仅保留div结构和类以匹配jQuery对象。

<div class="accordion" ng-controller="AccordionCtrl">
    <div class="accordion-group">
        <div class="accordion-heading">
            <a class="accordion-toggle" ng-click="toggle('a')">test</a>
        </div>
        <div class="accordion-body">
            <div class="accordion-inner" ng-show="show.a">
                <div>Anim pariatur cliche reprehenderit, enim eiusmod high life 
            accusamus terry richardson ad squid. 3 wolf moon officia aute,
            non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt
            laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird
            on it squid single-origin coffee nulla assumenda shoreditch et.
            Nihil anim keffiyeh helvetica, craft beer labore wes anderson 
            cred nesciunt sapiente ea proident. Ad vegan excepteur butcher
            vice lomo. Leggings occaecat craft beer farm-to-table, raw 
            denim aesthetic synth nesciunt you probably haven't heard of 
            them accusamus labore sustainable VHS.</div>
            </div>
        </div>
    </div>
</div>

And for toggling: 对于切换:

$scope.toggle = function(index) {
    $scope.show[index] = !$scope.show[index];
};

This? 这个?

http://jsfiddle.net/MTKp7/129/ http://jsfiddle.net/MTKp7/129/

Commented this line: 评论此行:

//$(arr[i]).attr("data-parent", "#" + id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多个引导手风琴面板-一次只能打开一个 - Multiple bootstrap accordion panels - only one open at a time 多个页面的面板名称相同,需要滑动才能打开 - Have multiple pages with panels that have the same name and need to swipe open jQuery手风琴-一次打开多个面板 - jQuery Accordion - Open multiple panels at once 带多个孩子的手风琴菜单(面板) - Accordion Menu with Multiple Children (panels) 如何使用 JS 媒体查询 (matchMedia()) 在移动设备中打开单个手风琴面板,并在桌面设备中打开多个面板? - How to open single accordion panel in mobile, and multiple panels in desktop using JS media queries (matchMedia())? 如何在页面加载时保持手风琴面板打开,但之后保持其可折叠性? - How can I keep my accordion panels open when the page loads, but maintain its collapsibility afterwards? 手风琴面板将无法打开(我该如何使div落后于其他人) - Accordion Panels wont open (how do i keep div behind others) 我需要点击一个按钮打开一个特定的手风琴组 - I need to open a particular accordion group on click of a button 我需要根据箭头点击打开和关闭手风琴 - I need to open and close accordion based on arrow click 多个 div 打开手风琴 - multiple divs open accordion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM