I'm making a sorting function by Vue js
I'd like to make a list by the ID order for the default, then, sorting function can be occured by clicking asc/desc by name button.
Also, when clicking all button, the list sorts by the ID order again and adding the class named is-active
I know I've added sorting function by the default but I don't know how to combine with the order of ID number.
If somebody know how to, please help.
Thank you
new Vue({
el: '#app',
data: {
allItem: true,
order: null,
list: [],
},
created: function () {
axios.get('https://jsonplaceholder.typicode.com/users')
.then(function (response) {
this.list = response.data
}.bind(this)).catch(function (e) {
console.error(e)
})
},
methods: {
all: function() {
this.full = true //for button class 'is-active'... NOT WORKING ...
},
},
computed: {
sort: function() {
console.log(this.order);
return _.orderBy(this.list, 'name', this.order ? 'desc' : 'asc') //loadash
},
sorted: function() {
if (this.order || !this.order) { //sort by arc/desc
this.ordered = true //for button class 'is-active'... NOT WORKING ...
this.allItem = false //for button class 'is-active'... NOT WORKING ...
console.log(this.order);
return this.sort
} else if (this.order = null){ //defalut order by ID number ... NOT WORKING ...
this.ordered = false //for button class 'is-active'... NOT WORKING ...
console.log(this.full);
return this.list
}
},
}
})
span {font-weight: bold;}
.is-active {background: turquoise;}
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
<div id="app">
<ul>
<li v-for="item in sorted" :key="item.id">
<span>ID:</span> {{item.id}} , <span>Name:</span> {{item.name}} , <span>Company:</span> {{item.company.name}}
</li>
</ul>
<button :class="{'is-active': allItem}" @click="all">all</button>
<button :class="{'is-active': ordered}" @click="order=!order">asc/desc by name</button>
</div>