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

时间:2016-10-17 19:16:30

标签: javascript jquery sorting listjs

我使用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>

我已经完成了一些代码,但是当我第一次选择它时它有错误但是如果我再次点击选择它会改变前一个值的排序项并在下拉打开后然后我选择然后它用当前值排序

这是我的代码

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);
});

1 个答案:

答案 0 :(得分:2)

您应该使用hotelList.sort函数:

&#13;
&#13;
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'));  
});
&#13;
<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>
&#13;
&#13;
&#13;