简体   繁体   English

向Isotope.JS搜索过滤器添加“错误”消息?

[英]Adding an 'error' message to an Isotope.JS Search filter?

I am trying to add a message I can display if someone's search comes up empty. 我正在尝试添加一条消息,如果某人的搜索为空时可以显示。 Something along the lines of "There doesn't seem to be anything here." 与“这里似乎什么都没有”类似。

I'm using Isotope.JS to handle my filtering, and was trying to use a JS If/Else statement based on length to display this error message. 我正在使用Isotope.JS来处理我的过滤,并试图根据长度使用JS If / Else语句来显示此错误消息。 I can't seem to get it to work because the console says there are [0] objects being displayed all the time, even when not being searched. 我似乎无法使其正常工作,因为控制台说一直显示[0]对象,即使没有被搜索也是如此。

My JS function for the error message 我的JS函数的错误消息

var exampleCount = $('.examples-container .example').length;    
    //shows empty state text when no jobs found
    if(exampleCount == '0') {
      $('.examples-container').addClass('empty');
    }
    else {
      $('.examples-container').removeClass('empty');
    }

The classes being added and removed 被添加和删除的类

.empty-item {
  transition-property: opacity;
  transition-duration: 0s;
  transition-delay: 0s;
  transition-timing-function: ease;
  display:none;
}

.empty .empty-item {
  transition-property: opacity;
  transition-duration: .2s;
  transition-delay: .3s;
  transition-timing-function: ease;
   display:block !important;
}

And my div to be hidden/displayed 和我的div被隐藏/显示

<span class="empty-item">no results</span>  

You can see an example at this codepen . 您可以在此Codepen中看到一个示例。 It seems to not want to append my class because it can't get the length of my examples in examples-container . 似乎不想追加我的类,因为它无法在examples-container获得我的示例的长度。

Alright, so I came across my answer. 好吧,所以我碰到了我的答案。 Basically, I needed to have my if/else at the end of my keyup or search function . 基本上,我需要在keyupsearch函数的末尾添加if / else。 This is what it looks like: 看起来是这样的:

// use value of search field to filter
  var $quicksearch = $('#quicksearch').keyup( debounce( function() {
    qsRegex = new RegExp( $quicksearch.val(), 'gi' );
    $container.isotope();

// display message box if no filtered items
if ( !$container.data('isotope').filteredItems.length ) {
  $('#noResult').show();
} else {
  $('#noResult').hide();
}

  }) );

Then I swapped my HTML to make an id to be easier to manage, so I used this for my 'no results' div . 然后,我交换了HTML以使id易于管理,因此将其用于“无结果” div

<div id="noResult">
   <div>No results</div>
</div>

And here is my final result . 这是我的最终结果

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

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