Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Add cache option to ngRepeat to keep filtered out DOM element #1865

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/ng/directive/ngRepeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
*
* For example: `(name, age) in {'adam':10, 'amalie':12}`.
*
*@param {boolean} keepCache(optional) When true keep every DOM element created from an object in the collection in a cache. Usefull when the collection is filtered many times and DOM creation of an element is slow. DOM elements corresponding to filtered out objects are simply hidden, not deleted.
*
* @example
* This example initializes the scope to a list of names and
* then uses `ngRepeat` to display every person:
Expand Down Expand Up @@ -64,6 +66,7 @@ var ngRepeatDirective = ngDirective({
compile: function(element, attr, linker) {
return function(scope, iterStartElement, attr){
var expression = attr.ngRepeat;
var keepCache = !!attr.keepCache;
var match = expression.match(/^\s*(.+)\s+in\s+(.*)\s*$/),
lhs, rhs, valueIdent, keyIdent;
if (! match) {
Expand Down Expand Up @@ -176,8 +179,13 @@ var ngRepeatDirective = ngDirective({
array = lastOrder[key];
while(array.length) {
value = array.pop();
value.element.remove();
value.scope.$destroy();
if (keepCache) {
nextOrder.push(value.scope[valueIdent], value);
value.element.css("display", "none");
} else {
value.element.remove();
value.scope.$destroy();
}
}
}
}
Expand Down