[英]Cannot click to close accordion in AngularJS
I CANNOT use the Bootstrap or JQuery accordion because of "div"'s and "td"'s so currently I have an accordion through some AngularJS here it is: 由于“ div”和“ td”的原因,我无法使用Bootstrap或JQuery手风琴,所以目前我通过一些AngularJS拥有一个手风琴,它是:
$scope.viewMeasures = function($event) {
var log = $($($event.target)[0]).closest('tbody');
$('tbody.measure.selected').hide();
$('tbody.selected').removeClass('selected');
$(log).addClass('selected');
$(log).next('tbody').fadeIn(20);
$(log).next('tbody').addClass('selected');
}
This allows me to open the accordion but not close. 这使我可以打开手风琴但不能关闭它。 Any suggestions?
有什么建议么?
Here is my Jade for reference as well: 这也是我的翡翠供参考:
body.log(ng-repeat-start="log in logs | orderBy : ['date_created'] : true")
tr.log(ng-click="viewMeasures($event)", id='{{log._id}}')
td
strong {{log.date_created | date:'MM-dd-yyyy'}}
td
span.static
strong {{log.name}}
input.editable.form-control(ng-model='log.name')
td
strong {{log.updated_by}}
It is not a good practice to deal with the DOM in controllers. 在控制器中处理DOM并不是一个好习惯。 If you want to add behaviour to an element, use directives instead.
如果要向元素添加行为,请改用指令。
The approach I usually take is to first look for an HTML element that already has the behaviour I need. 我通常采用的方法是首先查找已经具有所需行为的HTML元素。 Otherwise I'd look for an existing directive or module ( ngmodules.org for example).
否则,我将寻找现有的指令或模块(例如ngmodules.org )。 If I can't find anything, I get the closest element I can find in terms of functionality and augment it.
如果找不到任何内容,则会获得可以在功能方面找到的最接近的元素并将其扩充。
In your case, I'd consider using a <summary>
with <details>
. 在您的情况下,我考虑将
<summary>
与<details>
。 To give the accordion effect (collapse one when another opens), I'd create a directive called group
(or something like this) that would aggregate all <summary>
tags by [group]
. 为了产生手风琴效果(当另一个打开时折叠),我将创建一个名为
group
的指令(或类似的东西),该指令将按[group]
聚合所有<summary>
标签。
Check this JSBin out. 检查此JSBin 。
angular.module('gtDetailsGroup', []).
directive('gtGroup', function () {
var groups = {};
var directive = { restrict: 'A' };
directive.compile = function compile (tElement, tAttrs) {
if (!isDetails(tElement)) return;
var group = tAttrs.gtGroup;
(groups[group] || (groups[group] = [])).push(tElement);
return link;
};
function isDetails (element) {
return element.prop('tagName') === 'DETAILS';
}
function link ($scope, iElement, iAttrs) {
var group = iAttrs.gtGroup;
iElement.on('click', function () {
groups[group].filter(function (element) {
return iElement[0] !== element[0];
}).forEach(function (element) {
element.removeAttr('open');
});
});
}
return directive;
});
In your app, include gtDetailsGroup
as a dependency. 在您的应用程序中,包括
gtDetailsGroup
作为依赖项。 Example: 例:
angular.module('MyApp', ['gtDetailsGroup']);
And use it like: 并像这样使用它:
<details gt:group="metrics">...</details>
<details gt:group="metrics">...</details>
<details gt:group="metrics">...</details>
These three details will behave like an accordion. 这三个细节将表现得像手风琴。 When one opens, the others close.
当一个打开时,其他关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.