【发布时间】:2016-02-03 09:26:40
【问题描述】:
我在将搜索结果重定向到我想要显示结果的页面时遇到问题。希望在标题中有搜索栏,以便我的结果显示在结果页面或类似内容上。一切都是通过教程(Laracasts)完成的,还在学习 javascript,所以我有点卡在这里.. 一些帮助会很棒!
index.blade.php
<script src="http://code.jquery.com/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.11.1/typeahead.bundle.min.js"></script>
<script src="http://cdn.jsdelivr.net/algoliasearch/3/algoliasearch.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/0.12.1/vue.js"></script>
<script>
new Vue({
el: 'body',
data: {
query: '',
users: []
},
ready: function () {
this.client = algoliasearch('', '');
this.index = this.client.initIndex('test_drive_contacts');
$('#typeahead').typeahead(null, {
source: this.index.ttAdapter(),
displayKey: 'firstname',
templates: {
suggestion: function (hit) {
return '<div>' +
'<h4 class="high">' + hit._highlightResult.firstname.value + '</h4>' +
'<h5 class="high">' + hit._highlightResult.company.value + '</h5>'
+ '</div>';
}
}
})
.on('typeahead:select', function (e, suggestion) {
this.query = suggestion.firstname;
}.bind(this));
},
methods: {
search: function () {
this.index.search(this.query, function (error, results) {
this.users = results.hits;
}.bind(this));
}
}
});
</script>
这是搜索输入:
<input id="typeahead" type="text" class="form-control" v-model="query"
v-on="keyup: search | key 'enter'">
<article v-repeat="user: users">
<h2>@{{ user.firstname }}</h2>
<h4>@{{ user.company }}</h4
</article>
【问题讨论】:
-
对不起,我不明白你的问题。你介意澄清一下吗?
-
当我输入搜索查询时,如何重定向以便结果显示在我想要的页面中。现在它显示在我的搜索输入下。不知道如何在我们称之为“结果页面”上重定向结果。希望我现在解释得更好..
-
我仍然不确定你在寻求什么帮助。在 JavaScript 中重定向就像
window.location = url一样简单。您的代码已经使用了typeahead:select事件,这是您想要重定向的地方。 -
我该如何写这个?那是我的问题,我不太了解语法。我只想在页面中显示结果,而不是在输入搜索下。
标签: javascript typeahead.js vue.js algolia