Shasha
Shasha

Reputation: 669

How to use angularJs filter on ng-model

I'm having json as below

  var content = [{
  "DocList": [
   {
  "Events": [
    {
      "CategoryName": "PressStatements",
      "DisplayName": "Press Statements",
      "ID": 9
    },
    {
      "CategoryName": "Reports",
      "DisplayName": "Reports",
      "ID": 10
    }
  ],
  "Heading": "News 2",
  "PageID": 23,
   "Date": "\/Date(1454537792000)\/"

},
{
  "Events": [
    {
      "CategoryName": "Research",
      "DisplayName": "Research",
      "ID": 6
    }
  ],
  "Heading": "Heading",
  "PageID": 20,
  "Date": "\/Date(1437417792000)\/"
}
]}
];


$scope.data=content;

 $scope.filterItems = function(g) {
//console.log('filterItems is run');
var ret = $filter('filter')(g.NewsList,"");
//g.filteredItemCount = ret.length
return ret
 };

 $scope.abms = [];


 //Options Inside Select tag
angular.forEach(news[0].NewsList, function(newsItems, index) {
  angular.forEach(newsItems.CategoryList, function(category, index){
    $scope.abms.push(category.DisplayName);
  });
});

i'm displaying Events in select option depending upon that i have to display the data, for selection i'm using select tag with ng-model

<select ng-model="selected"><option ng-repeat="item1 in abms" >{{item1}}</option></select>
   <div id="dataRow{{$index}}" ng-repeat="item1 in (filtered = filterItems(d)) | findobj:selected">

  //display data depending on option selection

  </div>

If i select any categoryName from Events[0] then it should display data from DocList[0] data and same to other for select option.

//filter - dont knw this the way to do it

myapp.filter('findobj', function() {
  return function(dataobj, selected) {
  return dataobj.filter(function(news) {
  //alert('got something');
    return (selected || []).some(function(s) {
    alert('got something');
    return news.CategoryList.CategoryName === s.CategoryList.CategoryName;
    });
   });
 };
});

How to achieve that?

Thanks in Advance...

Upvotes: 0

Views: 322

Answers (1)

Grundy
Grundy

Reputation: 13382

you need fix your findobj filter something like this

myapp.filter('findobj', function () {
    return function (dataobj, selected) {
        if (!selected) return dataobj;//if nothing selected - return initial array
        return dataobj.filter(function (news) {
            //select only news where in category list places selected category
            return news.CategoryList.some(function (category) {
                //check display category name
                return category.DisplayName === selected;
            });
        });
    };
});

working sample on JSFIDDLE

Upvotes: 1

Related Questions