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

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

对象:

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

Javascript(咖啡脚本):

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

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>

过滤代码(js/coffee)

    .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

所以我在这里想要的是只过滤显示的元素(id、类别、名称和颜色),但是由于某种原因,当我在输入中键入 25 时,对象仍然显示,因为他的 $$haskKey .

感谢您添加过滤器代码。 解决方案应该像在搜索匹配项时明确忽略 $$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)

我添加了一些小的重构:

  • 我将顶级 if 语句更改为保护子句,这减少了代码的缩进级别
  • 将短 doesMatch 函数更改为单行
  • 在条件语句中使用 andisisnt

主要更改是跳过任何 key 等于 $$hashkey

的属性

这是未经测试的,所以我希望它对你有用。