简体   繁体   English

AngularJS:Bootstrap提前自定义突出显示

[英]Angularjs : Bootstrap typeahead customize highlight

I am using UI bootstrap type-ahead and I am matching based on the leading characters. 我正在使用UI Bootstrap预先输入,并且根据前导字符进行匹配。 For example if I type 'A' in the input box I would like to see all the states that start with 'A' as in "Alabama" and not all the states that contain an 'A' in the name. 例如,如果我在输入框中键入“ A”,我希望看到以“ A”开头的所有状态,如“阿拉巴马州”,而不是名称中所有包含“ A”的状态。

Now what I want to achieve is if I type 'A' in the input box in addition to filtering out all states that start with 'A', I want only the first A to be highlighted. 现在,我想要实现的是,除了在输入框中键入“ A”之外,还要过滤掉所有以“ A”开头的状态,我只希望突出显示第一个A。 If the word is "Alabama" it should highlight only the first "A" of "Alabama" and not other occurences of "A". 如果单词是“阿拉巴马州”,则应仅突出显示“阿拉巴马州”的第一个“ A”,而不突出显示“ A”的其他出现。 If I have the word 'Papa' in my list and I type 'Pa' in the input box it should highlight the first occurrence of the word 'Pa'in 'Papa'. 如果我的清单中有单词“爸爸”,并且在输入框中键入“ Pa”,则它应突出显示“爸爸”中单词“ Pa”的首次出现。

Here is my sample code 这是我的示例代码

myHtml.html myHtml.html

<div class='container-fluid' ng-controller="TypeaheadCtrl">
   <input type="text" ng-model="selected" typeahead="state for state in states | filter:$viewValue:startsWith">
</div>

myJs.js myJs.js

angular.module('ui.bootstrap.demo').controller('TypeaheadCtrl',   function($scope) {

  $scope.states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas'];

 $scope.startsWith = function(state, viewValue) {
        return state.substr(0, viewValue.length).toLowerCase() ==  viewValue.toLowerCase();
      } 
 });

There are two (or three maybe) options, depending on whether or not you're using a custom results template. 根据是否使用自定义结果模板,有两个(或三个)选项。

decorate the existing filter 装饰现有的过滤器

If using the built-in results template, you can override or decorate the existing filter used. 如果使用内置结果模板,则可以覆盖或装饰使用的现有过滤器。 Like so: 像这样:

 (function () { 'use strict'; angular.module('yourModuleName') .config(['$provide', '$injector', function($provide, $injector) { $provide.decorator('uibTypeaheadHighlightFilter', ['$delegate', function($delegate) { var extendsFilter = function() { var isSanitizePresent = $injector.has('$sanitize'); function escapeRegexp(queryToEscape) { // Regex: capture the whole query string and replace it with the string that will be used to match // the results, for example if the capture is "a" the result will be \\a return queryToEscape.replace(/([.?*+^$[\\]\\\\(){}|-])/, '\\\\$1'); } function containsHtml(matchItem) { return /<.*>/g.test(matchItem); } function matchFunction (matchItem, query) { if (!isSanitizePresent && containsHtml(matchItem)) { $log.warn('Unsafe use of typeahead please use ngSanitize'); // Warn the user about the danger } /* THIS LINE HERE IS THE ONLY LINE I CHANGED */ matchItem = query ? ('' + matchItem).replace(new RegExp(escapeRegexp(query), 'i'), '<strong>$&</strong>') : matchItem; // Replaces the capture string with a the same string inside of a "strong" tag if (!isSanitizePresent) { matchItem = $sce.trustAsHtml(matchItem); // If $sanitize is not present we pack the string in a $sce object for the ng-bind-html directive } return matchItem; } return matchFunction.apply(null, arguments); }; return extendsFilter; }]); }]); })(); 

Notice that the only change i made to the original filter source code was in the regex replace, right under where i marked /* THIS LINE HERE IS THE ONLY LINE I CHANGED */ 请注意,我对原始过滤器源代码所做的唯一更改是在regex替换中,在我标记为/ *的位置正下方,此行是我更改的唯一行* /

original source here, at the very bottom: https://github.com/angular-ui/bootstrap/blob/master/src/typeahead/typeahead.js 原始资源在最底部: https : //github.com/angular-ui/bootstrap/blob/master/src/typeahead/typeahead.js

Old Code: 旧代码:

 matchItem = query ? ('' + matchItem).replace(new RegExp(escapeRegexp(query), 'gi'), '<strong>$&</strong>') : matchItem; // Replaces the capture string with a the same string inside of a "strong" tag 

New Code (removed the regex global flag 'g'): 新代码(删除了正则表达式全局标志“ g”):

 matchItem = query ? ('' + matchItem).replace(new RegExp(escapeRegexp(query), 'i'), '<strong>$&</strong>') : matchItem; // Replaces the capture string with a the same string inside of a "strong" tag 


Use your own custom filter 使用您自己的自定义过滤器

If you are using a custom results template ( see examples' source code http://angular-ui.github.io/bootstrap/versioned-docs/2.4.0/#/typeahead ), you can just specify your own highlight filter in the custom results template, like so: 如果您使用的是自定义结果模板(请参见示例的源代码http://angular-ui.github.io/bootstrap/versioned-docs/2.4.0/#/typeahead ),则只需在以下位置指定自己的突出显示过滤器自定义结果模板,如下所示:

 <script type="text/ng-template" id="yourCustomResultsTemplate.html"> <a> <img ng-src="http://upload.wikimedia.org/wikipedia/commons/thumb/{{match.model.flag}}" width="16"> <span ng-bind-html="match.label | uibTypeaheadHighlightCustomizedByYou: query"></span> </a> </script> 

Then you would just copy/paste the original filter source code, and modify it in the same way as we did above. 然后,您只需复制/粘贴原始过滤器源代码,然后以与上述相同的方式对其进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM