Asad
Asad

Reputation: 67

add row to first of the table angularjs

how i can row in angular to the first

html :

    <title>Add Rows</title>

    <link href="http://cdn.foundation5.zurb.com/foundation.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
  </head>

  <body ng-controller="MainController">

    <a href="#" class="button" ng-click="addRow()">Add Row {{counter}}</a>

<table>
  <thead>
    <tr>
      <th width="200">Some Header</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="rowContent in rows">
      <td>{{rowContent}}</td>
    </tr>
  </tbody>
</table>    

  </body>
</html>

js :

angular.module('MyApp', [])
.controller('MainController', [ '$scope', function($scope) {

  $scope.rows = ['Row 1', 'Row 2'];

  $scope.counter = 3;

  $scope.addRow = function() {

    $scope.rows.push('Row ' + $scope.counter);
    $scope.counter++;
  }
}]);

http://codepen.io/calendee/pen/buCHf

in the example below it added the row in the last , how i can it to be the first row not the last.

Upvotes: 0

Views: 1668

Answers (1)

Kevin Simple
Kevin Simple

Reputation: 1213

mate, just use unshift instead of push, then it will insert to the top of array,

  $scope.rows.unshift('Row ' + $scope.counter);
    $scope.counter++;

have done a fiddle for you:

http://codepen.io/anon/pen/QjPxvN?editors=101

Upvotes: 2

Related Questions