Angular ui-select tagging lose text input on blur

梦想的初衷 提交于 2019-12-05 08:23:06

I have forked the ui-select and enabled this functionality by adding a tag-on-blur='true' to the ui-select directive in your html. If you want you can use my repository while I wait for my pull request to be merged.

https://github.com/mattmcardle/ui-select/tree/tag_on_blur

If you were to use my fork and wanted to enable tagging on blur, your HTML code would look like this:

<h3>Array of objects</h3>
<ui-select multiple tagging tagging-label="new tag" tag-on-blur="true" ng-model="multipleDemo.selectedPeople" theme="select2" ng-disabled="disabled" style="width: 800px;">
  <ui-select-match placeholder="Select person...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match>
  <ui-select-choices repeat="person in people | propsFilter: {name: $select.search, age: $select.search}">
<div ng-bind-html="person.name | highlight: $select.search"></div>
<small>
  email: {{person.email}}
  age: <span ng-bind-html="''+person.age | highlight: $select.search"></span>
</small>
</ui-select-choices>
</ui-select>
<p>Selected: {{multipleDemo.selectedPeople}}</p>

Just create a directive. This handles the click outside tab and enter.

angular.module('module')
.directive('tagOnBlur', function($timeout) {
  return {
    require: 'uiSelect',
    link: function(scope, elm, attrs, ctrl) {

        scope.isTab = false;

        ctrl.searchInput.bind("keydown keypress", function (event) {
            if(event.which === 9 || event.which === 13) {
                event.preventDefault();
                scope.isTab = true;
            }
        });

        ctrl.bind('click', function (event) {
            scope.isTab = true;
        });

        ctrl.searchInput.on('blur', function() {
            if (scope.isTab){
                scope.isTab = false;
                return;
            }
            if ((ctrl.items.length > 0 || ctrl.tagging.isActivated)) {
                $timeout(function() {
                    ctrl.searchInput.triggerHandler('tagged');
                    var newItem = ctrl.search;
                    if ( ctrl.tagging.fct ) {
                        newItem = ctrl.tagging.fct( newItem );
                    }
                    if (newItem) ctrl.select(newItem, true);
                });
            }
        });
    }
  };
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!