角度可编辑下拉菜单 - 根据所选值进行编辑

2023-11-29

更新1:开发了第一个示例代码,为正确实现奠定了基础。
更新 2:开发了一个工作模型。查看答案。

我找到了这个库:

https://libraries.io/bower/editable-dropdown-angularjs

它允许使用 HTML5 数据列表功能添加可编辑的下拉列表。

它工作正常,但是,唯一需要的功能是仅当所选值为“其他”时才使字段可编辑。

请参阅 plunkr.co vreated 中基于演示的工作示例存储库

http://plnkr.co/edit/wDm2mbTqTsT1YC5H7UPy?p=preview

有关详细信息,请参阅下面的示例代码。

感谢您的建议,仅当所选值为“其他”时才使下拉字段可编辑。

HTML5:

<div ng-app="myApp">
    <div ng-controller="demo" style="width:300px;position:fixed;top:20px;left:20px">    
        <p>You selected {{selected}}</p>
        <editable-dropdown options='list' ng-model='selected'></editable-dropdown>
    </div>
</div>

JavaScript:

angular.module('myApp', ['editableDropdown'])
.controller('demo', function($scope){
    $scope.list = ['one', 'two', 'other']
    $scope.selected;
});

我能够使用 jsfiddle 开发此示例代码(基于这个答案):

http://jsfiddle.net/tarekahf/t392djx1/

如果选择“其他”,这将允许使下拉列表可编辑。现在,我正在将此模式转换为 Angular 方式。如果您有任何建议,请告诉我。


似乎您的问题没有引起太多关注,正如我评论的那样,您(仍然)最好编写自己的实现,而不是试图强制editable-dropdown-angular满足您的需求。

无论如何,我冒昧地写了自己的editable-select为您提供指导。

指令采用数组options可供选择和可选other字符串,这是用户可编辑选择的默认值。选择时禁用编辑options, while other可以自由修改。

希望你觉得它有用。

应用程序 HTML 模板

<body ng-controller="Ctrl as vm">
  <!-- directive -->
  <editable-select 
    ng-model="vm.selected" 
    options="vm.options" 
    other="Other"> <!-- write "other" here or assign var in controller -->
  </editable-select>

  <hr>
  <span>User selected: {{ vm.selected }}</span>
</body>

应用JavaScript

angular
.module('app', [])    
.controller('Ctrl', function() {
  var vm = this;
  vm.options = ['One', 'Two']; // selection options
})    
.directive('editableSelect', function() {
  return {
    restrict: 'E',
    require: '^ngModel',
    scope: {
      ngModel: '=',
      options: '=',
      other: '@'
    },
    replace: true,
    templateUrl: 'editable-select-tpl.html', 
    link: function(scope, element) {
      scope.isDisabled = true;

      // option clicked handler    
      scope.click = function(option) {
        scope.ngModel = option;
        scope.isDisabled = !scope.other || scope.other !== option;
        if (!scope.isDisabled) {
          element[0].querySelector('.editable-select').focus();
        }
      };

      // option typed handler          
      var unwatch = scope.$watch('ngModel', function(val) {
        if (!scope.isDisabled) {
          scope.other = scope.ngModel;
        }
      });

      // release watcher          
      scope.$on('$destroy', unwatch);
    }
  };
}); 

指令 HTML 模板 (editable-select-tpl.html)

<div>
  <div class="input-group dropdown">
    <input name="editable-select" 
           type="text" 
           class="form-control dropdown-toggle editable-select" 
           ng-disabled="isDisabled" 
           ng-model="ngModel">
    <ul class="dropdown-menu">
      <li ng-repeat="option in options" 
          ng-bind="::option" 
          ng-click="click(option)">
      </li>
      <li ng-if="other" role="presentation" class="divider"></li>
      <li ng-if="other" ng-bind="other" ng-click="click(other)"></li>
    </ul>
    <span class="input-group-addon dropdown-toggle" data-toggle="dropdown">
      <span class="caret"></span>
    </span>
  </div>
  <span class="small text-muted" ng-show="!isDisabled">Type in your selection</span>
</div>

CSS

input[name="editable-select"]:disabled {
  background-color: #ffffff;
}

.dropdown-menu:hover {
  cursor: pointer;
}

.dropdown-menu li {
  padding-left: 10px;
}

.dropdown-menu li:hover {
  background-color: #eeeeee;
}

enter image description here

相关的笨蛋在这里https://plnkr.co/edit/7bVgDW

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

角度可编辑下拉菜单 - 根据所选值进行编辑 的相关文章

随机推荐