在md-select控件中聚焦自定义md-checkbox

时间:2017-09-04 06:41:53

标签: angularjs angular-material md-select

我已经使用select all选项实现了select控件,当我打开控件时,它会关注第一个选项。我想专注于选择选项或至少禁用焦点。

HTML

<div ng-app="selectDemoOptGroups" ng-controller="SelectOptGroupController" >
    <md-select md-selected-text="selectedText" ng-model="selectedToppings"  multiple>   
     <div class="select-all-div" >
          <md-checkbox  class="select-selectAll"
                       >Select all</md-checkbox > </div>
        <md-option ng-value="topping.name" ng-repeat="topping in toppings">{{topping.name}}</md-option>
    </md-select>

JS

   angular
.module('selectDemoOptGroups', ['ngMaterial'])
.controller('SelectOptGroupController', function($scope) {

  $scope.toppings = [
    { category: 'meat', name: 'Pepperoni' },
    { category: 'meat', name: 'Sausage' },
    { category: 'meat', name: 'Ground Beef' },
    { category: 'meat', name: 'Bacon' },
    { category: 'veg', name: 'Mushrooms' },
    { category: 'veg', name: 'Onion' },
    { category: 'veg', name: 'Green Pepper' },
    { category: 'veg', name: 'Green Olives' }
  ];
  $scope.selectedToppings = [];


});

https://jsfiddle.net/AlexLavriv/ya6eu8kz/5/

2 个答案:

答案 0 :(得分:1)

您不能将div用于单个操作,而options用于其他操作。您可以使用相同的选项显示全选选项。

<md-select>
  <md-option>Select all</md-option>
  <md-option ng-value="topping.name" ng-repeat="topping in toppings">{{topping.name}}</md-option>
</md-select>

工作演示:JSFiDDLE

答案 1 :(得分:0)

Working Demo

尝试以下方式

&#13;
&#13;
   angular
    .module('selectDemoOptGroups', ['ngMaterial'])
    .controller('SelectOptGroupController', function($scope) {

      $scope.toppings = [     
        { category: 'meat', name: 'Pepperoni' },
        { category: 'meat', name: 'Sausage' },
        { category: 'meat', name: 'Ground Beef' },
        { category: 'meat', name: 'Bacon' },
        { category: 'veg', name: 'Mushrooms' },
        { category: 'veg', name: 'Onion' },
        { category: 'veg', name: 'Green Pepper' },
        { category: 'veg', name: 'Green Olives' }
      ];
      $scope.toppings.unshift( { category: 'select', name: 'Select all' })
      $scope.selectedText = $scope.toppings[0].name;
       
   
    });
&#13;
.select-selectAll{
text-align: left;
     padding-left: 10px;  
    /* padding-right: 16px; */
    display: flex;
    cursor: pointer;
    position: relative !important;
    display: -webkit-box;
    display: -webkit-flex;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    align-items: center;
    width: 95%;
    -webkit-transition: background .15s linear;
    transition: background .15s linear;
    /* padding: 0 16px; */
    height: 48px;
}


.select-selectAll div.md-icon{
    left:10px;
}



    .select-all-div:hover{
    background: rgb(238,238,238);
    }
&#13;
<div ng-app="selectDemoOptGroups" ng-controller="SelectOptGroupController" >
        <md-select md-selected-text="selectedText" ng-model="selectedToppings"  multiple>          
            <md-option ng-value="topping.name" ng-repeat="topping in toppings">{{topping.name}}</md-option>
        </md-select>
</div>
&#13;
&#13;
&#13;