Skip to content

Instantly share code, notes, and snippets.

@oliverbth05
Last active May 18, 2018 19:44
Show Gist options
  • Select an option

  • Save oliverbth05/89b7cfaf93113e5c94cc9c95b8a203f9 to your computer and use it in GitHub Desktop.

Select an option

Save oliverbth05/89b7cfaf93113e5c94cc9c95b8a203f9 to your computer and use it in GitHub Desktop.
Budget Managin Functionality (vue)
new Vue({
el: "#app",
data: {
entries: [],
amount :"",
date :"",
input2: "",
setBudget: 400
},
methods: {
submitEntry: function(){
var formEntry = {
amount: parseFloat(this.amount),
date: new moment(this.date)
}
this.entries.push(formEntry);
this.amount = "";
this.date = "";
},
changeBudget: function(){
this.setBudget = parseFloat(this.input2);
this.input2 = '';
}
},
computed: {
weeklyExp: function(){
var expense = 0;
this.entries.forEach(function(entry){
if(entry.date.diff(moment().startOf('week'), 'days')>0 && entry.date.diff(moment().startOf('week'), 'days')<=7){
expense -= entry.amount;
}
})
return expense;
},
monthlyExp: function(){
var expense = 0;
this.entries.forEach(function(entry){
if(entry.date.diff(moment().startOf('month'), 'days')>=0 && entry.date.diff(moment().startOf('week'), 'days')<=30){
expense -= entry.amount;
}
})
return expense;
},
yearlyExp: function(){
var expense = 0;
this.entries.forEach(function(entry){
if(entry.date.diff(moment().startOf('year'), 'days')>=0 && entry.date.diff(moment().startOf('week'), 'days')<=365){
expense -= entry.amount;
}
})
return expense;
},
budgetRemaining: function(){
return this.setBudget - this.monthlyExp;
}
}
})
<script src = "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.js"></script>
<div id="app">
<input v-model = "amount" @keydown.enter = "submit" placeholder = "amount $">
<input type = "date" v-model = "date">
<button @click = "submitEntry">
Enter
</button>
<ul>
<li v-for = "entry in entries">
{{entry.amount}}
{{entry.date.format("M/DD/YY")}}
</li>
</ul>
<h3>
Weekly Budget: {{setBudget}} $
</h3>
<h3>
Remaining: {{budgetRemaining}}
</h3>
<br>
<div id = "spendingPanel">
<h1>
This Week: ${{weeklyExp}}
<br>
This Month: ${{monthlyExp}}
<br>
This Year: ${{yearlyExp}}
</h1>
</div>
<input v-model = "input2" placeholder = "budget">
<button @click = "changeBudget">
Set Budget
</button>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment