gpt4 book ai didi

javascript - Twitter TypeAhead.js 不更新输入

转载 作者:行者123 更新时间:2023-11-28 20:14:40 27 4
gpt4 key购买 nike

我正在使用Twitter's TypeAhead.js我的 bootstrap 3 项目上的库。它似乎很棒,但是,我的输入字段遇到了麻烦。一旦我开始填写,就会触发预输入并相应地出现建议框。然而,选择任何条目后,输入字段根本没有被填充。有人可以帮我吗?

这是我现在正在工作的代码:

HTML:

<input type="text" class="form-control typeahead" name="txtCliente" id="txtCliente" placeholder="Cliente ou Aterro de Destino" value="" maxlength="100" required>

Javascript 代码 - 激活字段:

$('.typeahead').typeahead({
name: 'parceiro',
remote: '../api/index.php/parceiro/busca/%QUERY',
template: [
'<p>{{RAZAO_SOCIAL}}</p>',
'<p>{{DESCRICAO_TIPOPARCEIRO}}</p>',
'<p>{{CNPJ}}</p>'
].join(''),
engine: Hogan
});

最后,从服务器返回的 JSON:

[{
"ID":"17",
"TIPOPARCEIRO":"C",
"DESCRICAO_TIPOPARCEIRO":"CLIENTE",
"RAZAO_SOCIAL":"VINICIUS SOARES BATISTA",
"CNPJ":"12344123213123"
}]

提前致谢!

[编辑]

感谢@bass-jobsen,我已经能够部署它了。正如他所说, secret 在于 TypeAhead 提供的 filter() 函数。下面是该函数的编写代码。

...

 $('#txtCliente').typeahead({                                
header: '<b>Clientes</b>',
template: [
'<span>{{RAZAO_SOCIAL}}<BR>{{CNPJ}}</span>',
].join(''),
limit: 3,
remote: {
url: '../api/index.php/parceiro/busca/%QUERY',
filter: function(parsedResponse) {
var dataset = [];
for(i = 0; i < parsedResponse.length; i++) {
if(parsedResponse[i].TIPOPARCEIRO == "C" || parsedResponse[i].TIPOPARCEIRO == "A"){
dataset.push({
ID: parsedResponse[i].ID,
RAZAO_SOCIAL: parsedResponse[i].RAZAO_SOCIAL,
DESCRICAO_TIPOPARCEIRO: parsedResponse[i].DESCRICAO_TIPOPARCEIRO,
CNPJ: parsedResponse[i].CNPJ,
value: parsedResponse[i].RAZAO_SOCIAL
});
}
}
return dataset;
},
},
engine: Hogan
})

...

最佳答案

您的数据集没有 value 属性(也缺少 tokens 属性)。

来自文档:

The individual units that compose datasets are called datums. The canonical form of a datum is an object with a value property and a tokens property. value is the string that represents the underlying value of the datum and tokens is a collection of single-word strings that aid typeahead.js in matching datums with a given query.

另请查看 http://twitter.github.io/typeahead.js/examples/ 上的“Twitter 开源项目”示例。这里返回的 json 是一个对象数组,例如:

 {
"name": "typeahead.js",
"description": "A fast and fully-featured autocomplete library",
"language": "JavaScript",
"value": "typeahead.js",
"tokens": [
"typeahead.js",
"JavaScript"
]
}

如果您无法更改 JSON 响应,您可以使用远程的过滤选项来构造有效数据:

$('.typeahead').typeahead({                                
name: 'is',
remote: {url:'database2.php?q=%QUERY', filter: function(data){

for (var i = 0; i < data.length; i++) {

data[i].value = data[i].RAZAO_SOCIAL;
data[i].tokens = new Array(data[i].RAZAO_SOCIAL,data[i].DESCRICAO_TIPOPARCEIRO);
}
return data;}

},
template: [
'<p>{{RAZAO_SOCIAL}}</p>',
'<p>{{DESCRICAO_TIPOPARCEIRO}}</p>',
'<p>{{CNPJ}}</p>'
].join(''),
filter: 'testfilter',
engine: Hogan
});

关于javascript - Twitter TypeAhead.js 不更新输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19387022/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com