代码之家  ›  专栏  ›  技术社区  ›  Mr world wide

AngularJS使用selectize和ng repeat设置multi select下拉列表的值

  •  0
  • Mr world wide  · 技术社区  · 6 年前

    JS Bin of pure html select tag

    但我用的是 选择插件
    https://selectize.github.io/selectize.js/
    对于下拉多选,我不知道如何填充已经选择的值。?

    :

    <div ng-repeat="item in items">
    
    <select  
    multiple 
    class="selectSkill"
    ng-model="item.storedArray"
    ng-options="opt.skillId as opt.skillName for opt in skills"
    ></select>
    
    </div>
    

    我的委托人:

    $scope.$selectSkill= $('.selectSkill').selectize({
        valueField: 'skillId',
        labelField: 'skillName',
        searchField: 'skillName',
        maxItems: 5,
        placeholder:"Select Skill",
        options: $scope.skills,
        create: false,
        sortField: {
            field: 'skillName',
            direction: 'asc'
        }
    });
    $scope.selectizeControlSkills = $scope.$selectSkill[0].selectize; 
    

    selectSkill ).

    如果没有填充,则正常加载可以工作,但我尝试使用selectize动态设置值。

    谢谢你的帮助。

    我的演示数据:

    **$scope.skills**
    

    enter image description here

    **item.storedArray**
    

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  1
  •   miqh    6 年前

    发布我创建的示例作为答案,因为它似乎有助于OP。

    ng-model ng-options 因为如果应用到同一个应用程序中,它们在功能上相互冲突 select

    如果您想使用Selectize,最好创建一个包装器(即一个指令,如我的示例中所示)来提供 选择 需要它的元素。

    scope.$apply()

    angular
      .module('app', [])
      .controller('ctrl', function ($scope) {
        $scope.items = [
          { storedArray: ['078...'] },
          { storedArray: [] },
          { storedArray: ['0cc...'] },
        ];
        $scope.skills = [
          { skillId: '078...', skillName: 'Java' },
          { skillId: '0cc...', skillName: 'Cisco UCS' },
        ];
        $scope.itemSelectizeOptions = $scope.items.map(function (item) {
          return {
            valueField: 'skillId',
            labelField: 'skillName',
            options: $scope.skills,
            items: item.storedArray,
            onChange: function (newItems) {
              item.storedArray = newItems;
              // `$apply()` needed because change occurs outside of AngularJS's knowledge
              $scope.$apply();
            }
          };
        })
      })
      .directive('selectize', function () {
        return {
          scope: {
            selectize: '<',
          },
          link: function (scope, el) {
            var selectize = $(el).selectize(scope.selectize);
          },
        };
      });
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/css/selectize.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.2/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/js/standalone/selectize.min.js"></script>
    <div ng-app="app" ng-controller="ctrl">
      <div ng-repeat="options in itemSelectizeOptions">
        <select
          multiple
          selectize="options"></select>
      </div>
      <pre>{{ items | json }}</pre>
    </div>