Skip to content

Instantly share code, notes, and snippets.

@allanfreitas
Forked from RDelorier/example.html
Last active September 21, 2015 15:22
Show Gist options
  • Save allanfreitas/bbfb57111c2ef2cd4728 to your computer and use it in GitHub Desktop.
Save allanfreitas/bbfb57111c2ef2cd4728 to your computer and use it in GitHub Desktop.
Vue js filter to use with select options
//js
users = [
{ name:'foo', id:1 },
{ name:'bar', id:2 },
{ name:'baz', id:3 }
];
//html
<select
v-model="user.id"
options="users | toSelect 'name' 'id'">
</select>
//outputs
<select>
<option value="1">foo</option>
<option value="2">bar</option>
<option value="3">baz</option>
</select>
'use strict';
/**
*
* @param collection to map
* @param text index of visible text from
* @param value index of value
* @returns array
*/
var toSelect = function(collection, text, value){
return collection.map(function(e){
return {
text: e[text],
value:e[value]
}
})
};
require('vue').filter('toSelect', toSelect);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment