Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

angularjs Separate values in select containing 2 object

function ctrl($scope) {
  $scope.options = [{
      name: "A",
      options: ["A1", "A2"]
    },
    {
      name: "B",
      options: ["B1", "B2"]
    },
  ];

  $scope.parseSelection = function() {
    const selected = {};
    
    // Loop over the selected options and check from which group they came
    $scope.rawSelected.forEach((value) => {
      $scope.options.forEach(({ name, options }) => {
        if (options.includes(value)) {
          // The option comes from the current group
          if (!selected[name]) {
            selected[name] = [];
          }
          selected[name].push(value);
        }
      });
    });
    $scope.selected = selected;
  };
}

angular.module("app", [])
  .controller("ctrl", ctrl)
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.js"></script>


<div ng-app="app" ng-controller="ctrl">
  <select multiple ng-model="rawSelected" ng-change="parseSelection()">
    <optgroup ng-repeat="group in options" label="group.name">
      <option ng-repeat="option in group.options">{{option}}</option>
    </optgroup>
  </select>
  {{rawSelected}} - {{selected}}
</div>
Comment

PREVIOUS NEXT
Code Example
Javascript :: AngularJS Pagination not showing all pages 
Javascript :: AngularJS disable default form submit hook 
Javascript :: Angular.js : recursive call to an $mdDialog controller 
Javascript :: How to hide div based on select the dropdown in angular js 
Javascript :: How to merge array into JSON array 
Javascript :: Json response reactnative fetch login data 
Javascript :: Pass JSON array to function in React as props and then count items in area 
Javascript :: Reanimated2 interpolateNode to animate opacity error "undefined is not an object 
Javascript :: How to hover over data inserted from JSON 
Javascript :: javascript array add method 
Javascript :: debugJSON 
Javascript :: vue custom event validation 
Javascript :: request submit form 
Javascript :: sinalR 
Javascript :: useEffect in React 18 in strictmode 
Javascript :: online jquery converter 
Javascript :: "Uncaught (in promise) TypeError: dispatch is not a function" 
Javascript :: phaser asteroid movement 
Javascript :: testing code through local server using express.js 
Javascript :: replace text content with element node 
Javascript :: vue js beforeEach is not a function 
Javascript :: javascript palindrome check 
Javascript :: Use Dynamic Scales 
Javascript :: true type of javascript 
Javascript :: load limited data and search data from all in angularjs 
Javascript :: var countdown = function(num) {} 
Javascript :: how to set javascript load order in html 
Javascript :: how to check if a div tag contains child 
Javascript :: java scrypt 
Javascript :: callback function jquery 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =