Feature: Searching and sorting in product works perfectly. Will work now to refactor it to make it usable.
This commit is contained in:
parent
b1aa130421
commit
811ec89c62
|
@ -1,12 +1,11 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var ProductListCtrl = ['$scope', '$location', '$routeParams', 'products', function ($scope, $location, $routeParams, products) {
|
var ProductListCtrl = ['$scope', '$location', '$routeParams', '$filter', 'products', function ($scope, $location, $routeParams, $filter, products) {
|
||||||
$scope.search = $routeParams.q || '';
|
$scope.search = $routeParams.q || '';
|
||||||
$scope.show_extended = false;
|
$scope.show_extended = false;
|
||||||
$scope.info = products;
|
$scope.info = products;
|
||||||
|
|
||||||
$scope.isTrue = function (value) {
|
$scope.isTrue = function (value) {
|
||||||
value = value.toLowerCase();
|
|
||||||
return !_.any(['f', 'fa', 'fal', 'fals', 'false', 'n', 'no', '0'], function (item) {
|
return !_.any(['f', 'fa', 'fal', 'fals', 'false', 'n', 'no', '0'], function (item) {
|
||||||
return item === value;
|
return item === value;
|
||||||
});
|
});
|
||||||
|
@ -16,29 +15,56 @@ var ProductListCtrl = ['$scope', '$location', '$routeParams', 'products', functi
|
||||||
$scope.filterProducts(newValue);
|
$scope.filterProducts(newValue);
|
||||||
}, true);
|
}, true);
|
||||||
|
|
||||||
$scope.parseFilterString = function (q, defaultKey) {
|
$scope.comparator = {
|
||||||
var re = /((([^ ]+):\s*('[^':]+'|"[^":]+"|[^ ]+))|[^ ]+[^: '"]*)/g;
|
'def': 'n',
|
||||||
var matches = [];
|
'n': {'Col': 'Name', 'Comparator': function (obj, text) {
|
||||||
if (angular.isUndefined(defaultKey)) {
|
return obj.toLowerCase().indexOf(text) > -1;
|
||||||
defaultKey = 'n';
|
}},
|
||||||
}
|
'a': {'Col': 'IsActive', 'Comparator': function (obj, text) {
|
||||||
|
return obj === $scope.isTrue(text);
|
||||||
|
}},
|
||||||
|
'u': {'Col': 'Units', 'Comparator': function (obj, text) {
|
||||||
|
return obj.toLowerCase().indexOf(text) > -1;
|
||||||
|
}},
|
||||||
|
't': {'Col': 'ProductGroup', 'Comparator': function (obj, text) {
|
||||||
|
return obj.toLowerCase().indexOf(text) > -1;
|
||||||
|
}}
|
||||||
|
};
|
||||||
|
$scope.sorter = {
|
||||||
|
'c': 'Code',
|
||||||
|
'n': 'Name',
|
||||||
|
'u': 'Units',
|
||||||
|
'p': 'Price',
|
||||||
|
't': 'ProductGroup',
|
||||||
|
'a': 'IsActive'
|
||||||
|
};
|
||||||
|
$scope.parseFilterString = function (q, defaultKey, comparator, sorter) {
|
||||||
|
var re = /((([^ ]+):\s*('[^':]+'|"[^":]+"|[^ ]+))|[^ ]+[^: '"]*)/g,
|
||||||
|
matches = [],
|
||||||
|
sorting = [];
|
||||||
|
|
||||||
|
defaultKey = comparator[defaultKey || comparator.def];
|
||||||
var m = q.match(re);
|
var m = q.match(re);
|
||||||
_.forEach(m, function (item) {
|
_.forEach(m, function (item) {
|
||||||
item = item.toLowerCase();
|
item = item.trim();
|
||||||
if (item.indexOf(':') === -1) {
|
if (item.indexOf(':') === -1) {
|
||||||
matches.push({'key': defaultKey, 'value': item});
|
if ((item.length > 1) && (item.charAt(0) === '+' || item.charAt(0) === '-') && (item.substr(1) in sorter)) {
|
||||||
|
sorting.push(item.charAt(0) + sorter[item.substr(1)]);
|
||||||
|
} else if (item.length > 2) {
|
||||||
|
matches.push({'Col': defaultKey.Col, 'Comparator': defaultKey.Comparator, 'Value': item});
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
var key = item.substr(0, item.indexOf(':')).toLowerCase();
|
var key = item.substr(0, item.indexOf(':')).toLowerCase();
|
||||||
var value = item.substr(item.indexOf(':') + 1, item.length).trim().toLowerCase();
|
var value = item.substr(item.indexOf(':') + 1, item.length).trim().toLowerCase();
|
||||||
if (value.indexOf("'") !== 0 || value.indexOf('"') !== 0) {
|
if (value.indexOf("'") !== -1 || value.indexOf('"') !== -1) {
|
||||||
value = value.substring(1, value.length - 2).trim();
|
value = value.substring(1, value.length - 1).trim();
|
||||||
}
|
}
|
||||||
if (key !== '' && value !== '') {
|
if (key !== '' && value !== '' && key in comparator) {
|
||||||
matches.push({'key': key, 'value': value});
|
matches.push({'Col': comparator.Col, 'Comparator': comparator[key], 'Value': item});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return matches;
|
return {'q': matches, 'o': sorting};
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.filterProducts = _.debounce(function (q) {
|
$scope.filterProducts = _.debounce(function (q) {
|
||||||
|
@ -50,42 +76,26 @@ var ProductListCtrl = ['$scope', '$location', '$routeParams', 'products', functi
|
||||||
$location.path('/Products').search({'q': q}).replace();
|
$location.path('/Products').search({'q': q}).replace();
|
||||||
}
|
}
|
||||||
$scope.$apply(function () {
|
$scope.$apply(function () {
|
||||||
var matches = $scope.parseFilterString(q);
|
var matches = $scope.parseFilterString(q, 'n', $scope.comparator, $scope.sorter);
|
||||||
$scope.show_extended = _.any(matches, function (item) {
|
$scope.show_extended = _.any(matches.q, function (item) {
|
||||||
return ((item.key === 'e' || item.key === 'ext') && $scope.isTrue(item.value));
|
return ((item.key === 'e' || item.key === 'ext') && $scope.isTrue(item.value));
|
||||||
});
|
});
|
||||||
$scope.products = $scope.doFilter(q, matches);
|
|
||||||
|
$scope.products = $scope.doFilter(q, $scope.info, matches.q, matches.o);
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, 350);
|
}, 350);
|
||||||
$scope.doFilter = _.memoize(function (q, matches) {
|
$scope.doFilter = _.memoize(function (q, array, filterExpressions, sortPredicates) {
|
||||||
var i, len = matches.length;
|
var filterCount = filterExpressions.length;
|
||||||
if (len <= 0) {
|
var arrayCopy = filterCount === 0 ? array : _.filter($scope.info, function (item) {
|
||||||
return $scope.info;
|
return _.every(filterExpressions, function (expression) {
|
||||||
}
|
return expression.Comparator(item[expression.Col], expression.Value);
|
||||||
return _.filter($scope.info, function (item) {
|
});
|
||||||
for (i = 0; i < len; i++) {
|
|
||||||
var match = matches[i];
|
|
||||||
if (match.key === 'n') {
|
|
||||||
if (item.Name.toLowerCase().indexOf(match.value) === -1) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else if (match.key === 'a') {
|
|
||||||
if (item.IsActive !== $scope.isTrue(match.value)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else if (match.key === 'u') {
|
|
||||||
if (item.Units.toLowerCase().indexOf(match.value) === -1) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else if (match.key === 'g') {
|
|
||||||
if (item.ProductGroup.toLowerCase().indexOf(match.value) === -1) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
});
|
});
|
||||||
|
arrayCopy = sortPredicates.length === 0 ? arrayCopy : $filter('orderBy')(arrayCopy, sortPredicates);
|
||||||
|
return arrayCopy;
|
||||||
|
|
||||||
});
|
});
|
||||||
}];
|
}];
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue