Skip to content

Instantly share code, notes, and snippets.

@rickymuvel
Last active July 11, 2017 16:53
Show Gist options
  • Save rickymuvel/8ddc4d14d90877329447ddde9c0aa835 to your computer and use it in GitHub Desktop.
Save rickymuvel/8ddc4d14d90877329447ddde9c0aa835 to your computer and use it in GitHub Desktop.
Problema
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
import { Settings } from '../config';
@Injectable()
export class UbigeoService {
constructor(private http:Http,
private sett:Settings) { }
cargarUbigeo(term:string){
let obj = {term: term};
let body = JSON.stringify(obj);
let headers = new Headers({
'Content-Type': 'application/json'
});
console.log("Esta linea se puede leer cuando el autocomplete");
console.log(`${this.sett.url}/ubigeo`); //http://ventas.dev/api/ubigeo
return this.http.post(`${this.sett.url}/ubigeo`, body, { headers })
.map(res => {
console.log( res.json() );
return res.json();
});
}
}
<h2 class="titulo">Usuarios</h2>
<p-growl [(value)]="msjs" [style]="{'margin-top': '53px'}"></p-growl>
<p-dialog header="Usuarios" [(visible)]="display_modal" [width]="1000" [responsive]="true" [modal]="true" (onShow)="limpiarForm()" [style]="{'margin-top':'30px'}">
<div class="row top-30">
<div class="col-md-12">
<form [formGroup]="frmUsuario" novalidate="novalidate" (ngSubmit)="crearUsuario()">
<div class="row">
<div class="col-md-6">
<div class="form-group row" [ngClass]="{'has-error': !frmUsuario.get('ubigeo').valid && frmUsuario.get('ubigeo').touched}">
<label class="control-label col-md-3 derecha">Ubigeo</label>
<div class="col-md-9">
<div [ngClass]="{'input-icon right': (!frmUsuario.get('ubigeo').valid && frmUsuario.get('ubigeo').touched ) }">
<i [ngClass]="{'fa fa-warning tooltips': (!frmUsuario.get('ubigeo').valid && frmUsuario.get('ubigeo').touched ) }" data-original-title="Seleccione el Ubigeo"></i>
<!-- <input type="text" class="form-control" formControlName='ubigeo' [(ngModel)]="usuario.ubigeo" name='ubigeo' placeholder="Seleccione el Ubigeo"> -->
<p-autoComplete [(ngModel)]="usuario.ubigeo" formControlName='ubigeo' [suggestions]="resultados" (completeMethod)="Buscar($event)" [inputStyleClass]="autocomplete"></p-autoComplete>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div style="text-align:right">
<button type="submit" class="btn red-sunglo" [disabled]="!frmUsuario.valid">Crear usuario</button>
</div>
</div>
</div>
</form>
</div>
</div>
</p-dialog>
<div class="row">
<div class="col-md-12">
<p-dataTable [value]="Usuarios" selectionMode="single" resizableColumns="true" [rows]="7" [paginator]="true" [pageLinks]="3" [rowsPerPageOptions]="[5,10,20]" scrollable="true" scrollWidth="100%">
<p-column [sortable]="true" field="id" header="ID" [style]="{'width':'50px'}"></p-column>
<p-column [sortable]="true" field="dni" header="DNI" [style]="{'width':'80px'}"></p-column>
<p-column [sortable]="true" field="ap_paterno" header="Apellido Paterno" [style]="{'width':'120px'}"></p-column>
<p-column [sortable]="true" field="ap_materno" header="Apellido Materno" [style]="{'width':'120px'}"></p-column>
<p-column [sortable]="true" field="nombres" header="Nombres" [style]="{'width':'120px'}"></p-column>
<p-column [sortable]="true" field="fecnac" header="Fecha de Nac" [style]="{'width':'100px'}"></p-column>
<p-column [sortable]="true" field="est_civil" header="Estado Civil" [style]="{'width':'80px'}"></p-column>
<p-column [sortable]="true" field="fec_ingreso" header="Fecha de Ingreso" [style]="{'width':'80px'}"></p-column>
<p-column [sortable]="true" field="movil" header="Movil" [style]="{'width':'80px'}"></p-column>
<p-column [sortable]="true" field="fijo" header="Fijo" [style]="{'width':'60px'}"></p-column>
<p-column [sortable]="true" field="direccion" header="Dirección" [style]="{'width':'120px'}"></p-column>
<p-column [sortable]="true" field="ubigeo" header="Ubigeo" [style]="{'width':'120px'}"></p-column>
<p-column [sortable]="true" field="email_corp" header="Email corporativo" [style]="{'width':'100px'}"></p-column>
<p-column [sortable]="true" field="email_per" header="Email personal" [style]="{'width':'100px'}"></p-column>
<p-column [sortable]="true" field="login" header="Login" [style]="{'width':'80px'}"></p-column>
<p-column [sortable]="true" field="contacto_emergencia" header="Contacto de emergencia" [style]="{'width':'120px'}"></p-column>
<p-column [sortable]="true" field="telef_contacto" header="Teléfono de Contacto" [style]="{'width':'80px'}"></p-column>
<p-column [sortable]="true" field="perfil" header="Perfil" [style]="{'width':'80px'}"></p-column>
<p-column [sortable]="true" field="turno" header="Turno" [style]="{'width':'80px'}"></p-column>
</p-dataTable>
</div>
</div>
<div class="row top-30">
<div class="col-sm-6">
<div style="">
<button class="btn red-sunglo" (click)="display_modal = true">Crear Usuarios</button>
</div>
</div>
</div>
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { NgForm, FormGroup, FormControl, Validators } from "@angular/forms";
// cargamos servicios
import { UsuarioService } from '../../../../../../services/usuario.service';
import { UbigeoService } from '../../../../../../services/ubigeo.service';
// cargamos la interface
import { UsuarioInterface } from '../../../../../../interfaces/usuario.interface';
import { Message } from 'primeng/primeng';
@Component({
selector: 'app-usuario',
templateUrl: './usuario.component.html',
host: {'(window:keydown)': 'hotkeys($event)'}
})
export class UsuarioComponent implements OnInit {
// host: {'(window:keydown)': 'hotkeys($event)'}
hotkeys(event){
// ALT + C
if(event.altKey && event.keyCode==67){
this.display_modal = true;
}
}
msjs:Message[] = [];
resultados:string[] = [];
Usuarios:UsuarioInterface[] = [];
usuario:UsuarioInterface = {
dni:"41234160",
ap_paterno:"Muñoz",
ap_materno:"Velásquez",
nombres:"Luis Enrique",
fecnac: new Date("1982-04-14"),
est_civil:"",
fec_ingreso: new Date("2017-08-15"),
movil:"958969230",
fijo:"5374222",
direccion:"Av. Universitaria 7760",
ubigeo:"Comas",
email_corp:"lmunoz@kobsa.com.pe",
email_per:"rickymuvel@gmail.com",
login:"rickymuvel",
contacto_emergencia:"Nadia",
telef_contacto:"5374222",
perfil:"",
turno:""
}
estado_civil:string[] = [ "Soltero", "Casado", "Viudo", "Divorciado" ];
turnos:string[] = [ "Mañana", "Tarde", "Noche" ];
perfiles:string[] = [ "Call Center", "Jefe de cuenta", "Jefe de venta", "Administrador" ];
display_modal = false;
frmUsuario:FormGroup;
constructor(private router:Router,
private _us:UsuarioService,
private _ubs:UbigeoService) {
this.frmUsuario = new FormGroup({
'dni': new FormControl('', Validators.required),
'ap_paterno': new FormControl('', Validators.required),
'ap_materno': new FormControl('', Validators.required),
'nombres': new FormControl('', Validators.required),
'fecnac': new FormControl('', Validators.required),
'est_civil': new FormControl('', Validators.required),
'fec_ingreso': new FormControl('', Validators.required),
'movil': new FormControl('', Validators.required),
'fijo': new FormControl('', Validators.required),
'direccion': new FormControl('', Validators.required),
'ubigeo': new FormControl('', Validators.required),
'email_corp': new FormControl('', Validators.required),
'email_per': new FormControl('', Validators.required),
'login': new FormControl('', Validators.required),
'contacto_emergencia': new FormControl('', Validators.required),
'telef_contacto': new FormControl('', Validators.required),
'perfil': new FormControl('', Validators.required),
'turno': new FormControl('iurno', Validators.required)
});
}
ngOnInit() {
// cargamos los usuarios del servidor
this.getUsuarios();
}
limpiarForm(){
this.frmUsuario.reset( this.usuario );
setTimeout(()=>{
document.getElementById("dni").focus();
},500);
}
Buscar(event){
this._ubs.cargarUbigeo( event.query );
}
crearUsuario(){
this._us.setUsuario( this.usuario )
.subscribe(data =>{
console.log(data);
})
}
getUsuarios(){
this.showSuccess( "Espere", "Estamos obteniendo los datos" );
this._us.getUsuarios()
.subscribe(data=>{
this.Usuarios = data.usuarios;
this.clear();
});
}
showSuccess( header, mensaje ) {
this.msjs = [];
this.msjs.push({severity:'success', summary: header, detail: mensaje});
}
showInfo( header, mensaje ) {
this.msjs = [];
this.msjs.push({severity:'info', summary: header, detail: mensaje});
}
showWarn( header, mensaje ) {
this.msjs = [];
this.msjs.push({severity:'warn', summary: header, detail: mensaje});
}
showError( header, mensaje ) {
this.msjs = [];
this.msjs.push({severity:'error', summary: header, detail: mensaje});
}
clear() {
this.msjs = [];
}
}
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { Settings } from '../config';
import { UsuarioInterface } from '../interfaces/usuario.interface';
import 'rxjs/add/operator/map';
@Injectable()
export class UsuarioService {
constructor(private http:Http,
private sett:Settings) { }
getUsuarios(){
return this.http.get(`${this.sett.url}/usuario`)
.map(res =>{
return res.json();
})
}
setUsuario( form:UsuarioInterface ){
let body = JSON.stringify(form);
let headers = new Headers({
'Content-Type': 'application/json'
});
return this.http.post(`${this.sett.url}/usuario`, body, { headers })
.map(res => {
return res.json();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment