Ajax呼び出しから取得するテキストボックスに入力するときにリストをフィルタリングしようとしています。この問題は、Ajaxの準備が整う前にフィルタが適用されているようです。Vue検索入力時にAjax応答配列
HTML:
<input type="text" class="form-control" v-model="searchTerm">
<table>
<tr v-for="food in filteredItems">
<td>{{ food.name }}</td>
<td>{{ food.energy }}</td>
</tr>
</table>
ヘルパー/ index.js:
export default {
getFoods() {
return Vue.http.get('http://localhost:3000/foods/allfoods')
.then((response) => {
return response.data;
});
}
}
Vueのコンポーネント:
import helpers from '../helpers'
export default {
name: 'Search',
mounted() {
helpers.getFoods().then((response) => {
this.foodData = response;
});
},
data() {
return {
searchTerm: '',
foodData: [],
}
},
computed: {
filteredItems() {
return this.foodData.filter(function(food){return food.name.toLowerCase().indexOf(this.searchTerm.toLowerCase())>=0;});
}
}
私はページをロードしたり、私が
を取得する入力を開始'TypeError: undefined is not an object (evaluating 'this.searchTerm')'.
foodData配列をハードコードするとすべてが正常に動作します。
私は何か誤解していましたか、何が間違っていますか?
2つの質問:1. 'filteredItems'の' this'参照を修正しようとしましたか? 2.どのように 'searchTerm'を設定しますか?私はあなたのテンプレートでそれを見ていないよ。 –
ここに忘れてしまったHTMLにの不足を追加しました。あなたはこの参照を修正することで何を意味するのか説明できますか? –
バートは言った;) –