【问题标题】:Filtering with Integer field in AngularJS在 AngularJS 中使用整数字段进行过滤
【发布时间】:2015-06-03 10:29:44
【问题描述】:

是否甚至可以使用整数进行过滤。我有一个列表,其中搜索工作正常,除了一个字段,即整数。我试图在 JsFiddle 中做几乎相同的情况,但我什至无法让 ng-repeat 工作:)

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>
        <body ng-app="">
        <div ng-init="rmas = [
      {
        brand: "TIK",
        extraInfo: "lisäinfoa",
        isWarranty: false,
        litm: "T00000245",
        lotn: "00004",
        model: "A10499",
        retailer: 40001,
        retailerName: "mr Retailer",
        rmaId: 1},
      {
        brand: "SAK",
        extraInfo: "LISÄÄÄ",
        isWarranty: false,
        litm: "S2181105",
        lotn: "003500",
        model: "A30340",
        retailer: 40011,
        retailerName: "MALL SAM",
        rmaId: 2
      },
      {
        brand: "TIK",
        extraInfo: "LISÄÄÄ",
        isWarranty: false,
        litm: "T00000245",
        lotn: "00004",
        model: "A10499",
        retailer: 40000,
        retailerName: "ULF SVENSSON",
        rmaId: 3
      }
    ]"></div>
       <div ng-init="friends = [{name:'John', phone:'555-1276'},
                             {name:'Mary', phone:'800-BIG-MARY'},
                             {name:'Mike', phone:'555-4321'},
                             {name:'Adam', phone:'555-5678'},
                             {name:'Julie', phone:'555-8765'},
                             {name:'Juliette', phone:'555-5678'}]"></div>
        
       
    
    <label>Search: <input ng-model="searchText"></label>
    <table id="searchTextResults">
      <tr><th>rmaId </th>  <th> retailerName </th><th> litm</th></tr>
      <tr ng-repeat="rma in rmas | filter:searchText"">
        <td>{{rma.rmaId}}</td>
        <td>{{rma.retailerName}}</td>
        <td>{{rma.litm}}</td>
      </tr>
    </table>
        <table id="searchTextResults">
      <tr><th>Name</th><th>Phone</th></tr>
      <tr ng-repeat="friend in friends | filter:searchText">
        <td>{{friend.name}}</td>
        <td>{{friend.phone}}</td>
      </tr>
    </table>
    <hr>
    
    </body>

http://jsfiddle.net/ demo

【问题讨论】:

  • 为什么不将数据放入控制器中?

标签: javascript angularjs ng-repeat angularjs-filter


【解决方案1】:

所以我纠正了你的一些错误并将你的数据放入控制器中,这里是working codepen

我认为你的错误是你在 ng-Init 中使用 " 只是使用这样的单引号:

 <div ng-init="rmas = [
  {
    brand: 'TIK',
    extraInfo: 'lisäinfoa',
    isWarranty: false,
    litm: 'T00000245',
    lotn: '00004',
    model: 'A10499',
    retailer: '40001',
    retailerName: 'mr Retailer',
    rmaId: 1},

]"></div>

【讨论】:

  • 我只是从 chrome devtool 复制粘贴了那个 json,这就是为什么那些“。我仍然想知道为什么在我的现实应用程序中我不能用那个 rmaId 过滤。它与你的演示基本相同,但是json 要复杂得多。
  • 非常感谢。我必须实现搜索字段,因为基本上每个 json 中都有数字。 ng-model="searchKeyword.rmaId" 是解决这个问题的方法。我想知道是否有任何解决方案可以先用 rmaID 过滤,然后再用其他过滤?如果只有一个搜索字段,而不是两个,那将是更酷的方式。点赞过滤器:searchKeyword.rmaId |过滤器:搜索关键字“
  • 我不明白,什么意思?
  • 我的意思是当用户用“any”词组搜索时,他基本上不能用rmaID搜索,因为jsons里面有很多数字。如果他用数字 2 搜索,结果就是一切。我修改了你的演示:codepen.io/anon/pen/dovzjY 所以问题是你能在一个输入字段中获得两个过滤器吗?我不喜欢为此填写字段。
  • 我知道你现在需要什么了 :)。嗯,我认为在 1 个输入中执行 2 个过滤器是个坏主意,因为用户不会理解过滤器的工作原理。示例:如果用户键入 2,这将显示“MALLSAM”,但他继续键入 2,它将显示“mr Retailer”,在他的脑海中他会感到困惑:)。我认为最好是做一个选择选项,它允许用户按 id 或按名称选择。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-12
  • 1970-01-01
  • 1970-01-01
  • 2016-12-14
  • 2019-10-07
  • 1970-01-01
相关资源
最近更新 更多