gpt4 book ai didi

javascript - 当用户在输入栏中键入内容时过滤表格

转载 作者:行者123 更新时间:2023-12-01 01:00:06 25 4
gpt4 key购买 nike

我有一个基于国家/地区对象数组填充的表格,并且还有一个搜索栏,它将通过国家/地区数组进行实时过滤并仅显示部分或完全匹配用户的国家/地区来与表格进行交互在搜索栏中输入。

问题是我是 vue 新手,我无法弄清楚如何让它工作。如果有人可以查看我的代码并直接指出我的正确方向或我做错了什么,那就太好了!

所以现在我的逻辑是,我在文本字段上有一个 v-model,它将把用户输入的任何内容绑定(bind)到名为“filterBy”的数据值。

我的理解可能是不正确的,但我现在的想法是,通过在compute内部创建一个filteredCountries函数,并且由于compute会在函数内部的变量发生变化时运行,因此每当在搜索栏中输入内容时它都会自动被调用,从而过滤国家数组,并且表格将被重新渲染。

<template>
<div class="countries-table">
<div class="countries-search-bar">
<v-flex xs12 sm6 md3>
<v-text-field
v-model="filterBy"
placeholder="Search by country name or alpha2"
/>
</v-flex>
</div>
<v-data-table
:headers="headerValues"
:items="items"
:pagination.sync="pagination"
item-key="id"
class="elevation-1"
:rows-per-page-items="[300]"
>
<template v-slot:headers="props">
<tr>
<th
v-for="header in props.headers"
:key="header.text"
:class="[
'column sortable',
pagination.descending ? 'desc' : 'asc',
header.value === pagination.sortBy ? 'active' : ''
]"
@click="changeSort(header.value)"
>
<v-icon small>arrow_upward</v-icon>
{{ header.text }}
</th>
<th>
Edit
</th>
</tr>
</template>

<template v-slot:items="props">
<tr :active="props.selected" @click="props.selected = !props.selected">
<td>{{ props.item.country_alpha2 }}</td>
<td class="text-xs-right">{{ props.item.country_name }}</td>
<boolean-cell
custom-class="text-xs-right"
:input="props.item.is_active"
:output="{ true: 'Yes', false: 'No' }"
></boolean-cell>
<date-cell
custom-class="text-xs-right"
:input="props.item.updated_at"
></date-cell>
<td class="text-xs-right" @click="triggerEdit(props.item)">
<v-icon class="edit-icon">edit</v-icon>
</td>
</tr>
</template>
</v-data-table>
</div>
</template>

<script>
import BooleanCell from '~/components/global-components/Table/BooleanCell'
import DateCell from '~/components/global-components/Table/DateCell'

export default {
components: {
BooleanCell,
DateCell
},
props: {
headerValues: {
type: Array,
required: true
},
items: {
type: Array,
required: true
}
},
computed: {
filteredCountries() {
return this.items.filter(country => {
return country.country_name.includes(this.filterBy)
})
}
},
data() {
return {
pagination: {
sortBy: 'country_alpha2'
},
filterBy: ''
}
},
methods: {
changeSort(headerValue) {
if (this.pagination.sortBy === headerValue) {
this.pagination.descending = !this.pagination.descending
} else {
this.pagination.sortBy = headerValue
this.pagination.descending = false
}
}
}
}
</script>

尽管我在搜索栏中输入了内容,但该表与我当前的代码保持不变。

有人可以告诉我我做错了什么吗?

最佳答案

对于 v-data-table 项目,您使用的是作为 Prop 的 items 。您应该使用 filteredCountries 计算属性。

关于javascript - 当用户在输入栏中键入内容时过滤表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56160414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com