Created
May 14, 2018 12:59
-
-
Save oliverbth05/b341a620189faeab9a91e01b38836dd4 to your computer and use it in GitHub Desktop.
Array Sorting Vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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