Created
February 3, 2017 16:24
-
-
Save tclancy/6abc8a85e3278d65a998dbef71a3d09b to your computer and use it in GitHub Desktop.
vue.js example code
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
var app = new Vue({ | |
http: { | |
headers: { | |
"X-CSRFToken": window.csrf_token | |
} | |
}, | |
el: "#admin-dash-users", | |
data: { | |
addresses: [], | |
count: 0, | |
previous: null, | |
next: null, | |
pageSize: 20, | |
currentSort: "address__user__last_name", | |
currentUrl: null, | |
row: null | |
}, | |
created: function() { | |
this.fetchData(api_url); | |
}, | |
filters: { | |
roundOne: function (current_gallons) { | |
return current_gallons.toFixed(1); | |
} | |
}, | |
methods: { | |
fetchData: function(url) { | |
var self = this; | |
self.currentUrl = url; | |
this.$http.get(url).then( | |
function (response) { | |
data = response.body; | |
self.addresses = data.results; | |
self.previous = data.previous; | |
self.next = data.next; | |
self.count = data.count; | |
}, | |
function(error) { | |
} | |
); | |
}, | |
nextPage: function() { | |
this.fetchData(this.next); | |
}, | |
previousPage: function() { | |
this.fetchData(this.previous); | |
}, | |
sort: function(field) { | |
if (this.currentSort == field) { | |
this.currentSort = "-" + field; | |
} else if (this.currentSort == "-" + field) { | |
this.currentSort = field; | |
} else { | |
this.currentSort = field; | |
} | |
this.fetchData(api_url + "?ordering=" + this.currentSort); | |
}, | |
search: function (e) { | |
var name = e.target.value; | |
if (!name) { | |
return; | |
} | |
this.fetchData(api_url + "?search=" + name); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment