I have a similar question: how to dynamically create calculations in Vue.js under dynamically loaded data .
But, in the end, here is a possible solution for you from the conclusion that I get after discussion and trial and error. There are several ways:
- create an expander that will assign a compute inside each element, and then call it using
item.computedProperty() in the user interface (with double brackets) - Suppose the data is loaded, such as
items , create an observer that will assign a calculated one to each element, and then put it in extendedItems . the computed function will become reactive when bound to the user interface, so the user interface will use extendedItems items instead of items - use vue
this.$set to manually set and enable reactivity for calculated values.
This is calculated using the function from solution No. 1:
new Vue({ el: '#content', data: { "users": [ { "id": 3, "first_name": "Joe", "last_name": "Blogs" }, { "id": 3, "first_name": "Jane", "last_name": "Doe" } ] }, computed: { extendedUsers: function(){ var users = this.users; for (var i in users) this.extendUser(users[i]) }, extendUser: function(user){ user.fullName = function(){ return user.first_name + " " + user.last_name; } } } }); <tr v-for="user in users | orderBy user.fullName()"> <td class="col-xs-6 col-sm-3 col-md-3 col-lg-3"> {{user.fullName()}} </td> <tr>
Hans yulian
source share