1

I have a Modal that has a table on the left with all the Customers in a table. On the right side are input fields. I need a user to be able to click on a Customer and populate the inputs fields with the Customer property values.

I made a Plunkr

My Code

 <table class=" table table-bordred table-striped table-hover">
      <tr><th style="font-weight: bold;">Customers</th></tr>
          <tbody>
              <tr ng-repeat="job in jobArray" class="pointer">
                  <td>{{job.Customers[0].CustomerName}}</td>
               </tr>
          </tbody>
  </table>

<div class="form-group">
   <div class="input-group">
      <span class="input-group-addon">C. Name</span>
      <input style="width:400px" ng-model="CustomerName"  type="text" />
    </div>
</div>

<div class="form-group">
     <div class="input-group">
          <span class="input-group-addon">C. Address</span>
          <input style="width:390px" ng-model="CustomerAddress" type="text">
 </div>
2
  • check your plunkr link, it's not working Commented Aug 22, 2014 at 16:57
  • I guess the plunkr is not working because user3919120 doesn't know how to do it. Commented Aug 22, 2014 at 17:23

1 Answer 1

1

Of course there are many way of doing it.

The easiest way is for them to communicate using the same $scope, using the same controller,

  1. Move your ng-controller to a container element that includes the table and the fields.
  2. Then I would add a selectedCustomer to the $scope, in your controller you can initialize the first customer with $scope.selectedCustomer = $scope.customers[0];
  3. Then change the ng-model of the inputs to use the selectedCustomer properties <input ng-model="selectedCustomer.customerAddress" ... />
  4. Now we need to change the selectedCustomer to match the one you click on, you can use ng-click to execute an expression when an element is clicked. <tr ng-click="setSelectedCustomer(customer)".
  5. This also means we need the setSelectedCustomer function in the scope, so in the controller you can declare the function

    $scope.setSelectedCustomer = function (customer) { $scope.selectedCustomer = customer; }

Here's a plunkr.

http://plnkr.co/edit/9SkOWYEQt5dAGPwKbEOW?p=preview

Note: Instead of adding stuff to the scope, take a look at aliasing the controller and adding stuff to the controller instance. Happy coding.

Sign up to request clarification or add additional context in comments.

2 Comments

@guzart, but if I want to use the same form to add a new customer? what should I do then?
@th1rdey3 you would need another variable to store the form customer, i.e. the user bound to the form, and the table customer, so that when you click on "New Customer" you set $scope.formCustomer = getEmptyCustomer(); and when clicking "Edit" you set $scope.formCustomer = getCustomer(id); for example.

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.