Created
December 5, 2016 21:24
-
-
Save intellix/61befe98cbc69af5b499bc8fbf29af49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
angular | |
.module('chunkFilter') | |
.filter('chunk', chunk); | |
function chunk() { | |
return function(value, size, vertical, preserveKeys) { | |
var chunks = []; | |
var chunkCount = size; | |
var chunkIndex; | |
if (!value || value.length === 0) { | |
return value; | |
} | |
vertical = vertical || false; | |
preserveKeys = preserveKeys || false; | |
if (!vertical) { | |
chunkCount = Math.ceil(value.length / size); | |
} | |
for (chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++) { | |
chunks.push([]); | |
} | |
chunkIndex = 0; | |
value.forEach(function(value, key) { | |
if (preserveKeys) { | |
chunks[chunkIndex][key] = value; | |
} else { | |
chunks[chunkIndex].push(value); | |
} | |
if (++chunkIndex === chunkCount) { | |
chunkIndex = 0; | |
} | |
}); | |
// Hack against infdig when used in view - http://stackoverflow.com/a/29117743 | |
if (!value.$$splitListFilter || !angular.equals(value.$$splitListFilter, chunks)) { | |
value.$$splitListFilter = chunks; | |
} | |
return value.$$splitListFilter; | |
}; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment