【问题标题】:Isotope combine filter and search field with and logic同位素将过滤器和搜索字段与逻辑相结合
【发布时间】:2023-03-06 16:26:01
【问题描述】:

我在 codepen 上有一个代码,可用于过滤同位素网格,也可用于在同位素网格中进行搜索。

过滤器由复选框和逻辑组成,因此某些复选框不能同时激活(男性/女性)。不过,这并不重要……

过滤器正在工作。 搜索字段有效(不幸的是,只有当我首先没有激活过滤器时)

第一个愿望: 我想将过滤器和搜索与 AND 逻辑结合起来。 例如,当我在搜索框中插入“martin”时,搜索结果是“gruber martin”、“hofer martin”、“maier martina”。还行吧。 然后当我也激活“女性”时,我会只剩下“maier martina”(search=martin AND filter=female)

但是在这种情况下我的代码不记得搜索只设置了过滤器:-(

第二个愿望:搜索字段应始终有效,而不仅仅是在没有过滤器处于活动状态时。

如果有人可以帮助我,那就太好了。

谢谢..

HTML:

<button id="shuffle">shuffle</button>
<div class="button-group filter-button-group">
  <div class="btn">
    <input name="male" value=".male" id="male" type="checkbox">
    <label for="male">male</label>
  </div>
  <div class="btn">
    <input name="female" value=".female" id="female" type="checkbox">
    <label for="female">female</label>
  </div>
  <div class="btn">
    <input name="de" value=".de" id="de" type="checkbox">
    <label for="de">de</label>
  </div>
  <div class="btn">
    <input name="it" value=".it" id="it" type="checkbox">
    <label for="it">it</label>
  </div>
  <div class="btn">
    <input name="en" value=".en" id="en" type="checkbox">
    <label for="en">en</label>
  </div>
  <div class="btn">
    <input name="has-website" value=".has-website" id="has-website" type="checkbox">
    <label for="has-website">has-website</label>
  </div>
</div>
<input class="quicksearch" placeholder="Search" type="text">
<div class="blog_wrapper isotope_wrapper myclass">
  <div class="posts_group lm_wrapper masonry tiles col-6 isotope" style="position: relative; height: 10615.1px;">
    <div class="post-item isotope-item  de it male has-website" style="position: absolute; left: 0px; top: 0px;">
      <div class="post-photo-wrapper scale-with-grid"> </div>
      <div class="post-desc-wrapper">
        <div class="post-desc">
          <div class="post-head"></div>
          <div class="post-title">
            <h2 class="entry-title" itemprop="headline"><a href="#">gruber martin</a></h2>
          </div>
          <div class="post-excerpt"><span class="excerpt-hellip">Lorem ipsum […]</span></div>
        </div>
      </div>
    </div>
    <div class="post-item isotope-item  de it female" style="position: absolute; left: 179px; top: 0px;">
      <div class="post-photo-wrapper scale-with-grid"> </div>
      <div class="post-desc-wrapper">
        <div class="post-desc">
          <div class="post-head"></div>
          <div class="post-title">
            <h2 class="entry-title" itemprop="headline"><a href="#">maier erna</a></h2>
          </div>
          <div class="post-excerpt"><span class="excerpt-hellip">dolor sit […]</span></div>
        </div>
      </div>
    </div>
    <div class="post-item isotope-item  de it male" style="position: absolute; left: 359px; top: 0px;">
      <div class="post-photo-wrapper scale-with-grid"> </div>
      <div class="post-desc-wrapper">
        <div class="post-desc">
          <div class="post-head"></div>
          <div class="post-title">
            <h2 class="entry-title" itemprop="headline"><a href="#">hofer martin</a></h2>
          </div>
          <div class="post-excerpt"><span class="excerpt-hellip">No text […]</span></div>
        </div>
      </div>
    </div>
    <div class="post-item isotope-item  de it male" style="position: absolute; left: 539px; top: 0px;">
      <div class="post-photo-wrapper scale-with-grid"> </div>
      <div class="post-desc-wrapper">
        <div class="post-desc">
          <div class="post-head"></div>
          <div class="post-title">
            <h2 class="entry-title" itemprop="headline"><a href="#">zwerger josef</a></h2>
          </div>
          <div class="post-excerpt"><span class="excerpt-hellip">Hello […]</span></div>
        </div>
      </div>
    </div>
    <div class="post-item isotope-item  it female" style="position: absolute; left: 719px; top: 0px;">
      <div class="post-photo-wrapper scale-with-grid"> </div>
      <div class="post-desc-wrapper">
        <div class="post-desc">
          <div class="post-head"></div>
          <div class="post-title">
            <h2 class="entry-title" itemprop="headline"><a href="#">gruber susi</a></h2>
          </div>
          <div class="post-excerpt"><span class="excerpt-hellip">bla bla […]</span></div>
        </div>
      </div>
    </div>
    <div class="post-item isotope-item  en female" style="position: absolute; left: 899px; top: 0px;">
      <div class="post-photo-wrapper scale-with-grid"> </div>
      <div class="post-desc-wrapper">
        <div class="post-desc">
          <div class="post-head"></div>
          <div class="post-title">
            <h2 class="entry-title" itemprop="headline"><a href="#">maier martina</a></h2>
          </div>
          <div class="post-excerpt"><span class="excerpt-hellip"> […]</span></div>
        </div>
      </div>
    </div>
    <div class="post-item isotope-item male" style="position: absolute; left: 0px; top: 10435px;">
      <div class="post-photo-wrapper scale-with-grid"> </div>
      <div class="post-desc-wrapper">
        <div class="post-desc">
          <div class="post-head"></div>
          <div class="post-title">
            <h2 class="entry-title" itemprop="headline"><a href="#">maxi john</a></h2>
          </div>
          <div class="post-excerpt"><span class="excerpt-hellip"> […]</span></div>
        </div>
      </div>
    </div>
  </div>
</div>

Javascript:

    $('#male').click(function () {
        $('#female').prop('checked', false);
    });
    $('#female').click(function () {
        $('#male').prop('checked', false);
    });


  var grid=$('.isotope');
  var filter=$('.filter-button-group input');
  var qsRegex;


  grid.isotope({
    itemSelector: '.isotope-item',
      filter: function() {
          var searchResult = qsRegex ? $(this).text().match( qsRegex ) : true;
          return searchResult;
    }
  });



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


  // debounce so filtering doesn't happen every millisecond
    function debounce( fn, threshold ) {
      var timeout;
      return function debounced() {
        if ( timeout ) {
          clearTimeout( timeout );
        }
        function delayed() {
          fn();
          timeout = null;
        }
        timeout = setTimeout( delayed, threshold || 100 );
      }
    }

  $('#shuffle').click(function(){
    grid.isotope('shuffle');
  });


  filter.change(function(){
    var filters = [];
    filter.filter(':checked').each(function(){
      filters.push( this.value );
    });
    // filters = filters.join(', ');    OR
      filters = filters.join('');       //AND
    grid.isotope({ filter: filters });
  });

请看:code on codepen

【问题讨论】:

    标签: javascript jquery grid masonry jquery-isotope


    【解决方案1】:

    我找到了解决办法:

        $('#male').click(function () {
            $('#female').prop('checked', false);
        });
        $('#female').click(function () {
            $('#male').prop('checked', false);
        });
    
    
      var grid=$('.isotope');
      var filter=$('.filter-button-group input');
      var suche=$('.quicksearch');
      var qsRegex;
      var buttonFilter;
    
      grid.isotope({
        itemSelector: '.isotope-item',
          filter: function() {
             // console.log(searchResult);
              var searchResult = qsRegex ? $(this).text().match( qsRegex ) : true;
              var buttonResult = buttonFilter ? $(this).is( buttonFilter ) : true;
    
              return searchResult && buttonResult;
        }
      });
    
    
    
      // use value of search field to filter
      var quicksearch = suche.keyup( debounce( function() {
          qsRegex = new RegExp( quicksearch.val(), 'gi' );
          grid.isotope();
      }, 200 ) );
    
    
      // debounce so filtering doesn't happen every millisecond
        function debounce( fn, threshold ) {
          var timeout;
          return function debounced() {
            if ( timeout ) {
              clearTimeout( timeout );
            }
            function delayed() {
              fn();
              timeout = null;
            }
            timeout = setTimeout( delayed, threshold || 100 );
          };
        }
    
      $('#shuffle').click(function(){
        grid.isotope('shuffle');
      });
    
    
    
      filter.change(function(){
        var filters = [];
    
        filter.filter(':checked').each(function(){
          filters.push( this.value );
        });
         //filters = filters.join(', ');    //OR
        filters = filters.join('');         //AND
        buttonFilter = filters;
        grid.isotope();
      });
    

    See full example on codepen

    【讨论】:

      【解决方案2】:

      与上面的示例相同,但来自 de docs :) https://codepen.io/desandro/pen/mCdbD

      // quick search regex
      var qsRegex;
      var buttonFilter;
      
      // init Isotope
      var $grid = $('.grid').isotope({
        itemSelector: '.element-item',
        layoutMode: 'fitRows',
        filter: function() {
          var $this = $(this);
          var searchResult = qsRegex ? $this.text().match( qsRegex ) : true;
          var buttonResult = buttonFilter ? $this.is( buttonFilter ) : true;
          return searchResult && buttonResult;
        }
      });
      
      $('#filters').on( 'click', 'button', function() {
        buttonFilter = $( this ).attr('data-filter');
        $grid.isotope();
      });
      
      // use value of search field to filter
      var $quicksearch = $('#quicksearch').keyup( debounce( function() {
        qsRegex = new RegExp( $quicksearch.val(), 'gi' );
        $grid.isotope();
      }) );
      
      
        // change is-checked class on buttons
      $('.button-group').each( function( i, buttonGroup ) {
        var $buttonGroup = $( buttonGroup );
        $buttonGroup.on( 'click', 'button', function() {
          $buttonGroup.find('.is-checked').removeClass('is-checked');
          $( this ).addClass('is-checked');
        });
      });
      
      
      // debounce so filtering doesn't happen every millisecond
      function debounce( fn, threshold ) {
        var timeout;
        return function debounced() {
          if ( timeout ) {
            clearTimeout( timeout );
          }
          function delayed() {
            fn();
            timeout = null;
          }
          setTimeout( delayed, threshold || 100 );
        };
      }
      

      【讨论】:

      • 请添加一段简短的文字,解释您的答案的作用,以便该技术的新手有机会理解您的代码并重用它
      • 它结合了两个过滤器。如果您单击 codepen 链接,您会非常清楚地看到:/
      • 不得不依赖可能在某些时候中断的链接是不好的。这就是为什么我认为即使我们删除了代码链接,一个问题仍然应该是完全有效的。
      猜你喜欢
      • 2015-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-03
      相关资源
      最近更新 更多