我正在玩Vue.js,我在设法找出如何在单击按钮时对列表进行排序。
实际上,我确实成功地使用orderedBeers方法对列表进行了排序;当我单击按钮时,我可以在检查器中看到排序列表,但无法更改HTML中的数据。
每个建议都值得赞赏。预先谢谢你。
<template>
<div>
<div class=" main-conte" >
<transition-group name="fade" tag="div" id="container" class=" row " >
<figure v-for="(value,index) in toBeShown" id="page-wrap" :key="index" class="beer-container col-xs-6 col-sm-6 col-lg-4 col-xl-2" >
<a >
<img @click="goTodetail(value.id)" class="logo lazy img-responsive loaded" v-bind:src="getMissingImg(index)"/>
<figcaption>
<div class="beer-title">{{value.name}}</div>
<div class="beer-availability"> {{value.tagline}}</div>
<div class="learn-more">
<h4 class="beer-info-title">Format</h4>
<span class="serving-icons"></span>
<div class="serving">
<i v-if="value.abv >= 0 && value.abv <=6 " class="fas fa-wine-glass-alt"></i>
<i v-if="value.abv >= 6 && value.abv <=7" class="fas fa-glass-cheers"></i>
<i v-if="value.abv >= 7 && value.abv <=100" class="fas fa-wine-bottle"></i>
<span class="measure">{{value.abv}}</span>%
</div>
</div>
</figcaption>
</a>
</figure>
</transition-group>
<div class=prev-next>
<button @click="orderedBeers">Click Me!</button>
<button class="prev" @click="prevPage" :disabled="currentPage==1">
<i class="fas fa-angle-double-left"></i></button>
<button class="next" @click="nextPage" :disabled="currentPage == totalPages">
<i class="fas fa-angle-double-right"></i> </button>
</div>
</div>
<div>
</div>
</div>
</template>
<script>
import { mdbView, mdbMask } from "mdbvue";
import FadeTransition from "./fade-transition.vue";
export default {
name: "home",
components: {
mdbView,
mdbMask,
FadeTransition
},
data() {
return {
items: [],
currentPage: 1,
};
},
computed: {
//show more less products
toBeShown() {
return this.items.slice(0, this.currentPage * 5);
},
totalPages() {
return Math.ceil( this.items.length / 4);
},
},
mounted() {
this.fetchData();
},
methods: {
fetchData: function() {
const myRequest = new Request("https://api.punkapi.com/v2/beers");
fetch(myRequest)
.then(response => {
return response.json();
})
.then(data => {
this.items = data;
console.log(this.items);
})
.catch(error => {
console.log(error);
});
},
getMissingImg(index) {
return this.images[index];
},
nextPage(){
if(this.currentPage < this.totalPages) this.currentPage++;
// this.scrollToEnd();
},
prevPage(){
this.currentPage = this.currentPage - 1 || 1;
// this.scrollToEnd();
},
goTodetail(prodId) {
let proId=prodId
this.$router.push({name:'blog',params:{Pid:proId}})
},
orderedBeers: function () {
console.log(_.orderBy(this.toBeShown, 'name', 'asc'));
return _.orderBy(this.toBeShown, 'name', 'asc');
},
}
};
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
答案 0 :(得分:1)
您快到了。您只需要使用另一个对列表进行排序的计算属性,然后在v-for
中使用它即可。
将模板修改为:
<figure v-for="(value,index) in toBeShownOrdered" id="page-wrap" :key="index" >
<button @click="orderByName = !orderByName">Click Me!</button>
组件包括:
// Add in data:
orderByName: false,
// Add in computed
toBeShownOrdered() {
return this.orderByName ? _.orderBy(this.toBeShown, 'name', 'asc') : this.toBeShown;
}
另请参见:https://vuejs.org/v2/guide/list.html#Displaying-Filtered-Sorted-Results
答案 1 :(得分:1)
您的数据源是计算出的属性toBeShown
。如果您希望对数据进行排序,则toBeShown
必须返回该数据。
在orderedBeers
中,当您应更改returned
以使用排序后的数据本身时,正在按toBeShown
对项toBeShown
进行排序。
更改orderedBeers
以更新data.items
以反映新的顺序:
orderedBeers: function () {
this.data.items = _.orderBy(this.data.items, 'name', 'asc')
}
这将对整个项目列表进行排序,而不只是对当前显示的页面进行排序。
或
使用排序标志并将排序登录名移至toBeShown
data() {
return {
items: [],
currentPage: 1,
sorted: false
}
}
...
orderedBeers: function () {
this.sorted = true
}
...
toBeShown() {
return this.sorted ? _.orderBy(this.data.items, 'name', 'asc').slice( 0, this.currentPage) : this.items.slice(0, this.currentPage * 5);
}
这还将对所有项目进行排序,但是可以稍微更改代码以仅对当前显示的页面进行排序
toBeShown() {
let items = this.items.slice(0, this.currentPage * 5)
return this.sorted ? _.orderBy(items, 'name', 'asc') : items
}