How to do checkbox validation at the angular

Asked

Viewed 1,480 times

3

I need the add button is only activated if at least 1 item of the list has been checked. For text inputs, I used ng-required="true"

<div class="modal fade" id="listagem-tarefas" tabindex="-1"
    role="dialog" aria-labelledby="listagem-tarefas" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content" id="cadastro-tarefa">
            <form data-toggle="validator" role="form">
                <div class="modal-header">
                    <!--button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button-->
                    <h4 class="modal-title custom_align" id="Heading">Listagem de
                        Tarefas</h4>
                </div>
                <!--filtro tarefa -->
                <div
                    style="margin-top: 10px; margin-left: 10px; margin-bottom: 20px;">
                    <button type="submit" class="btn btn-primary btn-sm"
                        data-toggle="collapse"
                        data-target="#form-filtro-tarefa-em-atividade">Filtro</button>
                    <div class="collapse" id="form-filtro-tarefa-em-atividade"
                        style="margin-top: 20px; margin-right: 10px;">
                        <div class="form-group">
                            <label class="" for="orderBy">Código</label> <input
                                class="form-control" type="text" placeholder="Código da tarefa"
                                ng-model="criterioDeBuscaTarefaEmAtividades.codigo" id="">

                        </div>
                        <div class="form-group">
                            <label class="" for="orderBy">Nome</label> <input
                                class="form-control" ng-model="criterioDeBuscaTarefaEmAtividades.descricao"
                                type="text" placeholder="Nome da tarefa" id="">

                        </div>
                        <div class="form-group">
                            <label class="" for="orderBy">Tipo</label> <input
                                class="form-control" ng-model="criterioDeBuscaTarefaEmAtividades.descTipo"
                                type="text" placeholder="Tipo da tarefa" id="">

                        </div>
                    </div>
                </div>
                <div style="overflow: scroll; height: 400px;">
                    <table class="table table-sm table">
                        <thead>
                            <tr>
                                <th><a href="">Seleção</a></th>
                                <th><a href="" ng-click="filtroOrdenarPor('codigo')">Código</a></th>
                                <th><a href="" ng-click="filtroOrdenarPor('descricao')">Nome</a></th>
                                <th><a href="" ng-click="filtroOrdenarPor('tipo')">Tipo</a></th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr
                                ng-repeat="tarefa in tarefas | filter:criterioDeBuscaTarefaEmAtividades | orderBy:criterioDeOrdenacaoTarefasEmAtividades:filtroDirecaoDaOrdenacao">
                                <td class="text-center">
                                    <div class="ckbox text-center">
                                        <input type="checkbox" class="checkbox-tarefas-em-atividades"
                                            ng-model="tarefa.selecionada"> <label
                                            for="checkbox1"></label>
                                    </div>
                                </td>
                                <th scope="row" class="text-center">{{tarefa.codigo}}</th>
                                <td>{{tarefa.descricao}}</td>
                                <td>{{tarefa.descTipo}}</td>

                            </tr>
                        </tbody>
                    </table>
                </div>
                <div class="modal-footer">
                    <div class="flag-obrigatorio">
                        <button type="button" class="btn btn-primary btn-md"
                            ng-click="selecionarTarefas(tarefas)" data-dismiss="modal" id="">
                            <span class="glyphicon glyphicon-ok-sign"></span> Adicionar
                        </button>
                        <button type="button"
                            class="btn btn-primary btn-md limpa-tarefas-em-atividades"
                            data-dismiss="modal">
                            <span class="glyphicon glyphicon-ok-sign"></span> Voltar
                        </button>
                    </div>
                </div>
            </form>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>

I applied this solution, the button comes disabled, then I check and it enables, but if I uncheck it does not disable again, and loses the validation.

 $scope.value = [];
    $scope.updateQuestionValue = function(tarefa){
        $scope.value = $scope.value || [];
        if(tarefa.selecionada){
            $scope.value.push(tarefa.value);
            $scope.value = _.uniq($scope.value);
        }else{
            $scope.value = _.without($scope.value, tarefa.value);
        }
    };

<input type="checkbox" class="checkbox-tarefas-em-atividades"
ng-model="tarefa.selecionada" ng-click="updateQuestionValue(tarefas)">

<button type="button" class="btn btn-primary btn-md" 
  ng-click="selecionarTarefas(tarefa)" data-dismiss="modal" id="" ng-
  disabled="value.length==0">
  <span class="glyphicon glyphicon-ok-sign"></span> 
  Adicionar
</button>
  • in the task array have to have a selected?

  • @Virgilionovic exactly, I need at least 1 item of the task array to be selected

1 answer

0


I made a very simple example, in this example create a function where, will analyze the data of array and if he finds an item with the property selecionado true (true) he releases the button submit. Use the ng-required to the function show(), example:

var app = angular.module('app', []);
app.controller('ctrl', function($scope) {
  $scope.tarefas = [{
      'nome': 'a',
      'selecionado': false
    },
    {
      'nome': 'b',
      'selecionado': false
    }
  ];
  $scope.show = function(){
    var sts = false;
    for(i = 0; i < $scope.tarefas.length; i++)
    {
      if ($scope.tarefas[i].selecionado == true)
      {
        sts = true;
        break;
      }
    }
    return sts;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="ctrl">
  <form data-toggle="validator" role="form" name="form1">
    <table>
      <tr>
        <td>Nome</td>
        <td>Selecionado</td>
      </tr>
      <tr ng-repeat="t in tarefas">
        <td>{{t.nome}}</td>
        <td><input type="checkbox" 
        ng-required="!show()"
        ng-model="t.selecionado" /></td>
      </tr>
    </table>
  </form>
  <button ng-disabled="!form1.$valid">Enviar</button>
</div>

References

Browser other questions tagged

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