如何最好地在js上对产品进行排序?

时间:2019-04-01 09:48:09

标签: javascript html ecmascript-6

我有此代码,但无法正常工作。

let productBox = document.querySelectorAll('.product');
let selectCategory = document.querySelector('#select-category');
let categoryVal = document.querySelectorAll('#select-category option');

selectCategory.addEventListener('change', () => {
    Array.prototype.forEach.call(categoryVal, catItem => {
        Array.prototype.forEach.call(productBox, prodItem => {
            if((catItem.selected) && (prodItem.category === 
            catItem.category)) {
                prodItem.style.display = 'none'
            }
        })
    })
})
.product {
  border: 1px solid;
  padding: 2px;
  margin: .5rem;
}
<select id="select-category">
    <option category="all">All</option>
    <option category="dinner">Dinner</option>
    <option category="first meal">First meal</option>
    <option category="garnish">Garnish</option>
</select>

<div category="dinner" class="product">Dinner</div>
<div category="first meal" class="product">First meal</div>
<div category="garnish" class="product">Garnish</div>

我需要在选择option元素时,将display none应用于类别不匹配的块。最好的方法是什么?

4 个答案:

答案 0 :(得分:1)

您的代码中存在许多问题,并且您的HTML无效。

HTML5中没有定义category属性。如果要使用自定义属性,则应在它们前面加上data-*。对于select选项,您应该只使用value属性。

修复此问题后,逻辑只需:

  1. 如果select元素发生更改,则从中获取所选值。
  2. 将乘积div元素以及每个元素:
    • 如果所选类别为all,或者与元素的data-category相匹配,请将显示样式设置为其默认样式(''
    • 否则将显示样式设置为none

这是完整的代码段:

const productDivs = document.querySelectorAll('.product');
const categorySelect = document.querySelector('#select-category');

categorySelect.addEventListener('change', (e) => {
  const category = e.target.value;

  [...productDivs].forEach(pd => {
    const display = pd.dataset.category === category || category === 'all';
    pd.style.display = display ? '' : 'none';
  });
});
<select id="select-category">
  <option value="all">All</option>
  <option value="dinner">Dinner</option>
  <option value="first meal">First meal</option>
  <option value="garnish">Garnish</option>
</select>

<div data-category="dinner" class="product">Dinner</div>
<div data-category="first meal" class="product">First meal</div>
<div data-category="garnish" class="product">Garnish</div>

答案 1 :(得分:0)

工作正常。让我知道是您的期望

<!DOCTYPE html>
<html>
  <head>
    <title>Week One</title>
    <link href="./style_home.css" type="text/css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Baloo+Chettan|Montserrat|Oswald|ZCOOL+XiaoWei" rel="stylesheet">
  </head>
  <body>
    <div class="topbar">
      <table class="topbar">
        <tr>
          <td><a class="topbar">Home</a></td>
          <td><a class="topbar">About</a></td>
          <td><a class="topbar">Contact</a></td>
        </tr>
      </table>
    </div>
  </body>
</html>

答案 2 :(得分:0)

function selectCategory(element) {
  const value = element.value;
  const products = document.getElementsByClassName('product');
  Array.prototype.forEach.call(products, product => {
    const productAttr = product.getAttribute('category');
    if(value === 'All'){
      product.style.display = 'block';
      return;
    }
    productAttr !== value.toLowerCase() ? 
        product.style.display = 'none' :
        product.style.display = 'block';
  })
}
<select id="select-category" onChange="selectCategory(this)">
    <option category="all">All</option>
    <option category="dinner">Dinner</option>
    <option category="first meal">First meal</option>
    <option category="garnish">Garnish</option>
</select>

<div category="dinner" class="product">Dinner</div>
<div category="first meal" class="product">First meal</div>
<div category="garnish" class="product">Garnish</div>

答案 3 :(得分:0)

  1. 我将选项的category重命名为value
  2. 我用{}包裹了整个代码,所以我可以使用局部变量
  3. 我使用input事件代替了change事件
  4. 我监听文档中冒泡的输入事件,因此可以在以后添加的内容中检查该事件
  5. 我使用CSS类hide而不是直接设置样式
  6. 我首先将每个product的类设置为hide(不适用于选项all),然后为具有适当类别值的元素取消该类的设置。

console.clear();

{
    document.addEventListener('input', (e) => {
    const selectCategory = document.querySelector('#select-category');
    if (e.target === selectCategory) {
      const productBoxes = document.querySelectorAll('.product');
      const categoryVal = document.querySelectorAll('#select-category option');

      if (selectCategory.value == 'all') {
        show(productBoxes)
      } else {
        hide(productBoxes)
      }
      const matches = document.querySelectorAll(`[category*="${selectCategory.value}"]`)
      show(matches)
    }    

  })
  
  
  const hide = (items) => {
    Array.prototype.forEach.call(items, prodItem => {
      prodItem.classList.add('hide')
    })
  }
  const show = (items) => {
    Array.prototype.forEach.call(items, prodItem => {
      prodItem.classList.remove('hide')
    })
  }
  
}
.product {
  border: 1px solid;
  padding: 2px;
  margin: .5rem;
}

.hide {
  display: none;
}
<select id="select-category">
  <option value="all">All</option>
  <option value="dinner">Dinner</option>
  <option value="first meal">First meal</option>
  <option value="garnish">Garnish</option>
</select>

<div category="dinner" class="product">Dinner: One</div>
<div category="first meal" class="product">First meal</div>
<div category="garnish" class="product">Garnish: one</div>
<div category="garnish" class="product">Garnish: two</div>
<div category="dinner" class="product">Dinner: Two</div>

<div category="dinner" class="product">Dinner: three</div>

<br><br><select><option>totally<option>unrelated<option>select</select>