How can I pass selected value to a vuejs function?
v-model
won't help I guess.
I need to set values for the filter after
item: items | orderBy sortKey reverse
where reverse
and sortKey
are dynamic values.
html
<select class="sort-filter" v-on="change: sortBy(???)">
<option value="title asc">Title (A-Z)</option>
<option value="title desc">Title (Z-A)</option>
<option value="price asc">Price (Min. - Max.)</option>
<option value="price desc">Price (Max. - Min.)</option>
</select>
js
methods: {
sortBy: function (sortKey) {
console.log(sortKey)
}
}
You have several ways to do it.
Edit: Improved 2)
It is possible to use v-model just like in 2) but instead of using the value directly in your orderBy filter, you can use computed properties
This way, sortKey and sortOrder will be available like a normal property in you filter:
1) Use javascript event:
If you don't specify any parameter, the native event object will be passed automatically
You can then use it like this:
2) Using v-model
You can add the v-model directive
This way the
sorting
value will be updated on every change.You can add this value in the data object of you ViewModel to be more clear:
You can then access the value like in your method:
If you just need to update the
sortKey
value, this method is not even necessary.3) Other weird way
You can apparently use your model value as a parameter.
This is working but I don't really see the point.
If you want to pass selected value to a vuejs function, Please do the following :
So your code will look like :