0

for the customerGroup selector I would like the option value to be test[i][1] of the multidimensional array and the text of the options to be test[i][0] of the array. How should I change ng-options?

For example if the 2D array consisted of [1,7],[2,8],[3,9] elements. I would like it to turn into <option value="1">7</option> etc etc

            <select ng-model = "selectedGroup" class = "customergroup" ng-options = "val[][0] for val in cgOptions() track by val[][1]">
                <option value = "">Select</option>
            </select>

Also, on a side note is it possible to pass the function cgOptions later on into another function's parameter with something like exampleFunction(cgOptions)?

$scope.cgOptions = function () {

 var test = [];
 for (var i = 0; i < 4; i++) {
    test[i][0] = i;
    test[i][1] = i+10;
 }
 return test;};

Thank you in advance!

isherwood
  • 58,414
  • 16
  • 114
  • 157
DevSung
  • 1
  • 4
  • cgOptions function isn't even valid javascript. If you are going to use a data mapping function why not return array of objects that fits well with normal usage? – charlietfl Jan 23 '15 at 22:01

1 Answers1

0

In ng-options when you do val in array, val becomes each element of the array. So, you'd need val[0] for the value, and val[1] for the display:

<select ng-model="selectedGroup"
        ng-options="val[0] as val[1] for val in cgOptions()">
   <option value="">select</option>
</select>

Also, bear in mind that you cgOptions() function should initialize the array for each iteration.

for (var i = 0; i < 4; i++) {
    test[i] = []; // initialize the array first
    test[i][0] = i;
    test[i][1] = i+10;
 }
New Dev
  • 48,427
  • 12
  • 87
  • 129
  • So the text part works but it is not setting the value of the selections to how I want. Instead it is just giving a value that is incrementing by 1 starting from 0 no matter how I change test[i][0] – DevSung Jan 26 '15 at 17:06
  • @DevSung, Are you talking about the `value` property of ` – New Dev Jan 26 '15 at 17:22
  • yeah the value property of option – DevSung Jan 26 '15 at 18:59
  • You shouldn't care about that. Angular will take care of the mapping between selection and `ng-model` value. The main thing is that `$scope.selectedGroup` obtains the right value, which it will - in this case, it will equal `val[i][0]` – New Dev Jan 26 '15 at 19:01
  • Ah ok. How do i access this value? Because when I do `console.log($( ".customergroup option:selected" ).val())` it gives me the index of the array value rather than the val[i][0] value. – DevSung Jan 26 '15 at 19:17
  • You already have access to it via `$scope.selectedGroup`. Please read this [SO question and answer](http://stackoverflow.com/questions/14994391/thinking-in-angularjs-if-i-have-a-jquery-background). Angular uses a fundamentally approach from apps built with jQuery (for example). – New Dev Jan 26 '15 at 19:42
  • So how I would I obtain that selectedGroup value in another function? `$scope.scOptions = function () { //var x = $( ".customergroup option:selected" ).val() + "00"; var x = $scope.selectedGroup; console.log(x); var test2 = []; for (var i = 0; i < 4; i++) { test2[i] = x; } return test2; };` – DevSung Jan 26 '15 at 20:01
  • @DevSung, what is "another function"? Is it a function in another controller? Then look into communication between controllers. It typically involves using a service. In some cases it is preferable to rely on scope inheritance. In any case, this is a new question and not possible to give a good answer (or even direction) in comments – New Dev Jan 26 '15 at 20:03
  • It's in the same controller. I want the value of selected value in selectedGroup in another function but inside same controlelr – DevSung Jan 26 '15 at 20:08
  • Then what is the problem? Any typical Angular example will show you how to do this. Your question seems to be stemming from insufficient knowledge of Angular, and the comments section is not the right place to learn. I suggest you amend and clarify this question with more info, or ask another – New Dev Jan 26 '15 at 20:14