2

I used the code to add the row and 2 columns by clicking the add rows. "My need is", First fill the values in the input field, after that click the Add item button, the values must be shown in table structure. Am the beginneer. Cant able to use the for loop. Can anyone please solve this issue.

try the code : https://jsfiddle.net/idris9791_/a7L832LL/

  <html >
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">

    <title>Add Rows</title>


    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>

  <body ng-controller="MainController" ng-app="MyApp">

    <a href="#" class="button" ng-click="addRow()">Add Row</a>
    <form>
    First Name : <input name="myInput" ng-model="user.firstName" required>
    First Name : <input name="myInput" ng-model="user.lastName" required>
    </form>


<table>
  <thead>
    <tr>
      <th>Some Header</th>
    </tr>
  </thead>
  <tbody>
   <tr ng-repeat="rowContent in rows">
  <td>{{rowContent.firstName}}</td>
  <td>{{rowContent.lastName}}</td>
</tr>
  </tbody>
</table>    
<script>
angular.module('MyApp', [])
.controller('MainController', [ '$scope', function($scope) {

  $scope.rows = [];

  $scope.counter = 0;

  $scope.addRow = function() {

    $scope.row.push({
    firstName: $scope.firstName,
    lastName: $scope.lastName
});

    $scope.counter++;

  }
}]);
</script>

  </body>
</html>

1 Answer 1

8

You have to actually push the entered input values into the rows array:

$scope.row.push({
    firstName: $scope.firstName,
    lastName: $scope.lastName
});

I'd update the html to read:

<a href="#" class="button" ng-click="addRow()">Add Row</a>
First Name : <input ng-model="firstName" required>
Last Name : <input ng-model="lastName" required>

And then:

<tr ng-repeat="rowContent in rows">
  <td>{{rowContent.firstName}}</td>
  <td>{{rowContent.lastName}}</td>
</tr>

Make sure angular is included properly and your controller is being used the right way too.

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

7 Comments

Yes, now i included the angular. But still is not working. I checked from my side.
A couple things.. change it to this: $scope.rows.push({ firstName: $scope.user.firstName, lastName: $scope.user.lastName }); You have a typo in the $scope.row.push line and you gotta line up the data with the model from your html.
is it possible to display the input elements in dialog box?
I'm not quite sure I understand your question?
Already i worked in jquery. just the check the dialog box in my fiddle. Fiddle link : jsfiddle.net/idris9791_/97xrvajj
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.