Adding drop down box on clicking submit in angular js

时间:2015-06-15 14:55:28

标签: angularjs

I have two buttons which add or delete a combo box when clicked. What is want is to add the combo box right below the current row and so on when "add" button is clicked. Here's the plunker

<html lang="en" ng-app="ui.bootstrap.demo">
    <head>
        <meta charset="UTF-8">
        <title>Text Box</title>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
        <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.0.js"></script>
        <script src="script.js"></script>
        <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
    </head>

    <body>
        <div id="ctrl-as-exmpl" ng-controller="DatepickerDemoCtrl">
            <ul>
                <button ng-click="addDropDown()">add</button>
                <button ng-click="deleteDropDown()">Delete</button>
                <li ng-repeat="dropDown in comboBox track by $index">
                    <select ng-model="valueSelected">
                        <optgroup>
                            <option value="LIKE">LIKE</option>
                            <option value="BETWEEN">BETWEEN</option>
                            <option value="EXISTS">EXISTS</option>
                        </optgroup>
                    </select>
                    <div ng-switch on="valueSelected">
                        <div ng-switch-when="LIKE"><input type="text"/></div>
                        <div ng-switch-when="BETWEEN">
                            <style>
                                .full button span {
                                    background-color: limegreen;
                                    border-radius: 32px;
                                    color: black;
                                }
                                .partially button span {
                                    background-color: orange;
                                    border-radius: 32px;
                                    color: black;
                                }
                            </style>
                            <div ng-controller="DatepickerDemoCtrl">

                                <div class="row">
                                    <div class="col-md-6">
                                        <p class="input-group">
                                            <input type="text" class="form-control" datepicker-popup="{{format}}" ng-model="dt" is-open="opened" min-date="minDate" max-date="'2015-06-22'" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close" />
                                            <span class="input-group-btn">
                                                <button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
                                            </span>
                                        </p>
                                    </div>

                                    <div class="col-md-6">
                                        <p class="input-group">
                                            <input type="date" class="form-control" datepicker-popup ng-model="dt" is-open="opened" min-date="minDate" max-date="'2015-06-22'" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close" />
                                            <span class="input-group-btn">
                                                <button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
                                            </span>
                                        </p>
                                    </div>
                                </div>
                            </div>
                            <div ng-switch-when="EXISTS">I show when EXISTS is selected</div>
                            <div ng-switch-default>I show by default</div>
                        </div>
                </li>
            </ul>
        </div>
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

One solution is to create a recursive directive. You create a data structure to hold each user and its children (other users). Every time the "add" or "delete" button is clicked you add/remove from the list of children for the clicked user.

Please see this working Plunker

Code Sample

This is the directive:

.directive('dropDown', function($compile) {
    return {

      restrict: 'E',

      scope: {
        user: '=user'
      },

      controller: function($scope) {

          $scope.addChild = function (child) {
              var index = $scope.user.children.length;
              $scope.user.children.push({
                  "parent": $scope.user,
                  "children": [],
                  "index": index
              });
          }

          $scope.remove = function () {
              if ($scope.user.parent) {
                var parent = $scope.user.parent;
                var index = parent.children.indexOf($scope.user);
                parent.children.splice(index, 1);
              }
          }
      },

      templateUrl: 'dropdown.tpl.html',

      link: function ($scope, $element, $attrs) {

      },

      compile: function(tElement, tAttr) {
          // Compile the directive for recursive call.
          var contents = tElement.contents().remove();
          var compiledContents;
          return function(scope, iElement, iAttr) {
              if(!compiledContents) {
                  compiledContents = $compile(contents);
              }
              compiledContents(scope, function(clone, scope) {
                       iElement.append(clone); 
              });
          };
      }
    };
});

This is the directive's template:

<button ng-click="addChild()">add</button>
<button ng-click="remove()">Delete</button>
<select ng-model="valueSelected">
  <optgroup>
      <option value="LIKE">LIKE</option>
      <option value="BETWEEN">BETWEEN</option>
      <option value="EXISTS">EXISTS</option>
  </optgroup>
</select>
<div ng-switch on="valueSelected">
    <div ng-switch-when="LIKE"><input type="text" /></div>
    <div ng-switch-when="BETWEEN">
        <style>
            .full button span {
                background-color: limegreen;
                border-radius: 32px;
                color: black;
            }
            .partially button span {
                background-color: orange;
                border-radius: 32px;
                color: black;
            }
        </style>
        <div ng-controller="DatepickerDemoCtrl">

            <div class="row">
                <div class="col-md-6">
                    <p class="input-group">
                        <input type="text" class="form-control" datepicker-popup="{{format}}" ng-model="dt" is-open="opened" min-date="minDate" max-date="'2015-06-22'" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close" />
                        <span class="input-group-btn">
                            <button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
                        </span>
                    </p>
                </div>

                <div class="col-md-6">
                    <p class="input-group">
                        <input type="date" class="form-control" datepicker-popup ng-model="dt" is-open="opened" min-date="minDate" max-date="'2015-06-22'" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close" />
                        <span class="input-group-btn">
                            <button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
                        </span>
                    </p>
                </div>
            </div>
        </div>
        <div ng-switch-when="EXISTS">I show when EXISTS is selected</div>
        <div ng-switch-default>I show by default</div>
    </div>
</div>

<!-- Recursive call to the directive -->
<ul>
  <li ng-repeat="child in user.children">
    <drop-down user="child"></drop-down>
  </li>
</ul>

Main index.html:

<html lang="en" ng-app="ui.bootstrap.demo">
    <head>
        <meta charset="UTF-8">
        <title>Text Box</title>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
        <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.0.js"></script>
        <script src="script.js"></script>
        <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
    </head>

    <body>
        <div id="ctrl-as-exmpl" ng-controller="DatepickerDemoCtrl">
            <drop-down user="users[0]"></drop-down>
        </div>
    </body>
</html>