如何向fetchRestaurants()传参queryString
时间: 2024-11-29 20:38:45 浏览: 5
JavaScript QueryString解析类代码
在当前代码中,并没有定义 `fetchRestaurants` 函数,而是使用了 `querySearchAsync` 方法来处理异步搜索建议。如果你希望向 `querySearchAsync` 方法传递 `queryString` 参数,可以通过以下方式实现:
### 1. 定义 `fetchRestaurants` 方法
首先,可以在 `methods` 中定义一个 `fetchRestaurants` 方法,该方法接受 `queryString` 参数并调用 `querySearchAsync`。
```javascript
methods: {
// 其他方法...
fetchRestaurants(queryString) {
this.querySearchAsync(queryString, (results) => {
// 处理返回的结果
console.log(results);
});
},
querySearchAsync(queryString, cb) {
var restaurants = this.restaurants;
console.log(restaurants)
var results = queryString ? restaurants.filter(this.createStateFilter(queryString)) : restaurants;
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
cb(results);
}, 2000 * Math.random());
},
// 其他方法...
}
```
### 2. 调用 `fetchRestaurants` 方法
然后,在需要的地方调用 `fetchRestaurants` 方法,并传递 `queryString` 参数。
例如,假设你在某个按钮点击事件中调用 `fetchRestaurants`:
```html
<button @click="fetchRestaurants('920113')">Fetch Restaurants</button>
```
### 3. 完整示例
以下是完整的代码示例,包括定义 `fetchRestaurants` 方法和在按钮点击事件中调用它:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="../static/element-ui/lib/theme-chalk/index.css">
<link rel="stylesheet" href="../static/bootstrap/dist/css/bootstrap.css">
<script src="../static/jquery/dist/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../static/bootstrap/dist/js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="panel panel-primary">
<div class="panel-heading" style="text-align: center;" id="title">����ƻ�������ͳ��ϵͳ</div>
<div class="panel-body"></div>
<br>
<div id="app">
<template>
<el-autocomplete v-model="state" :fetch-suggestions="querySearchAsync" placeholder="����������" @select="handleSelect" clearable @clear="handleClear"></el-autocomplete>
</template>
<el-descriptions title="��������" :column="10" border style="font-size: medium;">
<el-descriptions-item label="س Gratuit"> 920113 </el-descriptions-item>
<el-descriptions-item label="����"><el-tag type="danger" style="font-size: larger;">���Ѿ�</el-tag></el-descriptions-item>
<el-descriptions-item label="������ʽ"><span class="label label-warning" style="font-size: larger;">��Ƥ���������</span></el-descriptions-item>
<el-descriptions-item label="����ҽʦ"><el-tag>����</el-tag></el-descriptions-item>
<el-descriptions-item label="����"><el-tag style="font-size: larger;">2024��11��6��14:29:07</el-tag-success></el-descriptions-item>
<el-descriptions-item label="����"><el-tag style="font-size: large;color: red;">2024-11-6 14:29:12</el-tag></el-descriptions-item>
</el-descriptions>
<hr>
<div style="background-color:lightskyblue;font-size: large;"><strong>������ͳ��</strong></div>
<template>
<el-table :data="tableData" style="width: 100%">
<!-- 表格列定义 -->
</el-table>
</template>
<button @click="fetchRestaurants('920113')">Fetch Restaurants</button>
</div>
</div>
<script src="../static/vue/dist/vue.min.js"></script>
<script src="../static/element-ui/lib/index.js" type="text/javascript" charset="utf-8"></script>
<script>
let arr = [
{ "value": "920113", "address": "����������·144��" }
]
var app = new Vue({
el: '#app',
data: {
tableData: [
{ date: '2016-05-02', name: '��С��', address: '�Ϻ�����������ɳ��· 1518 Ū', editing: false },
{ date: '2016-05-04', name: '��С��', address: '�Ϻ�����������ɳ��· 1517 Ū', editing: false },
{ date: '2016-01', name: '��С��', address: '�Ϻ�����������ɳ��· 1519 Ū', editing: false },
{ date: '2016-05-03', name: '��С��', address: '�Ϻ�����������ɳ��· 1516 Ū', editing: false }
],
restaurants: [],
state: '',
timeout: null
},
methods: {
fetchRestaurants(queryString) {
this.querySearchAsync(queryString, (results) => {
// 处理返回的结果
console.log(results);
});
},
querySearchAsync(queryString, cb) {
var restaurants = this.restaurants;
console.log(restaurants)
var results = queryString ? restaurants.filter(this.createStateFilter(queryString)) : restaurants;
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
cb(results);
}, 2000 * Math.random());
},
createStateFilter(queryString) {
return (state) => {
return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
};
},
handleSelect(item) {
console.log(item);
},
handleClear() {
this.state = '';
},
handleEdit(index, row) {
this.$set(this.tableData[index], 'editing', true);
},
handleSave(index, row) {
this.$set(this.tableData[index], 'editing', false);
this.$message.success('����ɹ�');
},
handleDelete(index, row) {
this.tableData.splice(index, 1);
this.$message({ type: 'error', message: 'ɾ���ɹ�', duration: 2000 })
}
}
});
</script>
</body>
</html>
```
这样,当你点击按钮时,`fetchRestaurants` 方法会被调用,并将 `queryString` 参数传递给 `querySearchAsync` 方法。
阅读全文