【问题标题】:AngularJS typeahead + multi select tagsAngularJS typeahead + 多选标签
【发布时间】:2014-05-06 18:19:25
【问题描述】:

我正在寻找的输入类似于 Gmail 的电子邮件地址预先输入

挑战:

1) 它应该同时显示 Name EmailAddress 和 Image(基本上是一个可自定义的模板)

2) 它应该显示添加到列表中的联系人的姓名

3) 它应该与退格一起使用以删除上一个条目

4) 它应该与 select 和 , 一起使用来添加新的 etnry

【问题讨论】:

标签: angularjs angularjs-directive multi-select typeahead


【解决方案1】:
.directive('typeahead', function () {
    return {
        restrict: 'AEC',
        scope: {
            model: '=ngModel'
        },
        link: function link($scope, $element, $attrs) {
            $scope.$watch('inputValue', function (value) {
                $scope.changed();
            });

            $scope.Emails = ['a@a.com', 'b@b.com', 'c@c.com'];

            $element.bind("keydown keypress", function (event) {
                switch (event.keyCode) {
                    case 40:
                        $scope.$apply(function () {
                            if ($scope.selected < $scope.List.length) {
                                $scope.selected++;
                            }
                        });
                        event.preventDefault();
                        break;
                    case 38:
                        $scope.$apply(function () {
                            if ($scope.selected > 0) {
                                $scope.selected--;
                            }
                        });
                        event.preventDefault();
                        break;
                    case 13:
                        $scope.$apply(function () {
                            $scope.selectAndClose($scope.List[$scope.selected]);
                        });
                        event.preventDefault();
                        break;
                    case 32:
                    case 188:
                        $scope.$apply(function () {
                            inputValues = $scope.inputValue.split(',');
                            for (var i = 0; i < inputValues.length; i++) {
                                if (inputValues[i].length > 0) {
                                    $scope.GetOrCreateEmailAndSelect(inputValues[i]);
                                }
                            }
                            $scope.clear();
                            $scope.close();
                        });
                        event.preventDefault();
                        break;
                    case 27:
                        $scope.$apply(function () {
                            $scope.close();
                        });
                        event.preventDefault();
                        break;
                    case 8:
                        $scope.$apply(function () {
                            if ($scope.inputValue == null || $scope.inputValue.length == 0) {
                                $scope.model.pop();
                            }
                        });
                        break;
                }
            });

            $scope.remove = function (emailid) {
                $scope.model.splice($scope.model.indexOf(emailid), 1);
            }

            $scope.changed = function () {
                fetchEmail({
                    'EmailAddress__icontains': $scope.inputValue
                }).then(function (data) {
                    $scope.List = data;
                    if (typeof ($scope.model) === typeof ([]) && $scope.model !== null) {
                        for (var i = 0; i < $scope.model.length; i++) {
                            for (var j = 0; j < $scope.List.length; j++) {
                                if ($scope.List[j].id == $scope.model[i].id) {
                                    $scope.List.splice(j, 1);
                                }
                            }
                        }
                    }
                    $scope.selected = 0;
                    dropdownShow = false;
                    if ($scope.List.length > 0) {
                        if (typeof ($scope.inputValue) !== 'undefined' && $scope.inputValue !== null) {
                            if ($scope.inputValue.length > 1) {
                                dropdownShow = true;
                            }
                        }
                    }
                    $scope.dropdownShow = dropdownShow;
                });
            };

            $scope.selectAndClose = function (value) {
                $scope.select(value);
                $scope.clear();
                $scope.close();
            };

            $scope.select = function (value) {
                $scope.model.push(value);
            };
            $scope.clear = function () {
                $scope.inputValue = null;
            };
            $scope.close = function () {
                $scope.dropdownShow = false;
            };
            $scope.GetOrCreateEmailAndSelect = function (EmailAddress) {
                EmailAddress = EmailAddress.toString();
                data = $scope.fetchEmail(EmailAddress); //you can add an ajax call here
                if (data.length == 0) {
                    $scope.CreateEmail(EmailAddress);
                } else {
                    $scope.select(data[0]);
                }
            });

        $scope.fetchEmail =function(EmailAddress) {
            result = [];
            for (var i = 0; i < $scope.Emails.length; i++) {
                if ($scope.Emails[i].indexOf(EmailAddress) > -1) {
                    result.push($scope.Emails[i]);
                }
            }
        }

        $scope.CreateEmail =function(EmailAddress) {
            $scope.Emails.push(EmailAddress);
        };
    }
    $scope.mouseoverChoice = function (emailidobject) {
        $scope.selected = $scope.List.indexOf(emailidobject);
    };
    $scope.editEmailId = function (emailidobject) {
        $scope.inputValue = emailidobject.EmailAddress;
        $scope.remove(emailidobject);
    }
    $scope.CheckSelected = function (element) {
        if (typeof ($scope.List) !== 'undefined' && typeof ($scope.selected) !== 'undefined' && typeof ($scope.List[$scope.selected]) !== 'undefined') {
            return $scope.List[$scope.selected].id == element.id;
        } else {
            return false;
        }
    }
},
    templateUrl: 'typeaheadtemplate.html',
}
});

【讨论】:

  • 你试过 Angular-UI 的 Typehead 和自定义模板吗?
  • 嘿,我有,但它不支持我所说的所有功能。尤其不是自定义键方法,也不是标签。我需要分叉它并进行大量更改。
  • @sj7,不知道这是否是一个工作示例,但 plkr 给出了很多错误,现在我清理了这些错误,但仍然试图弄清楚应该如何设置才能得到这个在职的。这是我到目前为止所拥有的:plnkr.co/edit/0Nm4Wf?p=preview
  • @Kiwi,你说得对,这不是一个可行的例子。我实际上在我的一个项目中使用过它,并没有花时间让它独立运行。让我在这周的某个时候试一试。将尝试使其独立运行。
  • @sj7 这是我使用的插件:mbenford.github.io/ngTagsInput 但需要对 CSS 部分进行大量调整:p
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-05
  • 2016-02-18
相关资源
最近更新 更多