Taking the value of the table with ckeckbox and moving to the controller with Angularjs

Asked

Viewed 1,337 times

0

Goodnight!

I’ve got a problem I haven’t found a solution to

First I make a query in the database that returns me some values and I play within a table with ng-repeat

Table

<tr ng-repeat="contrato in contratos">
<td>{{contrato.NOME}}</td>
<td>{{contrato.CPF_CONTRATO}}</td>
<td>{{contrato.NUMERO_CONTRATO}}</td>
<td>{{contrato.FISICO}}</td>
<td><input type="checkbox" /></td>
</tr>

so far so good, but I need to make sure that by clicking on checkbox I get the NUMERO_CONTRATO and move to a function in my controller

Function

$scope.PegaContrato = function () {

}

1 answer

5


Add the argument ng-click to your checkbox:

<input type="checkbox" ng-click="PegaContrato(contrato.NUMERO_CONTRATO)">

Then change your function to receive the value:

$scope.PegaContrato = function (numero_contrato) {
    console.log(numero_contrato);
}

There, so you can already know which contract has been selected.


UPDATE:

If there is a need to remove a selected contract (reverse action), add the argument ng-checked to your checkbox:

<input type="checkbox" 
    ng-click="PegaContrato(contrato.NUMERO_CONTRATO)"
    ng-checked="contratos.indexOf(contrato.NUMERO_CONTRATO) > -1">

Change your job function:

$scope.contratos = [];
$scope.PegaContrato = function (numero_contrato) {
    //console.log(numero_contrato);
    var index = $scope.contratos.indexOf(numero_contrato);

    // se já selecionou o contrato, então remove (nesse caso o checkbox foi desmarcado)
    if (index > -1)
        $scope.contratos.splice(index, 1);
    // se selecionou um novo contrato, adiciona ao array
    else
      $scope.contratos.push(numero_contrato);

    // print em tela do array com os contratos selecionados
    console.log(contratos);
}
  • And it’s got more than one contract to go ?

Browser other questions tagged

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