Angularjs中json数组的复选框过滤器

时间:2015-04-28 11:27:45

标签: angularjs

我已经创建了一个过滤器,但是这个过滤器不能用于数组里面的数组。

'http://plnkr.co/edit/oygy79j3xyoGJmiPHm4g?p=info'

以上plkr链接正在运行演示。

app.filter('checkboxFilter', function($parse) {
    var cache = { //create an cache in the closure
        result: [],
        checkboxData: {}
    };

    function prepareGroups(checkboxData) {
        var groupedSelections = {};
        Object.keys(checkboxData).forEach(function(prop) {

            //console.log(prop);

            if (!checkboxData[prop]) {
                return;
            } //no need to create a function

            var ar = prop.split('=');

            //console.log("ar is - "+ar);
            if (ar[1] === 'true') {
                ar[1] = true;
            } //catch booleans
            if (ar[1] === 'false') {
                ar[1] = false;
            } //catch booleans

            /* replacing 0 with true for show all offers */
            if(ar[0]=='SplOfferAvailable.text'){
                ar[1]='true';
            }else{

            }

            //make sure the selection is there!
            groupedSelections[ar[0]] = groupedSelections[ar[0]] || [];
            //at the value to the group.

            groupedSelections[ar[0]].push(ar[1]);
        });
        return groupedSelections;
    }

    function prepareChecks(checkboxData) {

        var groupedSelections = prepareGroups(checkboxData);

        var checks = [];

        //console.log(groupedSelections);
        Object.keys(groupedSelections).forEach(function(group) {
            //console.log("groupedSelections- "+groupedSelections);
            //console.log("group- "+group);
            var needToInclude = function(item) {
                //console.log("item- "+item);
                // use the angular parser to get the data for the comparson out.
                var itemValue = $parse(group)(item);

                var valueArr = groupedSelections[group];
                //console.log("valueArr- "+valueArr);

                function checkValue(value) { //helper function
                    return value == itemValue;
                }
                //check if one of the values is included.
                return valueArr.some(checkValue);
            };
            checks.push(needToInclude); //store the function for later use
        });
        return checks;
    }

    return function(input, checkboxData, purgeCache) {

        if (!purgeCache) { //can I return a previous 'run'?
            // is the request the same as before, and is there an result already?
            if (angular.equals(checkboxData, cache.checkboxData) && cache.result.length) {
                return cache.result; //Done!
            }
        }
        cache.checkboxData = angular.copy(checkboxData);


        var result = []; // this holds the results


        //prepare the checking functions just once.
        var checks = prepareChecks(checkboxData);



        input.every(function(item) {
            if (checks.every(function(check) {
                    return check(item);

                })) {
                result.push(item);
            }
            return result.length < 10000000; //max out at 100 results!
        });

        cache.result = result; //store in chache
        return result;
    };
});

上面的代码用于复选框过滤器。

当我点击名为&#34的复选框时;可用性&#34;它不会过滤结果。

请帮帮我。

感谢。

1 个答案:

答案 0 :(得分:0)

我认为你浏览json的方式是错误的,因为如果你这样做就可以了#

"Location": "Riyadh",
"AvlStatus": "AVAILABLE"
"Rooms": {.....

你必须以某种方式通过房间,现在我认为你没有这样做