简体   繁体   English

Angular JS:“多选选择框”的“全选”选项

[英]Angular JS: “Select All” options of “multi-select select box”

I have created a multiselect select box using Angular JS: below is the code for the same: 我已经使用Angular JS创建了一个多选选择框:以下是相同的代码:

JS: JS:

$scope.foobars = [{
    'foobar_id': 'foobar01',
    'name': 'foobar01',
}, {
    'foobar_id': 'foobar02',
    'name': 'foobar02',
}, {
    'foobar_id': 'foobar03',
    'name': 'foobar03',
}, {
    'foobar_id': 'foobar04',
    'name': 'foobar04',
}, {
    'foobar_id': 'foobar05',
    'name': 'foobar05',
}];

HTML: HTML:

<select multiple="multiple" size="5" id="selFooBar" ng-model="foobarName" ng-options="medcenter as medcenter.name for medcenter in medcenters track by medcenter.medcenter_id">
    <option selected="selected">Select All</option>
</select>

And the output is : 输出为:

在此处输入图片说明

Question 1: Why am I not getting the default option "Select All" in the list? 问题1:为什么列表中没有默认选项“全选”? And How do I get that? 那我该怎么办呢?

Question 2: How can I Select All optiions on click of "First Option : Select All"?? 问题2:如何在单击“第一个选项:全选”时选择“所有”选项?

Please suggest! 请提出建议!

If you want to keep the <option> in the <select> element before you add the ng-options you'll have to use transclusion. 如果要在添加ng-options之前将<option>保留在<select>元素中,则必须使用包含。 the ng-options directive doesn't use transclusion, but you can create a custom directive that does. ng-options指令不使用包含,但是您可以创建一个自定义指令。 You can do that by utilizing transcludeFn in the directive post compile function: 您可以通过在指令后编译函数中使用transcludeFn来做到这一点:

compile: function(element,attrs) {
  return {
    post: function(scope, element, attributes, controller, transcludeFn){
      transcludeFn(function(clone, scope) {
        // prepend the transcluded content to the select
        element.prepend(clone);
        // set the onclick of the clone to call the selectAll function
        clone.bind('click', function(){
          clone.scope().$parent.selectAll();
          scope.$apply();
        })
      });
    }
  }
},
controller: function($scope) {
  $scope.selectAll = function() {
    $scope.selectedValues = $scope.values;
  }
}

Then you can set the selectedValues to all possible values on the scope, whether that's isolate or inherited. 然后,您可以将selectedValues设置为作用域上所有可能的values ,无论是隔离的还是继承的。 In the following plnkr example it's isolated. 在下面的plnkr示例中,它是隔离的。 On click the Select All option will select the other elements. 单击时,全选选项将选择其他元素。 Plunker Example 柱塞示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM