How to insert and unhide a list of items in the database

0

The code below allows insertion of n items in the array $scope.items[] :

var app = angular.module('app', []);
    app.controller('controlador', function($scope, $http) {
	$scope.user = {};
        $scope.produtoTrib = {};
	$scope.items = [];
	var sum = 1;
	
	$scope.addItem = function (user){
			$scope.items.push({
				nome: user.nome,
				email: user.email,
				soma: sum++
			});
		  user.nome = '';
		  user.email = '';	
	};
      
     
    $scope.addAtributos = function (produto){
        $scope.produtoTrib = produto;
	};
      
   
});
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><bodyng-app="app" ng-controller="controlador">

<form ng-submit="submitForm()">
    <label>Nome: </label><input type="text" name="nome" ng-model="user.nome">
    <label>E-mail: </label><input type="text" name="email" ng-model="user.email">
    
    <input type="text" hidden name="email" ng-model="user.telefone">
    <input type="text" hidden name="email" ng-model="user.cpf">
    
    <input type="button" value="Adicionar" ng-click="addItem(user)" />
</form>
<br />

<div ng-repeat="item in items track by $index">
ID: {{item.soma}}<br />
Nome: {{item.nome}}<br />
E-mail: {{item.email}}<br /><br />
<!-- {{item.telefone}} - {{item.cpf}} -->
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#myModal" ng-click="addAtributos(item)">Atributos</button>
<hr />
</div>

<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Atributos</h4>
      </div>
      <div class="modal-body">
        <label>Telefone: </label><input type="text" name="telefone" ng-model="produtoTrib.telefone">
        <label>CPF: </label><input type="text" name="cpf" ng-model="produtoTrib.cpf">
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Atualizar</button>
      </div>
    </div>

  </div>
</div>

</body>

Assuming the system works with user authentication:

How to store the n items entered by a user x

→ How to unhide the above structure, containing all items entered by the user x ?

    
asked by anonymous 02.02.2017 / 11:48

1 answer

1

var app = angular.module('app', []);
app.controller('controlador', function($scope, $http, $rootScope, itensService) {
    $rootScope.userLogado = {id: 666, nome: 'Malandro'}; //Se tivesse autenticação, teria guardado os dados do usuário em algum lugar
    $scope.user = {};
    $scope.produtoTrib = {};
    $scope.items = [];
    var sum = 1;
    $scope.loadItems = function() {
        itensService.getItemsByidUsuario($rootScope.userLogado.id).then(function(obj) {
            $scope.items = obj.data;
        })
    }
    $scope.addItem = function(user) {
        $scope.items.push({
            idUserLogado: $rootScope.userLogado.id, // identifica o malandro que esta logado na hora do cadastro.
            nome: user.nome,
            email: user.email,
            soma: sum++
        });
        user.nome = '';
        user.email = '';
    };


    $scope.addAtributos = function(produto) {
        $scope.produtoTrib = produto;
    };

});

app.service("itemsService", function($http) {
    this.getItemsByidUsuario = function(idUserLogado) {
        //SELECT * FROM items WHERE idUserLogado = {{idUserLogado}}
        return $http.get("api/getItemsByidUsuario/" + idUserLogado);
    }
});
...
<body ng-app="app" ng-controller="controlador" ng-init="loadItems();">
...
    
07.02.2017 / 03:34