我有這樣的分層數據:角追加parent屬性值
[
{
"children":[
{
"children":[...],
[...]
},
{
"children":[...],
[...]
},
],
[...]
}
]
我想通過扁平化的數據建立樹狀網格。我使用以下指令:
app.directive('tree', function (hierarchyService, logger, $timeout) {
return {
scope: {
data: '='
},
restrict: 'E',
replace: true,
template: '<div>' +
'<table class="table table-striped table-hover">' +
' <thead>' +
' <tr>' +
' <th class="col-md-6">Account name</th>' +
' </tr>' +
' </thead>' +
' <tbody><tr collection data="data" /></tbody>' +
' </table>' +
'</div>'
};
});
app.directive('collection', function() {
return {
restrict: "A",
replace: true,
scope: {
data: '=',
depth: '@'
},
template: '<member ng-repeat="member in data" member="member" depth="{{depth}}" />',
link: function (scope, element, attrs) {
scope.depth = parseInt(scope.depth || 0);
}
}
});
app.directive('member', function($compile) {
return {
restrict: "E",
replace: true,
scope: {
depth: '@',
member: '='
},
template: '<tr ng-class="{selected: member.selected}">' +
'<td>' +
' <span ng-show="depth > 0" style="width: {{depth * 16}}px; display: inline-block;"></span> {{member.name}}' +
'</td>' +
'</tr>',
link: function (scope, element, attrs) {
scope.depth = parseInt(scope.depth || 0);
if (angular.isArray(scope.member.children) && scope.member.children.length > 0) {
var el = angular.element('<tr collection data="member.children" depth="{{newDepth}}" />');
scope.depth = parseInt(scope.depth || 0);
scope.newDepth = scope.depth + 1;
$compile(el)(scope);
// Flatten hierarchy, by appending el to parent
element.parent().append(el);
}
}
}
});
的問題是,在收集從link
方法添加,從父範圍depth
附加到newDepth
。因此,級別3節點的depth
的值爲depth="3 2 1 "
。
如何禁用繼承depth
?
我也注意到,當我在collection
和member
指令中將replace
更改爲false時,深度按預期工作,但是HTML無效。
你能爲你的情況下,plunker或小提琴? –
@SpartakLalaj您在這裏:https://plnkr.co/edit/CE2z5WeU41H4qJQQ73B7?p=preview – Krzysztof
我刪除了深度屬性,並在集合中聲明瞭等號並且似乎這些數字是正確的。試試看看這是否是正確的結果! –