Object with list of objects inside JavaScript

4

I'm using angular to make a form that contains multiple checkboxes (using angular-material), this form sends the values to the controler, which are received as follows:

Ineedtogetthisdata(allobjectscontainthesamefields)andturnitintoaJSON.Doesanyoneknowhowthiscanbedone?

Mycontrollercode:

vm.cadastraGrupo=function(dados){vm.disabled=true;console.log(dados);}

MyHTML:

<formname="userForm" ng-model="vm.data" ng-submit="vm.cadastraGrupo(vm.data)">
                        <md-input-container class="col-xs-12 md-block">
                            <label>Nome</label>
                            <input ng-model="vm.data.nome_grupo_menu" required="">
                        </md-input-container>
                        <div class="col-md-3 col-sm-6 col-xs-12">
                            <label class="label_tinyMCE left_100">Leitura</label>
                            <md-checkbox aria-label="Select All"
                                         ng-checked="vm.isCheckedLeitura()"
                                         md-indeterminate="vm.isIndeterminateLeitura()"
                                         ng-click="vm.toggleAllLeitura()"> Selecionar tudo
                            </md-checkbox>
                            <div ng-repeat="menus in vm.respostaListaMenusCadastrados">
                                <div style="display: none;">{{vm.data[menus.id_menu].id_menu_rel_permissao = menus.id_menu}}</div>
                                <input type="hidden" ng-model="vm.data[menus.id_menu].id_menu_rel_permissao">
                                <md-checkbox ng-checked="vm.existsLeitura(menus, vm.selectedLeitura)" aria-label="menus {{$index}}" ng-click="vm.toggleLeitura(menus, vm.selectedLeitura)" style="width: 100%;" ng-model="vm.data[menus.id_menu].read_permissao">{{menus.nome_menu_rel}}</md-checkbox>
                            </div>
                        </div>
</form>
    
asked by anonymous 29.09.2016 / 22:00

1 answer

4

With the help of OnoSendai, I was able to solve my problem in the following way: I get the data from the controller normally, I create a variable in the json format and then loop it over the objects to save them in the json variable

vm.cadastraGrupo = function (dados){
        var data = 
            [
                {
                    'nome_grupo_menu' : dados.nome_grupo_menu,
                    'authenticator' : '',
                    'permissoes' : []
                }
            ]
        for(var i in dados) {
            data[0].permissoes.push({'read_permissao' : dados[i].read_permissao, 'id_menu_rel_permissao' : dados[i].id_menu_rel_permissao});
        }
}
    
30.09.2016 / 12:42