[英]Vuejs / javascript / I am trying to connect a search bar that searches my table and displays the results according to my input
I'm trying to loop through each button within the search bar while displaying only the rows that match the input with filteredUsers
.我试图遍历搜索栏中的每个按钮,同时仅显示与filteredUsers
用户的输入匹配的行。
I am getting the list of users from an AWS S3 bucket and then bringing that data into a table.我从 AWS S3 存储桶中获取用户列表,然后将该数据放入表中。
<div>
<div id="main">
Search: <input type="text" @input="filteredUsers" v-model="search" />
<base-table id="empTable" :data="info" :columns="columns">
<template slot="columns">
<th>Full Name</th>
<th>Work Email</th>
<th>Title</th>
<th> Status </th>
<th class="text-right">Actions</th>
</template>
<template
slot-scope="{row}"
v-if="filteredUsers &&
typeof info != 'undefined' &&
info != null &&
info.length != null &&
info.length > 0
" id="table">
<td>
<button class="buttonChange " @click="(t) =>viewInfo(t)">
{{row.formattedName }}
</button>
</td>
<td>
<button class="buttonChange" @click="(t) =>viewInfo(t)">
{{row.workEmail}}
</button>
</td>
<td>
<button class="buttonChange" @click="(t) =>viewInfo(t)">
{{row.title}}
</button>
</td>
<td>
<button class="buttonChange" @click="(t) =>viewInfo(t)">
{{row.activeORinactive}}
</button>
</td>
My Javascript我的 Javascript
data() {
return {
columns: [],
info: [],
infoModal: "",
modal: false,
modalView: false,
refreshOnExit: 0,
error: "",
name: '',
search: " ",
}
},
methods: {
filteredUsers() {
return this.info.filter((user) => {
return info.formattedName.match(this.search);
})
},
async getList() {
try {
const list = await axios.get(`MyRequest`);
this.info = list.data;
this.info.sort(function(a, b) {
var nameA = a.formattedName.toUpperCase();
var nameB = b.formattedName.toUpperCase();
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
});
} catch (e) {
console.log(`getList`, e);
if (e.response.status === 400) {
this.error = e.response.data.message;
}
}
}
}
I am using the getList
function on render我在渲染上使用getList
function
created() {
this.getList();
}
I'd like some help on how I should handle this issue.我想要一些关于我应该如何处理这个问题的帮助。
1st, You have to remove the @input
on the textbox, Vue observability will trigger updates automatically. 1、您必须删除文本@input
上的@input,Vue 可观察性会自动触发更新。
2nd, filteredUsers() should be a computed
property, not a method.第二,filteredUsers() 应该是一个computed
属性,而不是一个方法。
HTML changes HTML 变化
Search: <input type="text" v-model="search" />
<base-table id="empTable" :data="filteredUsers" :columns="columns">
...
<template
slot-scope="{row}"
id="table">
Javascript Javascript
data() {
return {
columns: [],
info: [],
infoModal: "",
modal: false,
modalView: false,
refreshOnExit: 0,
error: "",
name: '',
search: " ",
};
},
computed: {
filteredUsers() {
if (!(this.info && this.info.length)) return []; //return empty if no info[]
return this.info.filter((user) => {
return user.match(this.search);
});
},
},
methods: {
async getList() {
try {
const list = await axios.get(`MyRequest`);
this.info = list.sort(function(a, b) {
var nameA = a.formattedName.toUpperCase();
var nameB = b.formattedName.toUpperCase();
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
});
} catch (e) {
console.log(`getList`, e);
if (e.response.status === 400) {
this.error = e.response.data.message;
}
}
}
},
created() {
this.getList();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.