Advanced Angular filters (Ranges, less than, greater than)

341 views Asked by At

So I'm trying to make a filter with angular and ng-table so that I can search ranges (ex: 5-10 offsets), <, >, >=, <=, !=. What is the best approach to doing this? I may be overcomplicating this because I'm using a service to cache, order, filter data so I don't have $scope access (at least from what I understand, I could just return the promise back to the controller's scope).

In my service:

function transformData(data, params) {
            var orderedData = params.sorting() ?
                $filter('orderBy')(data, params.orderBy()) :
                data;

            for (var propt in params.filter()) {
                if (params.filter().hasOwnProperty(propt) && params.filter()[propt] === '') {
                    delete params.filter()[propt];
                }
            }

            return params.filter() ?
                $filter('filter')(orderedData, customFilter(params.filter()), function (actual, expected) {
                    if (!isNaN(expected)) {
                        return (actual == expected)
                    }
                    $log.info('actual: ', actual, ' expected: ', expected);
                    return angular.lowercase(actual.toString()).indexOf(angular.lowercase(expected.toString())) > -1;

                }) : orderedData;
        }

What I'm having trouble with that I made:

function customFilter(filter) {
            $log.debug('DS Filter:', filter);

            if (filter['vacantLessThan']) { // This will be a dropdown, or something..
                // Something..
            } else if (filter['vacantLessThanEq']) {
                // Something..
            } else if (filter['vacantGreaterThan']) {
                // Something..
            } else if (filter['vacantGreaterThanEq']) {
                // Something..
            }

            var keys = Object.keys(filter);

            return function (item) {
                var isEqual = true;
                angular.forEach(keys, function (key) {
                    $log.debug(filter[key], ' vs ', item[key]);
                    if(!isNaN(filter[key])){
                        isEqual = !(item[key] == filter[key]) ? false : isEqual;
                    } else {
                        isEqual = !(angular.lowercase(item[key].toString()).indexOf(angular.lowercase(filter[key].toString())) > -1) ? false : isEqual;
                    }
                });

                return isEqual;
            }
        }

Should I just scrap this and use ng-hide?

0

There are 0 answers