Skip to content

Instantly share code, notes, and snippets.

@arturo182
Last active December 6, 2019 15:26
Show Gist options
  • Save arturo182/1eacf225967c690bd6feef6d1d111aae to your computer and use it in GitHub Desktop.
Save arturo182/1eacf225967c690bd6feef6d1d111aae to your computer and use it in GitHub Desktop.
// config
var itemsPerPage = 25;
var filterZeroStock = true; // May screw with pagination and items per page
var filterExpanded = false; // Same as above
var sortBy = 'price'; // 'price' or 'stock'
// end of config
scope = angular.element(document).scope();
child = scope.$$childHead;
while (!child.hasOwnProperty('smtComponentList'))
child = child.$$nextSibling;
child.formPage.pageRows = itemsPerPage;
function sort_and_filter()
{
if (child.selectSmtComponentListLoading) {
setTimeout(sort_and_filter, 50);
return;
}
if (child.smtComponentList.length == 0)
return;
data = child.smtComponentList;
child.smtComponentList = data
.filter(function(el)
{
if (filterZeroStock && el.stockCount == 0)
return false;
if (filterExpanded && el.componentLibraryType == 'expand')
return false;
return true;
}).sort(function(a, b)
{
if (sortBy == 'price') {
return a.componentPrices[0].productPrice < b.componentPrices[0].productPrice ? -1 : 1;
} else if (sortBy == 'stock') {
return a.stockCount < b.stockCount ? 1 : -1;
}
return 0;
});
if (child.$$phase != '$apply')
child.$apply();
}
if (child.orig_enterSearch == null)
child.orig_enterSearch = child.enterSearch
child.enterSearch = function(event)
{
child.orig_enterSearch(event);
sort_and_filter();
}
if (child.orig_clickSearch == null)
child.orig_clickSearch = child.clickSearch
child.clickSearch = function(event)
{
child.orig_clickSearch(event);
sort_and_filter();
}
if (child.orig_page == null)
child.orig_page = child.page
child.page = function(event)
{
child.orig_page(event);
sort_and_filter();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment