如何使用 list.js 选择下拉选项来对列表项进行排序

How to sort list item by selecting dropdown option using list.js

我正在使用 list.js 对列表项进行排序,但我想在更改 select > option

时对项目进行排序

HTML

<div id="hotels">
    <input class="search" placeholder="Search" />
    <button class="sort" data-sort="name">
      Sort by name
    </button>

    <select class="sort" data-sort="">
      <option value="" selected>Sort by</option>
      <option value="hotelName">Hotel Name</option>
      <option value="price">Price</option>
      <option value="star">Star</option>
    </select>

    <ul class="list">
      <li>
        <h3 class="hotelName">The Umrao</h3>
        <p class="price">5002</p>
        <p class="star">3</p>
      </li>
      <li>
        <h3 class="hotelName">Welcomhotel Dwarka, New Delhi</h3>
        <p class="price">8367</p>
        <p class="star">5</p>
      </li>
      <li>
        <h3 class="hotelName">Lemon Tree Premier, Delhi Airport</h3>
        <p class="price">3978</p>
        <p class="star">4</p>
      </li>
      <li>
        <h3 class="hotelName">Red Fox Hotel, Delhi Airport</h3>
        <p class="price">9676</p>
        <p class="star">2</p>
      </li>
    </ul>
  </div>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  <script src="http://listjs.com/no-cdn/list.js"></script>

我已经完成了一些代码,但是当我 select 第一次工作时它有错误,但是如果我在单击 select 时再次更改,它会使用以前的值排序项目,然后在下拉列表之后打开然后我 select 然后它按当前值排序。

这是我的代码

var options = {
  valueNames: [ 'hotelName', 'price', 'star' ]
};

var hotelList = new List('hotels', options);

$('.sort').change(function(){
  var selection = $(this).val();
  console.log(selection);
  $(this).attr('data-sort', selection);
});

您应该为此使用 hotelList.sort 函数:

var options = {
  valueNames: [ 'hotelName', 'price', 'star' ]
};

var hotelList = new List('hotels', options);

$('select.sort').change(function(){
  var selection = $(this).val();
  hotelList.sort(selection);
});

$('button.sort').click(function() {
  hotelList.sort($(this).data('sort'));  
});
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://listjs.com/no-cdn/list.js"></script>
<div id="hotels">
  <input class="search" placeholder="Search" />
  <button class="sort" data-sort="hotelName">
    Sort by name
  </button>

  <select class="sort">
    <option value="" selected>Sort by</option>
    <option value="hotelName">Hotel Name</option>
    <option value="price">Price</option>
    <option value="star">Star</option>
  </select>

  <ul class="list">
    <li>
      <h3 class="hotelName">The Umrao</h3>
      <p class="price">5002</p>
      <p class="star">3</p>
    </li>
    <li>
      <h3 class="hotelName">Welcomhotel Dwarka, New Delhi</h3>
      <p class="price">8367</p>
      <p class="star">5</p>
    </li>
    <li>
      <h3 class="hotelName">Lemon Tree Premier, Delhi Airport</h3>
      <p class="price">3978</p>
      <p class="star">4</p>
    </li>
    <li>
      <h3 class="hotelName">Red Fox Hotel, Delhi Airport</h3>
      <p class="price">9676</p>
      <p class="star">2</p>
    </li>
  </ul>
</div>