javascript - 使用 VueJS 2 向 VuetifyJS 列表添加搜索和排序功能
问题描述
我需要向VuetifyJS list添加一个简单的搜索和排序功能。这是列表的 CodePen 示例:https ://codepen.io/anon/pen/bxGGgv
在 VueJS 2 中执行此操作的标准方法是什么?
HTML:
<v-list two-line>
<template v-for="(item, index) in items">
<v-list-tile
:key="item.title"
avatar
ripple
@click="toggle(index)"
>
<v-list-tile-content>
<v-list-tile-title>{{ item.title }}</v-list-tile-title>
<v-list-tile-sub-title class="text--primary">
{{ item.headline }}
</v-list-tile-sub-title>
<v-list-tile-sub-title>{{ item.subtitle }}</v-list-tile-sub-title>
</v-list-tile-content>
</v-list-tile>
<v-divider
v-if="index + 1 < items.length"
:key="index"
></v-divider>
</template>
</v-list>
JS:
export default {
data () {
return {
selected: [2],
items: [
{
action: '15 min',
headline: 'Brunch this weekend?',
title: 'Ali Connors',
subtitle: "I'll be in your neighborhood doing errands this weekend. Do you want to hang out?"
},
{
action: '18hr',
headline: 'Recipe to try',
title: 'Britta Holt',
subtitle: 'We should eat this: Grate, Squash, Corn, and tomatillo Tacos.'
}
]
}
},
}
解决方案
您可以在您的类中定义一个计算属性并执行您的过滤器。您可以将此计算属性用作过滤和排序功能。
这是代码笔
new Vue({
el: '#app',
data: {
selected: [2],
search: '',
items: [{
action: '15 min',
headline: 'Brunch this weekend?',
title: 'Ali Connors',
subtitle: "I'll be in your neighborhood doing errands this weekend. Do you want to hang out?"
},
{
action: '2 hr',
headline: 'Summer BBQ',
title: 'me, Scrott, Jennifer',
subtitle: "Wish I could come, but I'm out of town this weekend."
},
{
action: '6 hr',
headline: 'Oui oui',
title: 'Sandra Adams',
subtitle: 'Do you have Paris recommendations? Have you ever been?'
},
{
action: '12 hr',
headline: 'Birthday gift',
title: 'Trevor Hansen',
subtitle: 'Have any ideas about what we should get Heidi for her birthday?'
},
{
action: '18hr',
headline: 'Recipe to try',
title: 'Britta Holt',
subtitle: 'We should eat this: Grate, Squash, Corn, and tomatillo Tacos.'
}
]
},
computed: {
filteredItems() {
return _.orderBy(this.items.filter(item => {
return item.title.toLowerCase().includes(this.search.toLowerCase()) ||
item.action.toLowerCase().includes(this.search.toLowerCase()) ||
item.headline.toLowerCase().includes(this.search.toLowerCase()) ||
item.subtitle.toLowerCase().includes(this.search.toLowerCase());
}), 'headline');
}
},
methods: {
toggle(index) {
const i = this.selected.indexOf(index)
if (i > -1) {
this.selected.splice(i, 1)
} else {
this.selected.push(index)
}
}
}
})
推荐阅读
- python - 如何从python中的字典字典中获取列表列表?
- python - 如何在 python 中使用 twitter API 抓取包含特定链接的推文?
- lua - Lua 局部函数不选择局部变量成员,除非在声明时分配
- reactjs - 为什么它不起作用,函数调用和模块导入?
- python - 最有效的带有 if 语句的 Python 循环
- c - My function aren't working after i called them
- javascript - Sqlite 等待数据库打开
- javascript - 仅检查输入数字
- flutter - Flutter StreamBuilder ListView在流数据更改时不会重新加载
- amazon-web-services - 使用 react-native-aws3 上传到 S3