1. 程式人生 > >smart-table 數值篩選

smart-table 數值篩選

HTML
1
<st-number-range predicate="balance" lower="balance.lower" higher="balance.higher"></st-number-range>
 配置
1
app.directive('stNumberRange', ['$timeout', function ($timeout) { 2 return { 3 restrict: 'E', 4 require: '^stTable', 5
scope: { 6 lower: '=', 7 higher: '=' 8 }, 9 templateUrl: 'stNumberRange.html', 10 link: function (scope, element, attr, table) { 11 console.log(element) 12 var
inputs = element.find('input'); 13 var inputLower = angular.element(inputs[0]); 14 var inputHigher = angular.element(inputs[1]); 15 var predicateName = attr.predicate; 16 [inputLower, inputHigher].forEach(function (input, index) {
17 18 //紅色為官方示例錯誤 19 input.bind('input', function () { 20 var query = {}; 21 22 if (scope.lower) { 23 query.lower = scope.lower; 24 } 25 26 if (scope.higher) { 27 query.higher = scope.higher; 28 } 29 30 scope.$apply(function () { 31 table.search(query, predicateName) 32 }); 33 }); 34 }); 35 } 36 }; 37 }])
app.filter('customFilter', ['$filter', function($filter) {
            var filterFilter = $filter('filter');
            var standardComparator = function standardComparator(obj, text) {
                text = ('' + text).toLowerCase();
                return ('' + obj).toLowerCase().indexOf(text) > -1;
            };

            return function customFilter(array, expression) {
                function customComparator(actual, expected) {

                    var isBeforeActivated = expected.before;
                    var isAfterActivated = expected.after;
                    var isLower = expected.lower;
                    var isHigher = expected.higher;
                    var higherLimit;
                    var lowerLimit;

                    if (angular.isObject(expected)) {

                        if (isLower || isHigher) {
                            //number range
                            if (isLower) {
                                higherLimit = expected.lower;
                   //紅色官方示例錯誤
                                if (actual < higherLimit) {
                                    return false;
                                }
                            }

                            if (isHigher) {
                                lowerLimit = expected.higher;
                   //紅色官方示例錯誤 
if (actual > lowerLimit) { return false; } } return true; } //etc return true; } return standardComparator(actual, expected); } var output = filterFilter(array, expression, customComparator); return output; }; }]);