Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. import { Input,Component, OnInit,ViewChild } from '@angular/core';
  2. import { DataService } from '../service/data.service';
  3. import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
  4. import { Title } from '@angular/platform-browser';
  5. import { Router,ActivatedRoute } from '@angular/router';
  6. import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
  7. import { Subject } from 'rxjs/Subject';
  8.  
  9.  
  10. @Component({
  11. selector: 'app-menu',
  12. templateUrl: './menu.component.html',
  13. styleUrls: ['./menu.component.css']
  14. })
  15. export class MenuComponent implements OnInit {
  16. @ViewChild('content') private content;
  17. constructor(
  18. private service: DataService,
  19. private modalService: NgbModal,
  20. private route: ActivatedRoute,
  21. private fb: FormBuilder
  22. ) { }
  23. public modal_title:string='';
  24. public front_error:string=null;
  25. public modal_error:string=null;
  26. public listmenu: any;
  27. public form:FormGroup;
  28. public listtypemenu:any;
  29. ngOnInit() {
  30. this.getListMenu();
  31. this.listtypemenu = this.route.snapshot.data['typelist'].data;
  32. this.form = this.fb.group ( {
  33. id : [null],
  34. crud : ['N'],
  35. name: [null , Validators.compose ( [ Validators.required ] )] ,
  36. type_id: [null , Validators.compose ( [ Validators.required ] )],
  37. price:[0],
  38. note:[null]
  39. } );
  40. }
  41.  
  42. getListMenu() {
  43. this.service.getListMenu().subscribe(
  44. res => {
  45. this.listmenu = res.data;
  46. },
  47. err => {
  48. let resp = JSON.parse(err._body);
  49. console.log(resp);
  50. this.front_error = resp;
  51. }
  52. )
  53. }
  54. addMenu(){
  55. this.form.patchValue({
  56. id : null,
  57. crud : 'N',
  58. name: null ,
  59. type_id: null,
  60. price:0,
  61. note:null
  62. });
  63. this.modal_title='Add Menu';
  64. this.openModal(this.content);
  65. }
  66. editMenu(id){
  67. this.service.getMenu(id).subscribe(
  68. res => {
  69. if(res.success == true){
  70. this.form.patchValue({
  71. id : id,
  72. crud : 'E',
  73. name: res.data.name ,
  74. type_id: res.data.type_id,
  75. price:res.data.price,
  76. note:res.data.note
  77. });
  78. this.modal_title='Edit Menu';
  79. this.openModal(this.content);
  80. }else{
  81. this.front_error = res.message;
  82. }
  83. },
  84. err => {
  85. let resp = JSON.parse(err._body);
  86. console.log(resp);
  87. this.modal_error = resp;
  88. }
  89. )
  90. }
  91. openModal(content) {
  92. this.modalService.open(content).result.then((result) => {
  93. }, (reason) => {
  94. });
  95. }
  96. delete(id){
  97. this.service.deleteMenu(id).subscribe(
  98. res => {
  99. if(res.success == true){
  100. this.getListMenu();
  101. }else{
  102. this.modal_error = res.message;
  103. }
  104. },
  105. err => {
  106. let resp = JSON.parse(err._body);
  107. console.log(resp);
  108. this.front_error = resp;
  109. }
  110. )
  111. }
  112. save(post){
  113. if(post.crud == 'N'){
  114. this.service.saveMenu(post).subscribe(
  115. res => {
  116. if(res.success == true){
  117. this.getListMenu();
  118. }else{
  119. this.modal_error = res.message;
  120. }
  121. },
  122. err => {
  123. let resp = JSON.parse(err._body);
  124. console.log(resp);
  125. this.modal_error = resp;
  126. }
  127. )
  128. }else{
  129. this.service.editMenu(post).subscribe(
  130. res => {
  131. if(res.success == true){
  132. this.getListMenu();
  133. }else{
  134. this.modal_error = res.message;
  135. }
  136. },
  137. err => {
  138. let resp = JSON.parse(err._body);
  139. console.log(resp);
  140. this.modal_error = resp;
  141. }
  142. )
  143. }
  144. }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement