0

I have a table and I'd like one of the columns values to change based on it's header (which will be a select).

In my controller I have:

vm.pricing = [
        {
            id: 'price1',
            heading: 'Floor'
        },
        {
            id: 'price2',
            heading: 'Retail'
        }
    ];

My table is setup like:

<thead>
    <tr>
        <td>Vendor</td>
        <td>
            <select ng-model="vm.currentPricing"
                    ng-options="p.id as p.heading for p in vm.pricing">
            </select>
        </td>
    </tr>
</thead>

<tbody>
    <tr>
        <td>{{::vm.machine.vendorNbr}}</td>
        <td>{{vm.machine.price1}}</td>  <-- ****** What should go here?
    </tr>
</tbody>
1
  • Share what you've tried Commented Mar 19, 2015 at 15:12

3 Answers 3

1

Pass vm.machine to the controller which can use its currentPricing property to return to your code the correct value to render. Or use ng-show on lots of different objects to turn off the ones which are not relevant

<!-- Assuming vm is your controller, a function in it could do the rendering based on currentPricing field -->
<td>{{vm.describePrice(vm.machine)}}</td>

Or

<td ng-show="vm.currentPricing=='Option1'">{{vm.machine.price1}}</td>
<td ng-show="vm.currentPricing=='Option2'">{{vm.machine.price2}}</td>
Sign up to request clarification or add additional context in comments.

1 Comment

Option 1 worked great, for my controller I added: function describePrice(machine) { return machine[vm.currentPricing]; }
0

I think it should look like that

<tbody>
    <tr>
        <td>{{::vm.machine.vendorNbr}}</td>
        <td>{{vm.currentPricing.heading}}</td>  <-- ****** What should go here?
    </tr>
</tbody>

Comments

0

I'm assuming choosing something in dropdown recalculates the price. so given that assumption I would do this:

<thead>
<tr>
    <td>Vendor</td>
    <td>
        <select ng-model="vm.currentPricing"
                ng-options="p.id as p.heading for p in vm.pricing">
        </select>
    </td>
</tr>
</thead>

<tbody>
<tr>
    <td>{{::vm.machine.vendorNbr}}</td>
    <td>{{vm.machine.getPrice()}}</td>  
</tr>
</tbody>

--- app.js ----

app.controller('SomeController',function($scope){
    $scope.machine.getPrice = function(){
        var price = $scope.basePrice * (1 +  $scope.currentPricing); // I don't know how you calc new price
        return price;
}   

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.