1

I tried to add a column if a button is pressed, but with no success. I made a JSFiddle example with my problem. I would be very thankful if someone could help me with this problem.

This is my try:

$scope.addValue = function() {
      $scope.headers.push('new header');
      var users = 5;
      for(var i = 0; i < users; i++) {
          var rowData = [];
          for (var j = 0; j < 1; j++) {
              rowData.push('data i=' + i + ' j=' + j)
          }
          $scope.data.push(rowData);
      }
  };
2

1 Answer 1

1

Here is a working example based on this JSFiddle.

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
  $scope.headers = [];
  $scope.data = [];
  $scope.colCount = 3;

  var data = [];

  // populate demo data
  for (var i = 0; i < 10; i++) {
    $scope.headers.push('Col ' + (i + 1));
    var rowData = [];
    for (var j = 0; j < 10; j++) {
      rowData.push('Row-' + (i + 1) + ' - Col ' + (j + 1))
    }
    data.push(rowData);
  }

  $scope.increment = function(dir) {
    (dir === 'up') ? $scope.colCount++: $scope.colCount--;
  }

  $scope.data = data;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
  <button ng-click="increment('up')">Add Column</button>
  <button ng-click="increment('down')">Remove Column</button>
  <table>
    <tr>
      <th ng-repeat="h in headers | limitTo: colCount">{{h}}</th>
    </tr>
    <tr ng-repeat="row in data">
      <td ng-repeat="item in row | limitTo: colCount">{{item}}</td>
    </tr>
  </table>
</div>

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

1 Comment

I know, this was the basic of my example. but what I would need is a simple function where to put in data and column is than added.

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.