Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Input,Component, OnInit,ViewChild } from '@angular/core';
- import { DataService } from '../service/data.service';
- import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
- import { Title } from '@angular/platform-browser';
- import { Router,ActivatedRoute } from '@angular/router';
- import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
- import { Subject } from 'rxjs/Subject';
- @Component({
- selector: 'app-menu',
- templateUrl: './menu.component.html',
- styleUrls: ['./menu.component.css']
- })
- export class MenuComponent implements OnInit {
- @ViewChild('content') private content;
- constructor(
- private service: DataService,
- private modalService: NgbModal,
- private route: ActivatedRoute,
- private fb: FormBuilder
- ) { }
- public modal_title:string='';
- public front_error:string=null;
- public modal_error:string=null;
- public listmenu: any;
- public form:FormGroup;
- public listtypemenu:any;
- ngOnInit() {
- this.getListMenu();
- this.listtypemenu = this.route.snapshot.data['typelist'].data;
- this.form = this.fb.group ( {
- id : [null],
- crud : ['N'],
- name: [null , Validators.compose ( [ Validators.required ] )] ,
- type_id: [null , Validators.compose ( [ Validators.required ] )],
- price:[0],
- note:[null]
- } );
- }
- getListMenu() {
- this.service.getListMenu().subscribe(
- res => {
- this.listmenu = res.data;
- },
- err => {
- let resp = JSON.parse(err._body);
- console.log(resp);
- this.front_error = resp;
- }
- )
- }
- addMenu(){
- this.form.patchValue({
- id : null,
- crud : 'N',
- name: null ,
- type_id: null,
- price:0,
- note:null
- });
- this.modal_title='Add Menu';
- this.openModal(this.content);
- }
- editMenu(id){
- this.service.getMenu(id).subscribe(
- res => {
- if(res.success == true){
- this.form.patchValue({
- id : id,
- crud : 'E',
- name: res.data.name ,
- type_id: res.data.type_id,
- price:res.data.price,
- note:res.data.note
- });
- this.modal_title='Edit Menu';
- this.openModal(this.content);
- }else{
- this.front_error = res.message;
- }
- },
- err => {
- let resp = JSON.parse(err._body);
- console.log(resp);
- this.modal_error = resp;
- }
- )
- }
- openModal(content) {
- this.modalService.open(content).result.then((result) => {
- }, (reason) => {
- });
- }
- delete(id){
- this.service.deleteMenu(id).subscribe(
- res => {
- if(res.success == true){
- this.getListMenu();
- }else{
- this.modal_error = res.message;
- }
- },
- err => {
- let resp = JSON.parse(err._body);
- console.log(resp);
- this.front_error = resp;
- }
- )
- }
- save(post){
- if(post.crud == 'N'){
- this.service.saveMenu(post).subscribe(
- res => {
- if(res.success == true){
- this.getListMenu();
- }else{
- this.modal_error = res.message;
- }
- },
- err => {
- let resp = JSON.parse(err._body);
- console.log(resp);
- this.modal_error = resp;
- }
- )
- }else{
- this.service.editMenu(post).subscribe(
- res => {
- if(res.success == true){
- this.getListMenu();
- }else{
- this.modal_error = res.message;
- }
- },
- err => {
- let resp = JSON.parse(err._body);
- console.log(resp);
- this.modal_error = resp;
- }
- )
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement