angularjs - Angular 可编辑下拉列表 - 根据所选值进行编辑

标签 angularjs html dropdown

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

我找到了这个库:

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

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

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

请参阅 plunkr.co 中基于 repository 中的演示创建的工作示例

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 ( based in this answer ) 开发此示例代码:

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

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

最佳答案

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

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

指令采用options 数组和可选的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

关于angularjs - Angular 可编辑下拉列表 - 根据所选值进行编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41619490/

相关文章:

JavaScript - 构建复杂数据的对象

javascript - 使用 ng-view 时,AngularJS document.ready 不起作用

android - Html:通过 href 链接打开 ios 应用程序

jquery - Chrome 控制台 click() 不适用于 web.whatsapp 中的聊天列表

reactjs - 无法在 Primereact 下拉列表中解析 'react-transition-group'

javascript - ReactJS 中的语义 UI 下拉组件最大宽度

javascript - angularjs/signalR Bootstrap 进度条不随 Controller 更改而更新

javascript - 从nodejs + Ionic应用程序中index.html中的另一个文件夹导入js文件时出现404错误

html - 如何使 div 粘在没有包装的页面底部?

javascript - 获取字符串,但不是从下拉列表选项中选择的值