angularjs过滤器嵌套数组使用checkboxes-with-angularjs

时间:2015-08-21 03:00:18

标签: angularjs filter

我正在关注this approach来过滤嵌套的json响应。我有一个这样的嵌套属性:

instances:{
     instance:[
     {cname:'name1', location:'pa', price:40, model:'2014' },     
     {cname:'name1', location:'ga', price:30 , model:'2014'},
     {cname:'name1', location:'ga', price:20, model:'2010' }        
    ]}

我可以使用上面提到的示例过滤顶级属性,但不能使用子属性。 我已修改上面的示例,以显示我的json的嵌套属性。http://jsfiddle.net/jackAndy/qygL2m01/4/。我是angularjs的新手。

1 个答案:

答案 0 :(得分:1)

  1. 首先 - 您使用function Search() { if (checkInputSearch() == "true") { var date = ""; var searchid = document.getElementById("bookid").value; $.ajax({ type : "POST", url : "searchbook", data : { searchid : searchid }, success : function(html) { var res = html.split(";"); if (res[0] == "null") { alert(MSG0000 + " " + bookid.value + " " + MSG0004); Clear(); } else { alert(MSG0003); document.getElementById("bookid").value = res[0]; document.getElementById("booktitle").value = res[1]; document.getElementById("authorname").value = res[2]; document.getElementById("publishher").value = res[3]; date = res[4].split("-"); document.getElementById("day").value = date[2]; document.getElementById("month").value = date[1]; document.getElementById("year").value = date[0]; } }, error : function(e) { alert(MSG0005); console.log("Error:" + e); } }); } } 的原因?它主要不是使用instances.instance;
  2. 数据加载后仅使用players.instances = []个函数一次;观察过滤器 - 在这种情况下没有必要;
  3. 获取过滤器值的函数(我使用Group underscore函数,您可以使用自己的算法):

    uniq

    过滤$scope.getFieldsValues = function(field){ var result = []; for(var i = 0; i < $scope.players.length; i++){ result.push($scope.players[i][field]); } return _.uniq(result); };

    players

    模板:

    $scope.testFl = function(el){
        for(var filter in $scope.filters){
            var filterArray = [];
            for(var i in $scope.filters[filter]){
                if($scope.filters[filter][i]) filterArray.push(i);
            }
    
            //You can make array with instances properties & compare with it;
            if(filter === 'location'){
                if(el.instances && el.instances.length > 0){
                    var intersection = el.instances.filter(function(n) {
                        return filterArray.indexOf(n[filter]) != -1
                    });
                } else if(filterArray.length > 0){return false;}
            } else {
                if(filterArray.length > 0 && filterArray.indexOf(el[filter]) === -1) return false;
            }
    
        }
        return true;
    };
    

    过滤实例:

    <li ng-repeat="player in players | filter:testFl" >
    

    模板:

    $scope.testFl2 = function(el){
        var filterArray = [];
        for(var i in $scope.filters.location){
            if($scope.filters.location[i]) filterArray.push(i);
        }
        return filterArray.length > 0 && filterArray.indexOf(el.location) === -1 ? false : true;
    };
    

    Fiddle for this;

    <强>更新
    计数功能:

    <span ng-repeat="loc in player.instances | filter:testFl2" >
    

    Update fiddle - 更新下划线,添加计数功能;

    我希望这会对你有所帮助;

    使用答案:
    Add underscore to jsfiddle;
    variable property name in where underscore.js;