javascript Shuffle.js过滤

insrf1ej  于 2023-10-14  发布在  Java
关注(0)|答案(1)|浏览(81)

我目前正在尝试使用shuffle.js的多过滤器。
我只能让一个过滤器工作,但我不能让多个过滤器工作。
我已经尝试了多个版本,但我看起来它总是过滤数据解决方案。
有没有一种方法可以使它不仅过滤数据解决方案,而且还过滤区域。在这个功能中,我甚至想添加更多的过滤器项目。shuffle.js中已经有一个codepen,但没有真正的解释。我无法让它工作:(
这是我的密码笔Link to codepen

<script src="https://cdnjs.cloudflare.com/ajax/libs/Shuffle/5.2.1/shuffle.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    
    <div id="product_archive">
      <div class="container white_bg">
    <div class="filters_container">
    <div class="btn-group btn-group-toggle" data-toggle="buttons" id="solution-options">
    
            <label class="btn btn-outline-primary">
              <input type="radio" class="input-none" name="shuffle-filter" value="all" checked=""><div class="filter_option_solution"><div class="image_div"></div>All</div>
            </label>
    
            <label class="btn btn-outline-primary">
              <input type="radio" class="input-none" name="shuffle-filter" value="Broiler Lighting" checked=""><div class="filter_option_solution"><div class="image_div"></div>Broilers</div>
            </label>
    
            <label class="btn btn-outline-primary">
              <input type="radio" class="input-none" name="shuffle-filter" value="Layer Lighting" checked=""><div class="filter_option_solution"><div class="image_div"></div>Layers</div>
            </label>
    
            <label class="btn btn-outline-primary">
              <input type="radio" class="input-none" name="shuffle-filter" value="Breeder Lighting" checked=""><div class="filter_option_solution"><div class="image_div"> </div>Breeders</div>
            </label>
    
            <label class="btn btn-outline-primary">
              <input type="radio" class="input-none" name="shuffle-filter" value="Pullet lighting" checked=""><div class="filter_option_solution"><div class="image_div">
              </div>Pullet</div>
            </label>
    
            <label class="btn btn-outline-primary">
              <input type="radio" class="input-none" name="shuffle-filter" value="Swine Lighting" checked=""><div class="filter_option_solution"><div class="image_div">
        
              </div>Swine</div>
            </label>
    
          </div>
    
    
    <div class="region filter">
      <div class="region_group" id="region-options">
        <input type="radio" id="europe" name="region" value="europe">
        <label for="europe">Europe</label>
        
        <input type="radio" id="us" name="region" value="us">
        <label for="javascript">US</label>
      </div>
    </div>
    
    </div>
        <div clas="product_grid js-shuffle" id="product_grid">
    
    
        <div class="product_grid_item shuffle-item" data-solutions="[&quot;Broiler Lighting&quot;,&quot;Layer Lighting&quot;,&quot;Pullet lighting&quot;,&quot;Swine Lighting&quot;]" data-region="[&quot;Europe&quot;]">
           <div class="product_grid_item_container" >
            <div class="image_product_item"></div>
            <a href="/test">Item1</a>
          </div>
        </div>
        <div class="product_grid_item shuffle-item" data-solutions="[&quot;Broiler Lighting&quot;,&quot;Layer Lighting&quot;,&quot;Swine Lighting&quot;]" data-region="[&quot;US&quot;]">
           <div class="product_grid_item_container" >
            <div class="image_product_item"></div>
            <a href="/test">Item2</a>
          </div>
        </div>
        <div class="product_grid_item shuffle-item" data-solutions="[&quot;Broiler Lighting&quot;]" data-region="[&quot;US&quot;]">
           <div class="product_grid_item_container" >
            <div class="image_product_item"></div>
            <a href="/test">Item2</a>
          </div>
        </div>
    
          
      </div>
    </div>

<script>
const Shuffle = window.Shuffle;
const element = document.getElementById('product_grid');
const sizer = element.querySelector('.js-shuffle-sizer');

const shuffleInstance = new Shuffle(element, {
  itemSelector: '.product_grid_item',
  sizer: sizer,
  buffer: 1,
});

let selectedSolutions = [];
let selectedRegions = [];

jQuery('input[name="shuffle-filter"]').on('change', function (evt) {
  var input = evt.currentTarget;
  if (input.checked) {
    selectedSolutions.push(input.value);
  } else {
    var index = selectedSolutions.indexOf(input.value);
    if (index !== -1) {
      selectedSolutions.splice(index, 1);
    }
  }

  // Apply combined filtering
  shuffleInstance.filter(function (element) {
    const solutions = JSON.parse(element.getAttribute('data-solutions'));
    const regions = JSON.parse(element.getAttribute('data-region'));
    const solutionFilter = selectedSolutions.length === 0 || selectedSolutions.some(s => solutions.includes(s));
    const regionFilter = selectedRegions.length === 0 || selectedRegions.includes(regions[0]);
    return solutionFilter && regionFilter;
  });
});

jQuery('input[name="region"]').on('change', function (evt) {
  var input = evt.currentTarget;
  if (input.checked) {
    selectedRegions.push(input.value);
  } else {
    var index = selectedRegions.indexOf(input.value);
    if (index !== -1) {
      selectedRegions.splice(index, 1);
    }
  }

  // Apply combined filtering
  shuffleInstance.filter(function (element) {
    const solutions = JSON.parse(element.getAttribute('data-solutions'));
    const regions = JSON.parse(element.getAttribute('data-region'));
    const solutionFilter = selectedSolutions.length === 0 || selectedSolutions.some(s => solutions.includes(s));
    const regionFilter = selectedRegions.length === 0 || selectedRegions.includes(regions[0]);
    return solutionFilter && regionFilter;
  });
});
</script>
flseospp

flseospp1#

你在正确的方向上,但shuffleInstance.filter函数需要在每次你想过滤的时候被调用。我做了一个例子,但选择框,而不是单选按钮。请复制这个代码,如果你愿意,可以修改它。
另外,请阅读shuffle.js here中有关filter函数和其他函数的文档。

const Shuffle = window.Shuffle;
const element = document.querySelector(".products");
const sizer = element.querySelector('.js-shuffle-sizer');

const shuffleInstance = new Shuffle(element, {
  itemSelector: '.product',
  buffer: 1,
});

let selections = {region: "europe", color: "green"};

function filter() {
    shuffleInstance.filter(element => {
    colors = JSON.parse(element.getAttribute("data-colors"));
    regions = JSON.parse(element.getAttribute("data-regions"));
    hasColor = colors.includes(selections.color);
    hasRegion = regions.includes(selections.region);
    return (hasColor && hasRegion);
  });
}
filter();

$("select#color").on("change", function() {
    selections.color = $(this).val();
  filter();
});

$("select#region").on("change", function() {
    selections.region = $(this).val();
  filter();
});
.products {
  display: flex;
  gap: 10px;
  
  margin-top: 20px;
}

.products .product .placeholderimg {
  width: 200px;
  aspect-ratio: 16/9;
  background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Shuffle/5.2.1/shuffle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

<select id="color">
  <option value="green">Green</option>
  <option value="blue">Blue</option>
  <option value="red">Red</option>
  <option value="purple">Purple</option>
  <option value="orange">Orange</option>
</select>

<select id="region">
  <option value="europe">Europe</option>
  <option value="us">US</option>
</select>

<div class="products">
  <div class="product" data-colors='["green","orange"]' data-regions='["europe"]'>
    <div class="placeholderimg">image</div>
    <p>Green, Orange and Europe</p>
  </div>
  <div class="product" data-colors='["green","blue", "red", "purple", "orange"]' data-regions='["europe"]'>
    <div class="placeholderimg">image</div>
    <p>All colors and Europe</p>
  </div>
  <div class="product" data-colors='["green","orange"]' data-regions='["us"]'>
    <div class="placeholderimg">image</div>
    <p>Green, Orange and US</p>
  </div>
  <div class="product" data-colors='["purple"]' data-regions='["us"]'>
    <div class="placeholderimg">image</div>
    <p>Purple and US</p>
  </div>
</div>

相关问题