简体   繁体   English

如果过滤器返回空的AngularJS,如何隐藏元素

[英]How to hide element if filter returns null angularjs

I've got a list of products which I loop through. 我有一个循环浏览的产品清单。 The list should be sorted alphabetically, and each first new letter had the capital first letter at the top, which I got working. 该列表应按字母顺序排序,每个第一个新字母的顶部都有大写第一个字母,这是我正在使用的。

<div ng-repeat="product in data.products | orderBy : 'naam' : false | filter:searchText:strict track by $index  ">
  <li class="tooltipproduct" data-tooltip-content="1">
    //THIS LINE HERE HAS A FILTER ON IT IF ITS THE FIRST ITEM OF THIS ALPHABET LETTER
    <span style="color:#eb9600;font-size:25px;font-weight:800;">{{ product.naam | firstLetterFilter }}</span><br>
    <span class="tooltip1" data-tooltip-content="#tooltip_content{{$index + 1}}" style="cursor:pointer;">{{ product.naam }}</span><br>
  </li>
  <div class="tooltip_templates" style="background-color:#eb9600;">
    <span id="tooltip_content{{$index + 1}}" style="min-height:180px;!important">
      <h2 style="font-size: 20px;color:#fff;font-weight:bold;">{{product.naam}}</h2>
      <p style="max-width:200px;">{{ product.omschr_kort }}</p>
      <span>Meer informatie nodig <br> of snel een scherpe offerte?</span>
      <br>
      <a style="position:absolute;bottom:20px;font-weight:bold;color:#fff;background-color:#c17c02;padding:10px;border-radius:3px;" href="contact.php">Vraag offerte aan</a>
      <img style="max-width:90px;position:absolute;bottom:0px;right:12px;" src="images/contact_jess.png" />
    </span>
  </div>
</div>

Here's the filter: 这是过滤器:

.filter('firstLetterFilter', function () {
  var firstLetters = [];
  return function (item) {
    if(item !== undefined) {
      var firstLetter = item.charAt(0);
      if(firstLetters.indexOf(firstLetter) === -1) {
        firstLetters.push(firstLetter);
        //console.log(firstLetter);
        console.log(firstLetters);
        return firstLetter;
      }
    }
  };
});

What happens is the span gets created every time, but when it's not the first item with that letter it remains empty. 发生的情况是每次都会创建跨度,但是当它不是带有该字母的第一个项目时,它将保持空白。 I tried putting a ng-if on it with the ng-if="product.naam | firstLetterFilter" filter on it but this returns true even if it's empty. 我尝试在上面放置ng-if并带有ng-if="product.naam | firstLetterFilter"过滤器,但是即使它为空,它也会返回true。

Does someone know how I can hide the item if the filtered item returns nothing? 如果过滤后的项目什么也没返回,有人知道我如何隐藏该项目?

Just return false from the filter, so that it will hide the element. 只需从过滤器返回false,即可隐藏该元素。

Return false, when it's not the first item with that letter. 如果不是该字母的第一项,则返回false。

.filter('firstLetterFilter', function () {
  var firstLetters = [];
  return function (item) {
    if(item !== undefined) {
      var firstLetter = item.charAt(0);
      if(firstLetters.indexOf(firstLetter) === -1) {
        firstLetters.push(firstLetter);
        //console.log(firstLetter);
        console.log(firstLetters);
        return firstLetter;
      }
      else
        {
          return false
        }
    }

  };
});

Fixed it like this for future readers HTML: 修复了此问题,以供将来的读者使用HTML:

<div ng-repeat="product in data.products | orderBy : 'naam' : false | filter:{'naam' : searchText} track by $index  ">
  <li class="tooltipproduct" data-tooltip-content="1">
    <span style="color:#eb9600;font-size:25px;font-weight:800;" ng-if="product.naam | firstLetterFilter">{{ product.naam | firstLetterFilterTrue }}<br /></span>
    <span class="tooltip1" data-tooltip-content="#tooltip_content{{$index + 1}}" style="cursor:pointer;">{{ product.naam }}</span><br>
  </li>
  <div class="tooltip_templates" style="background-color:#eb9600;">
    <span id="tooltip_content{{$index + 1}}" style="min-height:180px;!important">
      <h2 style="font-size: 20px;color:#fff;font-weight:bold;">{{product.naam}}</h2>
      <p style="max-width:200px;">{{ product.omschr_kort }}</p>
      <span>Meer informatie nodig <br> of snel een scherpe offerte?</span>
      <br>
      <a style="position:absolute;bottom:20px;font-weight:bold;color:#fff;background-color:#c17c02;padding:10px;border-radius:3px;" href="contact.php">Vraag offerte aan</a>
      <img style="max-width:90px;position:absolute;bottom:0px;right:12px;" src="images/contact_jess.png" />
    </span>
  </div>
</div>

Filters: 筛选条件:

filter('firstLetterFilter', function () {
  var firstLetters = [];
  return function (item) {
    if(item !== undefined) {
      var firstLetter = item.charAt(0);
      if(firstLetters.indexOf(firstLetter) === -1) {
        firstLetters.push(firstLetter);
        //console.log(firstLetter);
        console.log(firstLetters);
        return firstLetter;
      }
    }
  };
})
.filter('firstLetterFilterTrue', function () {
  return function (item) {
    if(item !== undefined) {
      var firstLetter = item.charAt(0);
      return firstLetter;
    }
  };
});

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

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