[英]typeahead.js dynamic suggestion
I'm wondering if there's a way for me to provide dynamic suggestions based on a user's input using Twitter's typeahead.js library in conjunction with the bloodhound library. 我想知道是否有一种方法可以结合Twitter的typeahead.js库和Bloodhound库,根据用户输入提供动态建议。 I cannot figure out how to create a dynamic suggestion
without completely duplicating the code. 我无法弄清楚如何在不完全复制代码的情况下创建动态suggestion
。
Here's my code: 这是我的代码:
/* Bloodhound for main search window */
partsList = new Bloodhound({
datumTokenizer: function (datum) {
return Bloodhound.tokenizers.whitespace(datum.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: 'pSearch.php?p=%QUERY',
wildcard: '%QUERY',
minLength: 2,
filter: function (partsList) {
return $.map(partsList, function (itemNumber) {
return {
itemOneHeader : itemNumber.header_one,
itemTwoHeader : itemNumber.header_two,
itemThreeHeader : itemNumber.header_three,
itemOne : itemNumber.one,
itemTwo : itemNumber.two,
itemThree : itemNumber.three
};
});
}
},
limit: 15
});
/* Main search box typeahead */
$('.mainSearchBox').typeahead(
{ hint: false,
name: 'mainSearchBox',
highlight: true,
minLength: 2
},
{
displayKey: 'itemOne',
templates: {
suggestion: Handlebars.compile("<div><strong>{{itemOne}}</strong> <div class='mfr_suggestion'>{{itemTwo}} > {{itemThree}}</div></div>")
},
source: partsList.ttAdapter(),
limit: 15}).on('typeahead:selected', function (obj, datum) {
console.log(datum);
iOne = datum.itemOne;
iTwo = datum.itemTwo;
iThree = datum.itemThree;
iOne_header = datum.itemOneHeader;
iTwo_header = datum.itemTwoHeader;
iThree_header = datum.itemThreeHeader;
var d = document.getElementById("search");
d.className = "fa fa-2x fa-check-square uCheck fa-happy";
headerOne = iOne_header;
headerTwo = iTwo_header;
headerThree = iThree_header;
$('#headerOne').val(headerOne);
$('#headerTwo').val(headerTwo);
$('#headerThree').val(headerThree);
})
.on('typeahead:asyncrequest', function() {
$('#loadingCogs').html('<i class="fa fa-circle-o-notch fa-3x fa-spin"></i>');
})
.on('typeahead:asynccancel typeahead:asyncreceive', function() {
$('#loadingCogs').html('');
});
Typeahead begins by using bloodhound to send a query off to pSearch.php
, and it returns 6 items from that page to typeahead, which displays some of those to the user via the dropdown box, with this code: Typeahead首先使用pSearch.php
将查询发送到pSearch.php
,然后从该页面将6项返回给pSearch.php
,然后通过下拉框向用户显示其中的一些内容,并带有以下代码:
templates: {
suggestion: Handlebars.compile("<div><strong>{{itemOne}}</strong> <div class='suggestion'>{{itemTwo}} > {{itemThree}}</div></div>")
},
What I'm aiming to do is something like this: 我打算做的是这样的:
templates: {
if ( itemOne == "a" ) {
suggestion: Handlebars.compile("<div><strong>{{itemOne}}</strong> <div class='suggestion'>{{itemTwo}} > {{itemThree}}</div></div>")
} else {
suggestion: Handlebars.compile("something else...")
}
},
which I obviously cannot do because I can't add an if
statement here. 我显然做不到,因为我不能在此处添加if
语句。 Is there a way to accomplish what I'm trying to do without duplicating the entire function? 有没有一种方法可以完成我要完成的工作而又不重复整个功能? I can echo a PHP session variable that will allow me to select the correct code prior to user starting to type in the input box, but I feel like this makes my page unnecessarily long if there's a shorter way to do this... 我可以回显一个PHP会话变量,该变量将允许我在用户开始在输入框中键入内容之前选择正确的代码,但是我觉得如果有更短的方法可以使我的页面不必要地冗长...
The only way I've thought might work is by using a ternary operator, but this does not work because itemOne
is not defined until bloodhound defines it: 我认为唯一可行的方法是使用三元运算符,但这无法正常工作,因为在itemOne
定义itemOne
之前,未定义itemOne
:
suggestion: Handlebars.compile( itemOne == "a" ? "<div><strong>{{itemOne}}</strong> <div class='suggestion'>{{itemTwo}} > {{itemThree}}</div></div>" : "something else..." )
So - using the already-established PHP session variable I mentioned in my question - I was able to do this using a ternary operation, without having to duplicate the entire function: 所以-使用我在问题中提到的已经建立的PHP会话变量-我能够使用三元操作来做到这一点,而不必重复整个函数:
/* before I initialize anything related to typeahead... */
php_session_variable = = <?php echo json_encode($_SESSION['abc']);?>;
suggestion: Handlebars.compile( php_session_variable == "a" ? "<div><strong>{{itemOne}}</strong></div>" : "<div><strong>{{itemOne}}</strong> <div class='suggestion'>{{itemTwo}} > {{itemThree}}</div></div>" )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.