remove right column

Asked

Viewed 208 times

1

Based on a script of the answer to that question: Dynamic table with angular

Where it creates a dynamic table with angular, but it cannot exclude the right "cell", it always excludes the last included question. To play the error just mark the first radio button from the first column and try to delete the question, it will remain and delete from the right column.

var app = angular.module("app", []);
app.filter('range', function() {
  return function(input, total) {
    total = parseInt(total);
    for (var i = 0; i < total; i++) {
      input.push(i);
    }
    return input;
  };
});

app.controller("ctrl", ["$scope", function($scope) {
  // criando variavel de perguntas
  $scope.answer = ["Sim", "Não"];
  // criando variavel de questão
  $scope.question = [{
      'name': "P 1",
      'r': [false, false]
    },
    {
      'name': "P 2",
      'r': [false, false]
    }
  ];
  // adicionando pergunta 
  $scope.addAnswer = function() {
    $scope.answer.push("R" + (new Date().getTime()));
    for (i = 0; i < $scope.question.length; i++) {
      $scope.question[i].r.push('false');
    }
  };
  // excluindo pergunta
  $scope.delAnswer = function(index) {
    $scope.answer.splice(index, 1);
    for (i = 0; i < $scope.question.length; i++) {
      $scope.question[i].r.splice(index - 1, 1);
    }
  };
  // adicionando questão
  $scope.addQuestion = function() {
    $r = $scope.answer.map(function() {
      return false;
    });
    $scope.question.push({
      'name': 'P' + (new Date().getTime()),
      'r': $r
    });
  };
  // excluindo questão
  $scope.delQuestion = function(index){
      $scope.question.splice(index, 1);
  }
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <div>
    <table border="0">
      <tr>
        <td>Perguntas:</td>
        <td ng-repeat="a in answer">
          <input value="{{a}}" style="width:40px" />
          <button ng-click="delAnswer($index)"> - </button>
        </td>
        <td><button type="button" ng-click="addAnswer()"> + </button></td>
      </tr>
      <tr ng-repeat="q in question">
        <td><input value="{{q.name}}" /></td>
        <td ng-repeat="b in q.r track by $index">
          <input type="radio" name="{{q.name}}" />
        </td>
        <td><button ng-click="delQuestion($index)"> - </button>
      </tr>
      <tr>
        <td>
          <button ng-click="addQuestion()"> + </button>
        </td>
        <tr>
    </table>
  </div>
</div>

OBS.: I think the error is in the function delAnswer, but I’m not finding the error.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.