Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs and automatically generated SELECT tags

I have a bit of a problem, I have an API that provides me with products, and each product has multiple options, like so :

[
    {
        "name": "Product 1",
        "options": [
            {
                "name": "Product 1 all options",
                "price": 2000
            }, {
                "name": "Product 1 no option",
                "price": 1400
            }
        ]
    }, {
        "name": "Product 2",
        "options": [
            {
                "name": "Product 2 all options",
                "price": 3000
            }, {
                "name": "Product 2 no option",
                "price": 1900
            }
        ]
    }];

Now I present it as so :

<tbody>
    <tr ng-repeat="product in products">
        <td>{{ product.name }}</td>
        <td>
            <select class="form-control">
                <option
                    ng-repeat="option in product.options"
                    data-price="{{ option.price }}">{{ option.name }}</option>
            </select>
        </td>
        <td>HERE I WANT TO DISPLAY THE SELECTED option.price +/- VAT</td>
    </tr>
</tbody>

Now as you may notice, the <select> tag itself is inside a ng-repeat, so it can be infinitely repeated, depending on the number of products I have; I can't bind the select to any ng-model because I may accidentally bind many <select>'s to the same model.

Now for each <select>, I have to display the price +/- a VAT in the third <td>, that corresponds to the selected option. Is there any way of doing that with angularjs? I don't want to go plain JS/jQuery.

like image 289
Shotgun Avatar asked Mar 04 '26 02:03

Shotgun


2 Answers

You can use the product variable as the model for the select. When the value changes it will also be updated outside the select as well.

An example would be:

app.controller('MainCtrl', function($scope) {
  $scope.products = ['a', 'b', 'c', 'd'];
});
<body ng-controller="MainCtrl">
  <div ng-repeat="product in products">
    <input ng-model="product">
    <div>{{product}}</div>
  </div>
</body>

You can find a working plnkr here.

A working example using your data. It's making a new field in your product called selectedPrice just to store the selected option.

app.controller('MainCtrl', function($scope) {
  $scope.products = [{
    "name": "Product 1",
    "options": [{
      "name": "Product 1 all options",
      "price": 2000
    }, {
      "name": "Product 1 no option",
      "price": 1400
    }]
  }, {
    "name": "Product 2",
    "options": [{
      "name": "Product 2 all options",
      "price": 3000
    }, {
      "name": "Product 2 no option",
      "price": 1900
    }]
  }];
});
<table>
  <tbody>
    <tr ng-repeat="product in products">
      <td>{{ product.name }}</td>
      <td>
        <select class="form-control" ng-model="product.selectedPrice">
          <option ng-repeat="option in product.options" value="{{ option.price }}">{{ option.name }}</option>
        </select>
      </td>
      <td>{{product.selectedPrice}}</td>
    </tr>
  </tbody>
</table>

A working example of that can be found here.

like image 198
toskv Avatar answered Mar 05 '26 14:03

toskv


Alternative to @toskv you can also avoid option tag altogether.

<tr ng-repeat="product in products">
  <td>{{ product.name }}</td>
    <td>
       <select class="form-control" ng-model="product.selectedPrice" ng-options="option.price as option.name for option in product.options track by option.name">
       </select>
    </td>
  <td>{{product.selectedPrice}}</td>
</tr>

see this

like image 24
Minato Avatar answered Mar 05 '26 14:03

Minato



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!