简体   繁体   English

Bootstrap UI中的预输入值

[英]Typeahead Value in Bootstrap UI

I'm working on an app using AngularJS and Bootstrap UI. 我正在使用AngularJS和Bootstrap UI开发应用程序。 I've been fumbling my way through using the Typeahead control in Bootstrap UI. 我一直在尝试通过Bootstrap UI中的Typeahead控件。

Here's my Plunker 这是我的笨蛋

My challenge is I want the user to have the option of choosing an item, but not required to do so. 我的挑战是我希望用户可以选择一个项目,但不是必须这样做。 For instance, right now, if you type Test in the text field and press "Enter", Test will be replaced with Alpha . 举例来说,现在,如果你键入Test在文本字段,然后按“Enter”键, Test将被替换Alpha However, I really want to use Test . 但是,我真的很想使用Test The only time I want the text to be replaced is when someone chooses the item from the drop down list. 我唯一要替换的文本是有人从下拉列表中选择项目时。 My markup looks like the following: 我的标记如下所示:

<input type="text" class="form-control" placeholder="Search..."
       ng-model="query"  
       typeahead="result as result.name for result in getResults($viewValue)"
       typeahead-template-url="result.html" />

How do I give the user the option of choosing an item, but allow them to still enter their own text? 如何为用户提供选择项目的选项,但仍允许他们输入自己的文本?

The issue is that both Enter and Tab confirm the selection of the currently highlighted item and Typeahead automatically selects an item as soon as you start to type. 问题是EnterTab都确认了当前突出显示的项目的选择,而Typeahead会在您开始键入时自动选择一个项目。

If you want, you can click off the control to lose focus or hit Esc to exit out of typeahead, but those might be difficult to communicate to your users. 如果需要,您可以单击控件以失去焦点,或单击Esc以退出快速输入,但是这些操作可能很难与您的用户进行交流。

There's an open request in Bootstrap Ui to not auto select / highlight the first item Bootstrap Ui中有一个打开请求,要求不要自动选择/突出显示第一个项目

One solution is to populate the first item with the contents of the query thus far , so tabbing or entering will only confirm selection of the current query: 一种解决方案是使用到目前为止的查询内容填充第一项 ,因此制表符或输入将仅确认选择当前查询:

JavaScript : JavaScript

angular.module('plunker', ['ui.bootstrap'])
    .filter('concat', function() {
        return function(input, viewValue) {
        if (input && input.length) {
            if (input.indexOf(viewValue) === -1) {
                input.unshift(viewValue);
            }
            return input;
         } else {
            return [];
    }};})

HTML : HTML

<input type="text" 
     ng-model="selected" 
     typeahead="state for state in states | filter:$viewValue | limitTo:8 | concat:$viewValue"
     class="form-control">

Demo in Plunker 在Plunker中的演示

I came across this same situation and found no good answers so I implemented it myself in ui-bootstrap Here is the relevant answer. 我遇到了同样的情况,没有找到好的答案,所以我自己在ui-bootstrap 是相关的答案。 This is probably not the best route to take, but it does get the job done. 这可能不是最佳方法,但确实可以完成工作。 It makes the first result in the typeahead to be what you're currently typing, so if you tab or enter off of it, it's selected -- you must arrow-down or select another option to get it. 它使预输入中的第一个结果成为您当前正在键入的内容,因此,如果您对其进行制表或输入,则将其选中-必须向下箭头或选择其他选项才能获得它。

Here is the modified ui-bootstrap-tpls.js file 是修改后的ui-bootstrap-tpls.js文件

I added a mustMouseDownToMatch property/attribute to the directive, like: 我向指令添加了mustMouseDownToMatch属性/属性,例如:

<input type="text" ng-model="selected" typeahead="item for item in typeaheadOptions | filter:$viewValue" typeahead-arrow-down-to-match="true">

And the javascript: 和javascript:

var mustArrowDownToMatch = originalScope.$eval(attrs.typeaheadArrowDownToMatch) ? originalScope.$eval(attrs.typeaheadArrowDownToMatch) : false;

I also added this function which will put the current text into the first item of the typeahead list, and make it the selected item: 我还添加了此功能,该功能会将当前文本放入预输入列表的第一项,并将其作为选定项:

var setFirstResultToViewValue = function (inputValue) {
        scope.matches.splice(0, 0, {
        id: 0,
        label: inputValue,
        model: inputValue
    });
}

And that is called in the getMatchesAsync call in the typeahead directive: 这在typeahead指令的getMatchesAsync调用中被调用:

var getMatchesAsync = function(inputValue) {
// do stuff
    $q.when(parserResult.source(originalScope, locals)).then(function(matches) {
        // do stuff
        if (matches.length > 0) {
             // do stuff
        }
        if (mustArrowDownToMatch) {
            setFirstResultToViewValue(inputValue);
            scope.activeIdx = 0;
            setTypeaheadPosition();
        }
        // do stuff
  };

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

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