Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Automatic display keyed value in a dynamic table row using javascript, angular js and html

angular.module('myApp', [])
  .controller('myCtrl', ['$scope', function($scope) {
    $scope.tableData = [{
        details: "Here are some details...",
        quantity: 5,
        unit_cost: 14.99
      },
      {
        details: "Here are some details2...",
        quantity: 2,
        unit_cost: 4.99
      },
      {
        details: "Here are some details3...",
        quantity: 1,
        unit_cost: 44.99
      },
      {
        details: "Here are some details4...",
        quantity: 0,
        unit_cost: 104.99
      }
    ];

    $scope.getTotal = function() {
      return $scope.tableData.reduce((b, a) => {
        b += a.unit_cost * a.quantity;
        return b;
      }, 0).toFixed(2)
    }

    $scope.add = function() {
      let blankRow = {
        details: "",
        quantity: "",
        unit_cost: ""
      }
      $scope.tableData.push(blankRow)
    };


    $scope.remove = function(index) {
      $scope.tableData.splice(index, 1);
    };


  }]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  <form method="POST">
    <table class="table">
      <thead>
        <tr>
          <th>Details</th>
          <th>Quantity</th>
          <th>Unit cost</th>
          <th>Total cost</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat='i in tableData'>
          <td>
            <textarea class='form-control' ng-model="i.details" name="details_{{$index}}" required></textarea>
          </td>
          <td>
            <input type='number' name="quantity_{{$index}}"  class='form-control' ng-model='i.quantity' required/>
          </td>

          <td>
            <input type='number' name="unit_cost_{{$index}}"  class='form-control' ng-model='i.unit_cost' required/>
          </td>

          <td>
            ${{(i.unit_cost * i.quantity).toFixed(2)}}
          </td>
          <td>
            <button class="btn btn-danger" ng-click="remove($index)" type="button">- Del</button>
          </td>
        </tr>
      </tbody>
    </table>
Comment

PREVIOUS NEXT
Code Example
Javascript :: Fire "data-ng-change" programatically or another way to change value of input on website using Angular JS 
Javascript :: AngularJs - Display Validation Message within Component 
Javascript :: Angular after click add active class and remove from siblings 
Javascript :: Angularjs onchange datetime picker not working 
Javascript :: Angular.js : recursive call to an $mdDialog controller 
Javascript :: AngularJS Form validation transition to valid when some elements are not even touched yet 
Javascript :: Can’t connect Express.js server to the Angular frontend 
Javascript :: react-native navigation stack set push component then cover parent page 
Javascript :: react table Maximum update depth exceeded. 
Javascript :: how to make colspan of table footer flexible with javascript/jQuery 
Javascript :: Browser globals 
Javascript :: JSON.stringify on Arrays adding numeric keys for each array value 
Javascript :: How to limit properties of a JSON object given array of property names using JQ 
Javascript :: disconnect google colab runtime 
Javascript :: get oinput value clojurescript 
Javascript :: mustache tutorial javascript 
Javascript :: select random quotes from array called anecdotes 
Javascript :: Creating Multiple Methods From A List Of Words 
Javascript :: inspect vuex store 
Javascript :: In Self Invoking Functions, the This Below Console.Logs The Created Object 
Javascript :: kendo grid column template based on condition 
Javascript :: react console logs not working 
Javascript :: Create A Class That Returns A Promise In Constructor 
Javascript :: nested object data 
Javascript :: function titleCase 2 
Javascript :: Exporting Objects with the Exports Object in electron 
Javascript :: miragejs url parameters 
Javascript :: Using Fetched Data With Backbone 
Javascript :: Solution-4--solution options for reverse bits algorithm js 
Javascript :: javascript number reverse 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =