admin管理员组文章数量:1392066
I have a search box where when the user inputs an artist name and it shows a list of artists that match the user input.
I want to display the artist image next to the artist name in the search.
I have an object that contains the following. Artist Name as key and path to image as value
Radiohead: 'path_to_image',
Elliott Smith:'path_to_image'
I have a puted property that filters the artists name for search.
puted: {
filteredArtists() {
return Object.keys(this.artistsList).filter((artist) => {
return artist.match(this.artistName)
})
}
},
and in my template I'm iterating throgh the values
<ul class="searchFliter" v-for="artist in filteredArtists">
<li v-text="artist"></li>
</ul>
I can't find a way to manage that with puted values. I can easily iterate over my object and display both Artist name and Artist image but can't filter it.
Thanks in advance
I have a search box where when the user inputs an artist name and it shows a list of artists that match the user input.
I want to display the artist image next to the artist name in the search.
I have an object that contains the following. Artist Name as key and path to image as value
Radiohead: 'path_to_image',
Elliott Smith:'path_to_image'
I have a puted property that filters the artists name for search.
puted: {
filteredArtists() {
return Object.keys(this.artistsList).filter((artist) => {
return artist.match(this.artistName)
})
}
},
and in my template I'm iterating throgh the values
<ul class="searchFliter" v-for="artist in filteredArtists">
<li v-text="artist"></li>
</ul>
I can't find a way to manage that with puted values. I can easily iterate over my object and display both Artist name and Artist image but can't filter it.
Thanks in advance
Share Improve this question asked Sep 3, 2017 at 16:19 Qais Abou JaoudéQais Abou Jaoudé 6271 gold badge8 silver badges17 bronze badges1 Answer
Reset to default 4If you want to stick to your data structure then there are a number of ways you could manage to display the image along with the matching artists. Since you are essentially getting a list of keys to your artistList
object in your puted property, you can use that key to get the path using artistList[artist]
.
<ul class="searchFliter" v-for="artist in filteredArtists">
<li>{{artist}} <img :src="artistList[artist]"></li>
</ul>
However, if you want to instead, as you suggest in the title of your post, return multiple values from the list, then you can alter the puted property.
filteredArtists() {
let matches = return Object.keys(this.artistsList).filter((artist) => {
return artist.match(this.artistName)
})
return matches.map(m => ({name: m, imagePath: this.artistsList[m]}))
}
Here the puted property is finding all the matches and then creating a new object containing the name and image path. Use it in the template like so:
<ul class="searchFliter" v-for="artist in filteredArtists">
<li>{{artist.name}} <img :src="artist.imagePath"></li>
</ul>
Of course, you can also choose a different data structure as well. Why not use use an array of artist objects?
[
{name: "Radiohead", imagePath: "path to image"},
{name: "Elliott Smith", imagePath: "path to image"}
]
In which case your puted property simply bees
filteredArtists() {
return this.artistsList.filter(m => m.name.match(this.artistName))
}
本文标签: javascriptReturn multiple values from a computed filter in Vue JSStack Overflow
版权声明:本文标题:javascript - Return multiple values from a computed filter in Vue JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744781121a2624719.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论