简体   繁体   English

如何仅过滤显示的元素? (不要过滤$$ hashKey)

[英]How do i filter only the displayed elements? (dont filter the $$hashKey)

Object: 宾语:

    Object
    $$hashKey: "object:25"
    id: 1
    category: "Fruit"
    name: "Apple"
    color: "red"
    __proto__: Object

Javascript (coffescript): Javascript(语言):

    $scope.fruits = [
       {id: 1, category: "Fruit", name: "Apple", color: "red"}
    ]

Html: HTML:

    <input type="search" ng-model="fruitSearch"/>


    <div ng-repeat="i in filtro = (fruits | scFilter:fruitSearch)">
      <div>{{i.id}}</div>
      <div>{{i.category}}</div>
      <div>{{i.name}}</div>
      <div>{{i.color}}</div>
    </div>

Filter code (js/coffee) 过滤器代码(js /咖啡)

    .filter "scFilter", () ->
        (collection, search) ->
            if search
                regexp = createAccentRegexp(search)
                doesMatch = (txt) ->
                    (''+txt).match(regexp)
                collection.filter (el) ->
                    if typeof el == 'object'
                        return true for att, value of el when (typeof value == 'string') && doesMatch(value)
                    doesMatch(el)
                    false
                 else
                     collection

So what i want here is to filter only the displayed elements (id,category,name and color), but for some reason when i type 25 on the input the object still shows up, because of his $$haskKey. 所以我想在这里只过滤显示的元素(id,category,name和color),但是由于某种原因,当我在输入中键入25时,由于他的$$ haskKey,该对象仍然显示出来。

Thank you for adding the filter code. 感谢您添加过滤器代码。 The solution should be as simple as explicitly ignoring the $$hashKey when searching for matches: 解决方案应该简单到在搜索匹配$$hashKey时显式忽略$$hashKey一样简单:

.filter "scFilter", () ->
  (collection, search) ->
    return collection unless search
    regexp = createAccentRegexp(search)
    doesMatch = (txt) -> (''+txt).match(regexp)
    collection.filter (el) ->
      if typeof el == 'object'
        return true for att, value of el when typeof(value) is 'string' and doesMatch(value) and att isnt '$$hashKey'
      else  
        doesMatch(el)

I added some small refactors: 我添加了一些小的重构:

  • I changed the top level if statement to a guard clause, which reduces the level of indentation of the code 我将顶级if语句更改为guard子句,从而减少了代码的缩进级别
  • changed the short doesMatch function to a one liner 将short doesMatch函数更改为一个衬里
  • use and , is and isnt in your conditional statement 在您的条件语句中使用andisisnt

The main change was to skip over any attribute with key equal to $$hashkey 主要更改是跳过key等于$$hashkey任何属性

This is untested, so I hope it works for you. 这未经测试,所以我希望它对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM