【问题标题】:how to filter by username?如何按用户名过滤?
【发布时间】:2016-03-07 12:14:47
【问题描述】:

我有一个 Json 对象。

       [{
"id":"1",
"username": "vishnu",
"FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "fixing bugs",
"time": "1"
  }, {

"id":"2",
"username": "seenu",
"FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "working on ui",
"time": "2"
   }, {
"id":"3",
"username": "sam",
"FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "bigiron",
"task": "working on api",
"time": "5"
 },
    {
"id":"4",
"username": "vishnu",
"FromDate": "Wed Mar 03 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "timetracker",
"task": "ui designing",
"time": "1"
   },{
"id":"5",
"username": "arun",
"FromDate": "Wed Jan 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "fixing bugs",
"time": "1"
  }, {
"id":"6",
"username": "seenu",
"FromDate": "Wed Mar 01 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "working on ui",
"time": "2"
    }, {
"id":"7",
"username": "sam",
"FromDate": "Wed Mar 03 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "working on api",
"time": "6"
 },
   {
"id":"8",
"username": "vishnu",
"FromDate": "Wed Mar 03 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "timetracker",
"task": "ui designing",
"time": "4"
    }]

我想制作一个自定义过滤器来过滤特定用户(根据用户名),并从过滤后的数据中获取长度。 例如,用户名:Vishnu,我想过滤并检查用户名 Vishnu 有多少对象。像这样,我想为所有用户获取。

我创建了一个自定义过滤器,例如(键入的代码,不正确),

   app.filter('myfilter',function(){
 return function(mydata){
 var filterd data=[];
angular.foreach(mydata,function(s){
 if(s.name==$scope.model)
 {
filtereddata.push(s)
  }
 })
 return filtereddata;
}
  });

现在我该怎么办?

【问题讨论】:

标签: javascript angularjs ionic-framework


【解决方案1】:

使用

s.name.indexOf($scope.model)>-1 

代替

s.name==$scope.model  

用于类似搜索或包含搜索

【讨论】:

  • 你在说OP是谁?
  • 我在有问题的上下文中编写我的代码,对于每个循环都没有问题,但在使用循环之前您必须知道结束限制
  • 您必须在提供的代码中突出显示错误,然后提供您的解决方案/建议..
  • 我提到你必须在你的状态中使用 indexOf 而不是 ==
【解决方案2】:

您可以返回filtereddata.length而不是filtereddata,对吗?

【讨论】:

    【解决方案3】:

    根据评论创建的示例

    HTML

    <body ng-app="myApp">
        <div ng-controller="Ctrl">Search:
            <input ng-model="searchText">
            <div ng-repeat="(k,v) in items | myFilter:searchText">
                {{v}}
             </div>
        </div>
    </body>
    

    JS

    var myApp = angular.module('myApp', []);
    
    myApp.controller('Ctrl', function($scope) {
    
      $scope.items = [{
        "id": "1",
        "username": "vishnu",
        "FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
        "selectedProject": "createwhimsy",
        "task": "fixing bugs",
        "time": "1"
      }, {
    
        "id": "2",
        "username": "seenu",
        "FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
        "selectedProject": "createwhimsy",
        "task": "working on ui",
        "time": "2"
      }, {
        "id": "3",
        "username": "sam",
        "FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
        "selectedProject": "bigiron",
        "task": "working on api",
        "time": "5"
      }, {
        "id": "4",
        "username": "vishnu",
        "FromDate": "Wed Mar 03 2016 10:56:45 GMT+0530 (India Standard Time)",
        "selectedProject": "timetracker",
        "task": "ui designing",
        "time": "1"
      }, {
        "id": "5",
        "username": "arun",
        "FromDate": "Wed Jan 02 2016 10:56:45 GMT+0530 (India Standard Time)",
        "selectedProject": "createwhimsy",
        "task": "fixing bugs",
        "time": "1"
      }, {
        "id": "6",
        "username": "seenu",
        "FromDate": "Wed Mar 01 2016 10:56:45 GMT+0530 (India Standard Time)",
        "selectedProject": "createwhimsy",
        "task": "working on ui",
        "time": "2"
      }, {
        "id": "7",
        "username": "sam",
        "FromDate": "Wed Mar 03 2016 10:56:45 GMT+0530 (India Standard Time)",
        "selectedProject": "createwhimsy",
        "task": "working on api",
        "time": "6"
      }, {
        "id": "8",
        "username": "vishnu",
        "FromDate": "Wed Mar 03 2016 10:56:45 GMT+0530 (India Standard Time)",
        "selectedProject": "timetracker",
        "task": "ui designing",
        "time": "4"
      }]
    });
    
    
    myApp.filter('myFilter', function() {
      return function(items, search) {
        var result = [];
        angular.forEach(items, function(value, key) {
          angular.forEach(value, function(value2, key2) {
            if (value2 === search) {
              result.push(value2);
            }
          })
        });
        return result;
    
      }
    });
    

    工作Jsfiddle演示

    【讨论】:

      【解决方案4】:

      您可以通过这种方式简单地使用自定义过滤器:过滤器名称加上过滤器后缀

      然后在控制器中简单地使用它:

      function myCtrl($scope, filterFilter) { 
        var filteredRes = filterFilter(data, searchQuery);
      }
      

      现在filteredRes.length 为您提供匹配搜索查询的结果长度。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-23
        • 2018-05-10
        • 1970-01-01
        • 2018-09-03
        相关资源
        最近更新 更多