javascript - 用于多选的 AngularJS 指令

标签 javascript angularjs twitter-bootstrap multi-select directive

我在填充多选时遇到问题。我用的是这个版本的多选http://davidstutz.github.io/bootstrap-multiselect/

我查看了这个堆栈溢出页面 ( How can I use Bootstrap Multiselect Dropdown in AngularJS ),但我仍然遇到问题。我正在尝试使用从存储在 provData 中的数据库中获取的数据来填充我的多选。

这是我的 html:

<div class="col-sm-8">
    <select class="form-control" multiple ht-multi-select ng-model="patient.provid" ng-options="prov.id as prov.code for prov in provData">
        <option value="{{prov.id}}">{{prov.code}}</option>
    </select>
</div>

这是我的指令:

templates.directive('htMultiSelect', function() {
return {
  replace: true,
  require: 'ngModel',
  scope: {},
  link:function(scope, element, attrs) {
    console.log($(element));
    console.log('hit');
      // Below setup the dropdown:

      $(element).multiselect({
        enableClickableOptGroups: true
      });

      // Below maybe some additional setup
      scope.$watch(attrs.ngModel, function () {
       $(element).multiselect('refresh');
      });
  }
};
});

我的主要问题是我无法使用来自 provData 的数据填充我的多选,而且我无法使用它来设置 ng-model。任何帮助将不胜感激。

最佳答案

根据 Rashim 的博客,这是我在 Plunke 中尝试的方法。 https://rashimuddin.wordpress.com/2014/07/08/multi-select-drop-down-in-angularjs/

看看这个 http://plnkr.co/edit/y8VOlgeSMOqHjFxoZU1L?p=preview 在这里输入代码

HTML

 <div dropdown-multiselect="" items="FirstItems" on-close="closeAlert(val)" on-Validation="addAlert(validationMsg)" max-Selection="3" selected-items="FirstSelectedItems"></div>

Angular Directive(指令)将是

var app = angular.module('myApp', ['ui.bootstrap']);
app.controller('AppCtrl', ["$scope", function($scope) {

$scope.status = {
  isopen: false
};

$scope.FirstItems = [];

$scope.alerts = [{
  type: 'danger',
  msg: 'Oh snap! Change a few things up and try submitting again.'
}, {
  type: 'success',
  msg: 'Well done! You successfully read this important alert message.'
}];

$scope.addAlert = function(validationMsg) {
  if ($scope.validateFire === true) {
    $scope.alerts.push({
      msg: validationMsg
    });
  }
  $scope.validateFire = true;
};

$scope.closeAlert = function(val) {
  $scope.alerts = [];
}

for (var i = 0; i <= 20; i++) {
  var obj = [];
  obj["Id"] = i;
  obj["Name"] = "Name" + i;
  obj["IsSelected"] = false;
  $scope.FirstItems.push(obj);
}

$scope.FirstSelectedItems = [];

var removeItem = function(items, item) {
  for (var index = 0; index < items.length; index++) {
    if (item.Id == items[index].Id) {
      item.IsSelected = false;
      items.splice(index, 1);
      break;
    }
  }
};

$scope.removeFirstItem = function(item) {
  removeItem($scope.FirstSelectedItems, item);
};
$scope.removeSecondItem = function(item) {
  removeItem($scope.SecondSelectedItems, item);
};}]);

指令是这样的

app.directive('dropdownMultiselect', function() { 返回 { 限制:'A', 范围: { 项目:“=”, 所选项目:“=”, 最大选择:“=”, 验证火:“=”, onValidation: '&', onClose: '&' }, 模板:“”+ ""+ “添加” + ""+ ""+ ""+ ""+ “全部” + “无” + ""+ "

"+ ""+ ""+ "{{item.Name}}"+ ""+ ""+ "", Controller :函数($scope){ $scope.handleClick = function($event) { $event.stopPropagation(); }; $scope.status = { isopen:假 }; $scope.status = { isopen:假 };

  $scope.openDropdown = function($event) {
    if ($scope.items !== undefined && $scope.items.length > 0) {

      if ($scope.items.length > $scope.maxSelection)
        $scope.showAll = false;
      else
        $scope.showAll = true;

      for (var index = 0; index < $scope.items.length; index++) {
        $scope.items[index].IsSelected = false;
      }
      if ($scope.selectedItems !== undefined && $scope.selectedItems.length > 0) {
        for (var selectedItemIndex = 0; selectedItemIndex < $scope.selectedItems.length; selectedItemIndex++) {
          for (var itemIndex = 0; itemIndex < $scope.items.length; itemIndex++) {
            if ($scope.selectedItems[selectedItemIndex].Id == $scope.items[itemIndex].Id) {
              $scope.items[itemIndex].IsSelected = true;
              break;
            }
          }
        }
      }
    }
    $event.stopPropagation();
    $scope.status.isopen = true;
  };

  $scope.selectAll = function($event) {
    $scope.selectedItems = [];
    angular.forEach($scope.items, function(item) {
      item.IsSelected = true;
      $scope.selectedItems.push(item);
    });
    $event.stopPropagation();
  };
  $scope.deselectAll = function($event) {
    angular.forEach($scope.items, function(item) {
      item.IsSelected = false;
    });
    $scope.selectedItems = [];
    $event.stopPropagation();
  };
  $scope.selectItem = function(item) {
    if (item.IsSelected === false) {
      for (var index = 0; index < $scope.selectedItems.length; index++) {
        if (item.Id == $scope.selectedItems[index].Id) {
          item.IsSelected = false;
          $scope.selectedItems.splice(index, 1);

          $scope.onClose({
            val: "1"
          });

          break;
        }
      }
    } else {
      if ($scope.selectedItems.length > $scope.maxSelection) {
        item.IsSelected = false;
        $scope.validationMsg = "MAX_REACHED";
        $scope.validateFire = true;
        $scope.onValidation({
          validationMsg: "Max_Reached"
        });
        return;
      }

      $scope.selectedItems.push(item);
    }
  };

  $scope.clickItem = function($event) {
    $event.stopPropagation();
  };

  $scope.closeDropDown = function() {
    $scope.status.isopen = false;
    $event.stopPropagation();
  };
}

}; });`

关于javascript - 用于多选的 AngularJS 指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28568651/

相关文章:

html - 当 View 差异太大时,是否可以将显示 :none, 用于响应式网站?

JavaScript - 如何获取超过 1 个文件的文件 id 属性?

javascript - 在另一个函数中请求函数响应

css - 居中 div 框不起作用

javascript - 当项目包含在数组中时,在 ng-repeat 上添加 Angular 类

javascript - angular-route-segment 调用之前的 Controller

javascript - 当用户单击按钮时 Bootstrap 向下滚动

javascript - 调用返回多个值的函数

javascript - Node js 对象导出

c# - TagBuilder 添加没有值的属性