如何在角度js中创建一个muticolumn组合框

时间:2015-12-15 03:57:42

标签: javascript jquery angularjs

我希望有两个字段名称和阴影。这两个字段应显示为 单击下拉列表时的网格。当我从网格中选择一行时,只有名称应绑定到dropdown。在下面的链接中有这样的东西 https://demos.devexpress.com/ASPxEditorsDemos/ASPxComboBox/MultiColumn.aspx 下面是我的代码



<script>
angular.module('selectExample', [])
  .controller('ExampleController', ['$scope', function($scope) {
    $scope.colors = [
      {name:'black', shade:'dark'},
      {name:'white', shade:'light'},
      {name:'red', shade:'dark'},
      {name:'blue', shade:'dark'},
      {name:'yellow', shade:'light'}
    ];
    $scope.myColor = $scope.colors[2]; // red
  }]);
</script>
<div ng-controller="ExampleController">
  <span >
    <select ng-model="myColor" ng-options="color.name for color in colors">
      <option value="">-- choose color --</option>
    </select>
  </span>

 </div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您可以尝试使用此新指令: 注意:此答案基于您的参考链接。

&#13;
&#13;
(function(){

var app = angular.module('selectExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
        $scope.colors = [
            { name: 'black', shade: 'dark' },
            { name: 'white', shade: 'light' },
            { name: 'red', shade: 'dark' },
            { name: 'blue', shade: 'dark' },
            { name: 'yellow', shade: 'light' }
        ];
        $scope.myColor = $scope.colors[2];

        // red
    }]);
app.directive('dropdown', function($timeout) {
    return {
        restrict: 'A',
        require: 'ngModel',
        scope: {
            list: '=dropdown',
            ngModel: '=',
            selectedKey: '=',
            dropHeader: '='
        },
        templateUrl: '/tpl.html',
        replace: true,
        link: function(scope, elem, attrs, ngModel) {
            scope.head = false;
            scope.tdArry = [];
            if (scope.dropHeader == true) scope.head = true;

            if (Array.isArray(scope.list)) {
                var p = scope.list.map(function(_item) {
                    var keys = Object.keys(_item);
                    if (keys.length > 0) {
                        if (scope.tdArry.length == 0) scope.tdArry = keys;
                        else {
                            var j = keys.map(function(_k) {
                                if (scope.tdArry.indexOf(_k) == -1)
                                    scope.tdArry.push(_k);
                            });
                        }
                        return;
                    } else return;
                })
            } else {
                console.log('Directive Expecting an array of values ')
            }
            scope.$watch('ngModel', function() {
                scope.selected = ngModel.$modelValue;
            });
            scope.update = function(thing) {
                ngModel.$setViewValue(thing);
                ngModel.$render();
            };
            scope.getUpdatedVal = function() {
                var selectedKey = scope.selectedKey;
                if (selectedKey) {
                    if (scope.ngModel[selectedKey]) return scope.ngModel[selectedKey];
                    else {
                        var k = Object.keys(scope.ngModel);
                        return scope.ngModel[k[0]];
                    }
                } else {
                    var k = Object.keys(scope.ngModel);
                    return scope.ngModel[k[0]]
                }
            }
        },
    }
})

})()
&#13;
.title {
    padding: 5px 10px;
    border: 1px solid #ccc;
    cursor: pointer;
}

.title span {
    width: 100%;
    display: block;
    position: relative;
}

.title span:after {
    width: 0;
    height: 0;
    border: 5px solid transparent;
    border-bottom-width: 5px;
    border-bottom-style: solid;
    border-top-color: transparent;
    border-bottom-color: transparent;
    border-bottom: none;
    border-top-color: #201e1d;
    content: '';
    vertical-align: middle;
    display: inline-block;
    position: absolute;
    right: 2px;
    top: 8px
}

.drop-table {
    width: 100%;
    border-width: 0;
    border-collapse: collapse;
    border-spacing: 0;
}

.drop-table th {
    background: #eaeef2;
}

.drop-table th,
.drop-table td {
    text-align: left;
    padding: 5px 7px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.5/angular.min.js"></script>
<div ng-app="selectExample">
    <div ng-controller="ExampleController">
        <!-- <div> -->
        <div dropdown="colors" drop-header='true' selected-key="'name'" ng-model="myColor"></div>
    </div>

<script type="text/ng-template" id="/tpl.html">
    <div ng-click="open=!open">
        <div class="title">
            <span>{{getUpdatedVal()}}</span>
        </div>
        <table class="drop-table" ng-hide="!open">
            <tr ng-if="head">
                <th ng-repeat="h in tdArry">{{h}}</th>
            </tr>
            <tr ng-repeat="li in list" ng-click="update(li)">
                <td ng-repeat="d in tdArry">{{li[d]}}</td>
            </tr>
        </table>
    </div>
</script>
</div>
&#13;
&#13;
&#13;