4

我的 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 日知道如何在搜索结果中显示名字和姓氏吗?

4

3 回答 3

5

您可以尝试对预先输入使用自定义函数,而不是使用过滤器:

<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;

警告:我尚未测试此代码/从我的项目中将其拉出,因此可能存在一两个错误,需要进行一些调整才能修复。

于 2014-09-08T04:30:12.317 回答
3

要在搜索结果中同时显示名字和姓氏,请将 michael0x2a 的输入标记修改为:

 <input type="text" 
       ng-model="result" 
       typeahead="patient as patient.first_name + ' ' + patient.last_name for patient in filterByName(ngGridUsersData.patients, $viewValue)" 
       typeahead-template-url="customTemplate.html">
于 2015-01-13T11:08:27.367 回答
0

AngularJS UI Bootstrap – Typeahead – 按多个属性过滤:

“uib-typeahead”属性是您放置要过滤的字段的位置。

uib-typeahead="patient as (patient.first_name + ' ' + patient.last_name) for patient in $ctrl.patients | filter:$viewValue | limitTo:8"
于 2016-11-22T11:40:36.617 回答