简体   繁体   中英

Angular hide inputs in ng-repeat table rows

How would i hide input elements and replace there value to the table rows , the inputs are dynamically created with push see below code :

view

ID LIKE TO REPLACE THE INPUTS WITH THE VALUE OF INPUTS IN THE TABLE ROWS

<tr class="odd gradeX" ng-repeat="choice in vm.choices">
   <td><a href="" ng-click="vm.addNewChoice()">Add</a></td>
   <td><a href="" ng-click="vm.saveChoice()">save</a></td>
   <td>
    <div class="form-group">
       <div class="input-group">
         <input type="text" placeholder="Item Name" class="form-control" ng-model="choice.item_name"/>
       </div>
    </div>

</td>
<td>
  <div class="form-group">
     <div class="input-group">
       <select data-ng-options='t.value as t.label for t in vm.invoice_item_type' ng-model="choice.item_type" >
        </select>
     </div>
  </div>
</td>

contoller

vm.choices  = [];

vm.addNewChoice = function() {
   var newItemNo = parseInt(vm.choices.length+1);
   vm.choices.push({});
};


vm.saveChoice = function() {
          var lastItem = vm.choices.length-1;
          ------ What to do here ------
         };

Ok, the easiest way to do this would be probably something like this:

  1. Add additional field to every choice object saying if it's saved or not
  2. Add two <td> s for every choice, one with plain text and one with input and show/hide them depending on extra parameter' value.

Something like this:

<tr class="odd gradeX" ng-repeat="choice in vm.choices">
   <td><a href="" ng-click="vm.addNewChoice()">Add</a></td>
   <td><a href="" ng-click="vm.saveChoice(choice)">save</a></td>
   <td ng-hide="choice.saved">
    <div class="form-group">
       <div class="input-group">
         <input type="text" placeholder="Item Name" class="form-control" ng-model="choice.item_name"/>
       </div>
    </div>
   </td>
  <td ng-show="choice.saved">
    <div class="form-group">
       <div class="input-group">
         <input type="text" placeholder="Item Name" class="form-control" ng-model="choice.item_name"/>
       </div>
    </div>
   </td>
   <!-- rest of your row goes here -->
</tr>

And in controller:

vm.choices  = [];

vm.addNewChoice = function() {
   var newItemNo = parseInt(vm.choices.length+1);
   vm.choices.push({}); // we don't need to set `saved` property explicitly since undefined will be resolved to false
};


vm.saveChoice = function(choice) {
          var lastItem = vm.choices.length-1;
          choice.saved=true;
          // probably some extra logic related to saving 
};

Note that I've added parameter to saveChoice method - you need to know which choice to save. Also, I think that button for adding new choice should be moved outside of the table - adding new item is not related to any existing item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM