[英]How can I use a single search bar to filter two tables?
I have a Vue2 app, on one of the pages I have a pair of data tables.我有一个 Vue2 应用程序,在其中一个页面上我有一对数据表。 Each table is behind a tab, allowing users to select which they want.每个表都在一个选项卡后面,允许用户访问他们想要的 select。 What isn't behind a tab is the search bar, that I kept separate to avoid duplicating it with code.选项卡后面没有的是搜索栏,我将其分开以避免与代码重复。 However, I need it to be able to filter/search through either table that's being viewed.但是,我需要它能够过滤/搜索正在查看的任一表。
It works perfectly fine when I try to filter through one or the other, but because I can't bind multiple values in a v-model directive, I'm not sure what can be done to achieve what I need.当我尝试过滤其中一个时,它工作得非常好,但是因为我无法在 v-model 指令中绑定多个值,所以我不确定可以做些什么来实现我需要的东西。 Below is the relevant code.下面是相关代码。 Any guidance would be greatly appreciated.任何指导将不胜感激。
Search Bar搜索栏
<div class="container-fluid search-bar">
<v-row no-gutters>
<v-col cols="12" md="5" sm="12" class="text-left">
<v-text-field
type="text"
class="form-control bg-white search-input"
placeholder="SEARCH CONTENT BY NAME OR KEYWORD"
v-model="serviceKeyword"
/>
</v-col>
<v-col cols="12" md="7" sm="12"> </v-col>
</v-row>
</div>
Script to handle the search处理搜索的脚本
Note: this is within the script.注意:这是在脚本中。 Each one is built for the specific table it looks through.每一个都是为它所查看的特定表而构建的。 If I however, I can only get the search to work if I call in either serviceKeyword or resourceKeyword但是,如果我调用 serviceKeyword 或 resourceKeyword,我只能让搜索工作
computed: {
filteredServiceDirectories() {
if (!this.serviceKeyword) {
return this.serviceDirectories;
}
var searchBy = this.serviceKeyword.toLowerCase();
return this.serviceDirectories.filter(
(q) =>
q.name.toLowerCase().indexOf(searchBy) !== -1 ||
(q.organization &&
q.organization.name.toLowerCase().indexOf(searchBy) !== -1) ||
(q.categories &&
q.categories.some(
(c) => c.name.toLowerCase().indexOf(searchBy) !== -1
))
);
},
filteredResourceDirectories() {
if (!this.resourceKeyword) {
return this.resourceDirectories;
}
var resourceSearchBy = this.resourceKeyword.toLowerCase();
return this.resourceDirectories.filter(
(q) =>
q.name.toLowerCase().indexOf(resourceSearchBy) !== -1 ||
(q.organization &&
q.organization.name.toLowerCase().indexOf(resourceSearchBy) !== -1) ||
(q.resourceType &&
q.resourceType.value.toLowerCase().indexOf(resourceSearchBy) !== -1)
);
},
},
There are few ways to solve this.解决这个问题的方法很少。 One is simply searching/filtering both at the same time.一种是同时搜索/过滤两者。
You could change the resourceKeyword
and serviceKeyword
to simply keyword
, bind that to the input and use it to filter both resourceDirectores
and serviceDirectories
.您可以将resourceKeyword
和serviceKeyword
更改为简单的keyword
,将其绑定到输入并使用它来过滤resourceDirectores
和serviceDirectories
。
Only one table/tab is being viewed at a time so it doesn't really matter, or am I misunderstanding?一次只能查看一个表格/选项卡,所以这并不重要,还是我误解了?
If you want a full list when switching between tables you could add a @click="keyword === ''
on the tab.如果在表格之间切换时想要完整列表,可以在选项卡上添加@click="keyword === ''
。
Here is a code-example I made two show filtering two array based on one keyword: https://codepen.io/bergur-the-encoder/pen/ExwBZMj这是一个代码示例,我制作了两个显示基于一个关键字过滤两个数组: https://codepen.io/bergur-the-encoder/pen/ExwBZMj
Another solution is to add a @click
on the tab to set/tell the script what you are looking at, so @click="activeTab=resourceDirectores"
and @click="activeTab=serviceDirectores"
另一种解决方案是在选项卡上添加@click
以设置/告诉脚本您正在查看的内容,因此@click="activeTab=resourceDirectores"
和@click="activeTab=serviceDirectores"
Change to v-model="keyword
like before but in your computed property check the activeTab
data property像以前一样更改为v-model="keyword
但在您的计算属性中检查activeTab
数据属性
computed:
filteredServiceDirectories() {
if (activeTab === 'resourceDirectives') {
return this.serviceDirectories // should not filter those
}
...//rest of code
and then the the same (or opposite) for filteredResourceDirectories然后是相同(或相反)的过滤资源目录
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.