Skip to content

Instantly share code, notes, and snippets.

@oliverbth05
Created May 14, 2018 12:59
Show Gist options
  • Save oliverbth05/b341a620189faeab9a91e01b38836dd4 to your computer and use it in GitHub Desktop.
Save oliverbth05/b341a620189faeab9a91e01b38836dd4 to your computer and use it in GitHub Desktop.
Array Sorting Vue
new Vue({
el: "#app",
data: {
entries: [],
amount :"",
date :"",
byDate: true
},
methods: {
submit: function(){
var formEntry = {
amount: parseFloat(this.amount),
date: this.date
}
this.entries.push(formEntry);
this.amount = "";
this.date = "";
}
},
computed: {
dateSortedEntry: function(){
this.entries.sort(function(a, b) {
var dateA = new Date(a.date), dateB = new Date(b.date);
return dateA - dateB;
});
return this.entries.reverse();
},
amountSortedEntry: function(){
this.entries.sort(function(a, b){
return a.amount - b.amount;
});
return this.entries.reverse();
}
}
})
<div id="app">
<input v-model = "amount" @keydown.enter = "submit">
<input type = "date" v-model = "date">
<button @click = "byDate = !byDate">
Change Sort
</button>
<template v-if = "byDate">
<h5>
Date
</h5>
<ul>
<li v-for= "entry in dateSortedEntry">{{entry.amount}}/{{entry.date}}</li>
</ul>
</template>
<template v-else>
<h5>
Amount
</h5>
<ul>
<li v-for= "entry in amountSortedEntry">{{entry.amount}}/{{entry.date}}</li>
</ul>
</template>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment