gpt4 book ai didi

javascript - 在 AngularJS 中使用 Bootstrap Typeahead 在两个字段中搜索

转载 作者:搜寻专家 更新时间:2023-11-01 05:11:29 27 4
gpt4 key购买 nike

我的 JSON 中有两个字段:first_namelast_name

我想使用 Bootstrap typeahead.js,使用 Angular-UI。

<script type="text/ng-template" id="customTemplate.html">
<a>
<span bind-html-unsafe="match.label | typeaheadHighlight:query"></span>
</a>
</script>
<input type="text" ng-model="result" typeahead="patient as patient.first_name for patient in ngGridUsersData.patients | filter:{first_name:$viewValue}" typeahead-template-url="customTemplate.html">

我应该在 typeahead 字段中添加什么以在 last_name 中也进行搜索?我应该在模板中添加什么以在选择时同时显示名字和姓氏?

如果 angular-ui 指令没有这个功能,那么我想我会把名字和姓氏连接成一个字符串。

更新: 2014 年 9 月 15 日 知道如何在搜索结果中显示名字和姓氏吗?

最佳答案

您可以尝试为 typeahead 使用自定义函数,而不是使用过滤器:

<input type="text" 
ng-model="result"
typeahead="patient as patient.first_name for patient in filterByName(ngGridUsersData.patients, $viewValue)"
typeahead-template-url="customTemplate.html">

// ...snip...

function filterByName(patients, typedValue) {
return patients.filter(function(patient) {
matches_first_name = patient.first_name.indexOf(typedValue) != -1;
matches_last_name = patient.last_name.indexOf(typedValue) != -1;

return matches_first_name || matches_last_name;
});
}
$scope.filterByName = filterByName;

警告:我没有测试过这段代码/从我的项目中提取它,因此可能存在一两个错误,需要进行一些调整才能修复。

关于javascript - 在 AngularJS 中使用 Bootstrap Typeahead 在两个字段中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25717408/

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