Mat*_*ics 14 javascript tree angularjs
我创建了一个嵌套的树,它可能有1到5000个项目,我能够使它工作,但它在显示树之前冻结我的浏览器\加载微调器几秒钟.
如何使其顺利,以便浏览器永远不会冻结?
我怎么知道angularjs何时完成创建或渲染或计算(不确定正确的单词)整个列表,以便我可以删除加载微调器,因为你可以看到范围.$ last将无法工作,因为我们有嵌套ng-repeat和相同适用范围.$ parent.$ last
这是我创建的plunker,但有演示数据 -
http://plnkr.co/edit/GSZEpHjt5YVxqpg386k5?p=preview
示例数据集 - http://pastebin.com/YggqE2MK
在这个例子中并没有太糟糕,但是在我的浏览器中,我的浏览器冻结了超过10秒钟,在我的OWN设置中使用所有其他组件.
我已经考虑过了什么
HTML
<script type="text/ng-template" id="tree_item">
<div ng-init="category.expanded=true">
<div ng-class="{'selected': category.ID==selectedCategoryID}">
<div class="icon icon16" ng-if="category.Children.length>0" ng-click="$parent.category.expanded=!$parent.category.expanded" ng-class="$parent.category.expanded?'col':'exp'"></div>
<div class="icon icon-category" ng-class="'icon-category-'+category.TypeType" ng-style="{'border-color':category.Colour}" ng-attr-title="{{category.Status?Res['wpOPT_Status'+category.Status]:''}}"></div>
<a ng-href="#id={{category.ID}}" ng-class="{'pending-text': category.PendingChange}">{{category.Name}}</a>
</div>
<ul class="emlist" ng-show="category.expanded">
<li ng-repeat="category in category.Children | orderBy:'Name'" ng-include="'tree_item'" ng-class="{'selected': category.ID==selectedCategoryID}" e2-tree-item is-selected="category.ID==selectedCategoryID">
</li>
</ul>
</div>
</script>
<div id="CategoryListContainer" class="dragmenu-container initial-el-height">
<div class="spinner" data-ng-show="status=='loading'"></div>
<ul id="CategoryList" class="dragmenu-list ng-cloak" ng-show="status=='loaded'">
<li ng-repeat="category in tree | orderBy:'Name'" ng-include="'tree_item'" e2-tree-item is-selected="category.ID==selectedCategoryID"></li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
JS
var app = angular.module('recursionDemo', []);
app.controller("TreeController", function($scope, $timeout) {
$scope.status = "loading";
$timeout(function() {
var result = {
"GetCategoryTreeResult": [{ // too big data set so I pasted it here http://pastebin.com/YggqE2MK }];
$scope.tree = result.GetCategoryTreeResult;
$scope.status = "loaded";
}, 3000);
});
app.directive('e2TreeItem', function($timeout) {
function link(scope, element, ngModel) {
scope.$watch('isSelected', function(oldVal, newVal) {
if (scope.isSelected === true) {
element.parentsUntil('#CategoryListContainer', 'li').each(function(index, item) {
angular.element(item).scope().category.expanded = true;
});
}
});
// not working
//if (scope.$parent.$last) {
// console.log("last has been caught");
// var appElement = document.querySelector('[ng-app=recursionDemo]');
// angular.element(appElement).scope().status = "loaded";
//}
}
return {
link: link,
scope: {
isSelected: '=?'
}
};
});
Run Code Online (Sandbox Code Playgroud)
更新:
演示:http://plnkr.co/edit/bHMIU8Mx5grht9nod1CW? p=preview
您可以通过如下更改 app.js 的 L10901 来顺利加载数据。但你应该注意到,如果你仍然想按名称排序,并希望它稳定,你应该只对源数据进行排序,而不是使用“order by”按角度排序。(你想要一些代码来对源数据进行排序吗?)
function getDataGradually(data, childKey, gap, count, updateCb, finishCb) {
const lastPositon = [];
const linearData = [];
const ret = [];
function getLinearData(arr, position) {
arr.forEach(function (obj, index) {
const pos = position.concat([index]);
if (obj[childKey] && obj[childKey].length) {
var children = obj[childKey];
obj[childKey] = [];
linearData.push({
obj,
pos
});
getLinearData(children, pos);
} else {
linearData.push({
obj,
pos
});
}
});
}
getLinearData(data, []);
function insertData({
obj,
pos
}) {
let target = ret;
pos.forEach(function (i, index) {
if (index === pos.length - 1) {
target[i] = obj;
} else {
target = target[i][childKey];
}
});
}
let handled = 0;
function doInsert() {
let start = handled;
let end;
if (handled + count > linearData.length) {
end = linearData.length;
} else {
end = handled + count;
}
for (let i = start; i < end; i++) {
insertData(linearData[i]);
}
handled += count;
if (handled < linearData.length) {
setTimeout(function () {
doInsert();
updateCb && updateCb();
}, gap);
} else {
finishCb && finishCb();
}
}
doInsert();
return ret;
}
$scope.tree = getDataGradually(result.GetCategoryTreeResult, "Children", 0, 30, function () {
$scope.$digest();
}, function () {
//finished
});
//$scope.tree = result.GetCategoryTreeResult;
$scope.status = "loaded";
}, 3000);
Run Code Online (Sandbox Code Playgroud)
旧答案:
您可以使用 $timeout 获取渲染时间。尝试如下更改 app.js 的 L10901。
$scope.tree = result.GetCategoryTreeResult;
console.time("A")
$timeout(function(){
console.timeEnd("A");
});
$scope.status = "loaded";
}, 3000);
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我的电脑上出现了“1931.881ms”。
| 归档时间: |
|
| 查看次数: |
5629 次 |
| 最近记录: |