OrderBy不适用于升序,但适用于降序

时间:2019-04-17 12:53:03

标签: vue.js

我正在制作Vue Webapp,需要对数据进行升序或降序排序。 根据代码,一切都应该正常工作

模板

'dir_cat'>=30

订购方式

<div class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuButton">
          <h6 class="dropdown-header">Ascending</h6>
          <div class="dropdown-divider"></div>
          <a @click="order('name','asc')" class="dropdown-item" href="#">Category</a>
          <a @click="order('catName','asc')" class="dropdown-item" href="#">Category</a>
          <a @click="order('subcatName','asc')" class="dropdown-item" href="#">SubCategory</a>
          <a @click="order('cocatName','asc')" class="dropdown-item" href="#">CoCategory</a>
          <a @click="order('uploadedAt','asc')" class="dropdown-item" href="#">Uploaded At</a>
          <div class="dropdown-divider"></div>
          <h6 class="dropdown-header">Descending</h6>
          <div class="dropdown-divider"></div>
          <a @click="order('name','desc')" class="dropdown-item" href="#">Category</a>
          <a @click="order('catName','desc')" class="dropdown-item" href="#">Category</a>
          <a @click="order('subcatName','desc')" class="dropdown-item" href="#">SubCategory</a>
          <a @click="order('cocatName','desc')" class="dropdown-item" href="#">CoCategory</a>
          <a @click="order('uploadedAt','desc')" class="dropdown-item" href="#">Uploaded At</a>
        </div>

降序效果很好。 但是,在升序中,也将Z排序为A,与降序相同。 谁能说出为什么以及如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您的this.orderWay = "desc"是无效的比较。请记住使用===来比较两个变量。

this.orderWay === "desc"

答案 1 :(得分:0)

您可以通过以下方式缩短代码

order(On, Way) {
   this.loading = true;

   this.orderItems = On;
   this.orderWay = Way;

   this.items = _.orderBy(this.items, On, Way);

   this.loading = false;
}