Skip to content

Instantly share code, notes, and snippets.

@tclancy
Created February 3, 2017 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tclancy/6abc8a85e3278d65a998dbef71a3d09b to your computer and use it in GitHub Desktop.
Save tclancy/6abc8a85e3278d65a998dbef71a3d09b to your computer and use it in GitHub Desktop.
vue.js example code
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