Avoid passing values twice

0

When I click on the checkbox it adds the values I have to another table but what I want to avoid if you click again checkbox to avoid adding again its value since the other table already has it.

Html where I extract the values

   <tr ng-repeat="vacanteP in vacantesPerCtrl.vacantesLis">                           
        <td class="text-center">{{ vacanteP.nombreVacante }}</td>                            

         <input type="checkbox"
                ng-disabled="vacantesPerCtrl.limiteP"
                ng-click="vacantesPerCtrl.addVacantes(vacanteP, $index)">
         <span class="lever"></span>
         <small>Postular</small>

      </tr>

javaScript

scope.addVacantes = function(object, index){

 scope.addListaVacantesP.push(
    { operativo:object.nombreProyecto, estado:object.nombreEstado,
      municipio:object.nombreMunicipio, vacante:object.nombreVacante, valor:object.idConfigVacProy }
);

}

Html where I show them

<tr ng-repeat="vacantesLista in vacantesPerCtrl.addListaVacantesP">
    <td class="text-center">{{ vacantesLista.operativo }}</td>
    <td class="text-center">{{ vacantesLista.estado }}</td>
    <td class="text-center">{{ vacantesLista.municipio }}</td>
    <td class="text-center">{{ vacantesLista.vacante }}</td>

 </tr>
    
asked by wootsbot 18.12.2016 в 09:53
source

1 answer

3

When working with angularjs you can give your checkbox a ng-model and a ng-change

Since it is a checkbox, it has 2 default values or true or false and that value can be rescued with the ng-model

With the tag ng-change we can identify when this value changes. We set a function, something like that.

<input type="checkbox"
       ng-model="checkModel"
       ng-disabled="vacantesPerCtrl.limiteP"
       ng-change="vacantesPerCtrl.addVacantes(vacanteP, $índex)">

From the controller the following:

$scope.addVacantes = funcion(vacanteP, index) {
     if($scope.checkModel){
            guardas en la tabla
     }else {
            //evitas guardarla nuavemente
     }
}

OPTION 2

You leave your code as it is. Define at the beginning of your controller a variable, for example $scope.guardar = true within your function addVacantes you put the following

if($scope.guardar){
      //guardas lo que quieras y cambias el valor de $scope.guardar
      $scope.guardar = false;
}

This will prevent that if you click again, do not enter and do nothing

EDITING

As I said in this question, Angularjs does not support even dynamic models within ng-repeat and therefore you must create it. In your controller you must go through your arrangement and create a unique model for each of them, you have the ID field so it will be easy, see my answer that has a demonstration codepen and it will be easy to achieve what you want. Always validating by model.

    
answered by 18.12.2016 / 13:03
source