java—使用第一个元素的id创建两个不同的元素

sauutmhj  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(298)

我有两个表fnc和analysis\ fnc,我有一个表单创建一个fnc元素。我想在创建fnc的元素时,使用fnc表的id(n\u fnc)为我创建analyze\u fnc的元素。但是id在后端自动生成。我在前端使用angular 9,在后端使用spring boot和postgres数据库。表2为fnc表。

  1. **Front end**
  2. Fnc form html :
  3. <h3>Creation Fiche de non conformité</h3>
  4. <div [hidden]="submitted" style="width: 400px;">
  5. <form (ngSubmit)="onsubmit()">
  6. <div class="container">
  7. <div class="row">
  8. <div class="form-group">
  9. <label for="name">Cloturé par :</label>
  10. <input type="text" class="form-control" id="cloture_par" required [(ngModel)]="fnc.cloture_par" name="cloture_par" >
  11. </div>
  12. <div class="form-group">
  13. <label for="name">Date Cloture</label>
  14. <input type="date" class="form-control" id="date_cloture" required [(ngModel)]="fnc.date_cloture" name="date_cloture">
  15. </div>
  16. </div>
  17. <div class="row">
  18. <div class="form-group">
  19. <label for="name">Code Article</label>
  20. <select (change)="changeOtherToggles($event.target.value)" class="form-control" id="codeart" required [(ngModel)]="fnc.codeart" name="codeart">
  21. <option>Select</option>
  22. <option *ngFor="let o of origine">{{o.codeart}}</option>
  23. </select>
  24. </div>
  25. <div class="form-group">
  26. <label for="name">Libelle Article</label>
  27. <input type="text" class="form-control" id="libelle_art" required [(ngModel)]="fnc.libelle_art" name="libelleart">
  28. </div>
  29. <div class="form-group">
  30. <label for="name">Collection</label>
  31. <input type="text" class="form-control" id="collection" required [(ngModel)]="fnc.collection" name="collection">
  32. </div>
  33. </div>
  34. <div class="row">
  35. <div class="form-group">
  36. <label for="name">Type de detection</label>
  37. <input type="text" class="form-control" id="type" required [(ngModel)]="fnc.type" name="type">
  38. </div>
  39. <div class="form-group">
  40. <label for="name">Detecté par</label>
  41. <input type="text" class="form-control" id="detect_par" required [(ngModel)]="fnc.detect_par" name="detect_par">
  42. </div>
  43. <div class="form-group">
  44. <label for="name">Date de detection</label>
  45. <input type="date" class="form-control" id="date_detection" required [(ngModel)]="fnc.date_detection" name="datedetection">
  46. </div>
  47. </div>
  48. <div class="row">
  49. <div class="form-group">
  50. <label for="name">lieu de detection</label>
  51. <input type="text" class="form-control" id="lieu_detection" required [(ngModel)]="fnc.lieu_detection" name="lieudetection">
  52. </div>
  53. <div class="form-group">
  54. <label for="name">Document de référence </label>
  55. <input type="text" class="form-control" id="doc_ref" required [(ngModel)]="fnc.doc_ref" name="doc_ref">
  56. </div>
  57. <div class="form-group">
  58. <label for="name">Libelle de defaut </label>
  59. <input type="text" class="form-control" id="libelle_def" required [(ngModel)]="fnc.libelle_def" name="libelle_def">
  60. </div>
  61. </div>
  62. <div class="row">
  63. <div class="form-group">
  64. <label for="name">Description de non conformité </label>
  65. <input type="text" class="form-control" id="desc_non_confir" required [(ngModel)]="fnc.desc_non_confir" name="descnonconfir">
  66. </div>
  67. <div class="form-group">
  68. <label for="name">Description principale</label>
  69. <input type="text" class="form-control" id="desc_principale" required [(ngModel)]="fnc.desc_principale" name="descprincipale">
  70. </div>
  71. <div class="form-group">
  72. <label for="name">Defaut secondaire </label>
  73. <input type="text" class="form-control" id="defaut_secon" required [(ngModel)]="fnc.defaut_secon" name="defautsecon">
  74. </div>
  75. </div>
  76. <div class="row">
  77. <div class="form-group">
  78. <label for="name">Quantite litigieuse </label>
  79. <input type="text" class="form-control" id="quantite_litigieuse" required [(ngModel)]="fnc.quantite_litigieuse" name="quantitelitigieuse">
  80. </div>
  81. <div class="form-group">
  82. <label for="name">Quantite de non conformité </label>
  83. <input type="text" class="form-control" id="quantite_non_conf" required [(ngModel)]="fnc.quantite_non_conf" name="quantitenonconf">
  84. </div>
  85. <div class="form-group">
  86. <label for="name">Unité </label>
  87. <input type="text" class="form-control" id="unite" required [(ngModel)]="fnc.unite" name="unite">
  88. </div>
  89. </div>
  90. </div>
  91. <div class="row1">
  92. <div class="form-group">
  93. <label for="name">Nature </label>
  94. <select class="form-control" id="nature" required [(ngModel)]="fnc.nature" name="nature">
  95. <option>Select</option>
  96. <option *ngFor="let o of origineart ">{{o.nature}}</option>
  97. </select>
  98. </div>
  99. <div class="form-group">
  100. <label for="name">N°phase </label>
  101. <select class="form-control" id="n_phase" required [(ngModel)]="fnc.n_phase" name="n_phase">
  102. <option>Select</option>
  103. <option *ngFor="let o of origineart">{{o.n_phase}}</option>
  104. </select>
  105. </div>
  106. <div class="form-group">
  107. <label for="name">N°Operation </label>
  108. <select class="form-control" id="n_operation" required [(ngModel)]="fnc.n_operation" name="n_operation">
  109. <option>Select</option>
  110. <option *ngFor="let o of origineart ">{{o.n_operation}}</option>
  111. </select>
  112. </div>
  113. </div>
  114. <button class="btn btn-success">Créer</button>
  115. </form>
  116. </div>
  117. **Fnc form .ts**
  118. ` import { Component, OnInit } from '@angular/core';
  119. import { map } from 'rxjs/operators';
  120. import { Breakpoints, BreakpointObserver } from '@angular/cdk/layout';
  121. import { Fnc } from '../models/fnc.model';
  122. import { FncService } from '../services/fnc.service';
  123. import { Router } from '@angular/router';
  124. import { Observable } from 'rxjs';
  125. import { Origine } from '../models/origine.model';
  126. import { OrigineService } from '../services/origine.service';
  127. import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material/dialog';
  128. import { UseExistingWebDriver } from 'protractor/built/driverProviders';
  129. import { Statement } from '@angular/compiler';
  130. import { Pipe, PipeTransform } from '@angular/core';
  131. import { analyseFnc } from '../models/analysefnc.model';
  132. import { analyseFncService } from '../services/analysefnc.service';
  133. @Component({
  134. selector: 'app-creation-fnc',
  135. templateUrl: './creation-fnc.component.html',
  136. styleUrls: ['./creation-fnc.component.css']
  137. })
  138. export class CreationFncComponent implements OnInit {
  139. /**Based on the screen size, switch from standard to one column per row */
  140. origine:Origine[]=[];
  141. fnc:Fnc=new Fnc();
  142. analysefnc:analyseFnc=new analyseFnc();
  143. a:number;
  144. submitted=false;
  145. origineart:Origine[]=[];
  146. constructor(private analyseFncService:analyseFncService,private origineservice:OrigineService,private fncservice:FncService,private router:Router) {}
  147. ngOnInit(){
  148. this.origine=[];
  149. this.origineart = [];
  150. this.origineservice.obs.subscribe((response)=>this.origineservice.maptoorigine(response,this.origine));
  151. // Overrride default filter behaviour of Material Datatable
  152. this.origineservice.obs.subscribe((response)=>this.origineservice.maptoorigine(response,this.origineart));
  153. }
  154. changeOtherToggles(codearti:String) {
  155. console.log(codearti);
  156. this.origineart =[];
  157. console.log(this.origineart);
  158. console.log(this.origine);
  159. this.origineart =this.origine.filter(
  160. org => org.codeart === codearti);
  161. console.log(this.origineart);
  162. console.log(this.origine);
  163. }
  164. onsubmit(){
  165. this.submitted=true;
  166. this.fnc.etat="en cours";
  167. this.fncservice.create(this.fnc).subscribe(
  168. data=>console.log(data),error=>console.error(error)
  169. );
  170. this.analysefnc.n_fnc=this.fnc.n_fnc;
  171. this.analysefnc.statut="A créer";
  172. this.analyseFncService.create(this.analysefnc).subscribe(
  173. data=>console.log(data),error=>console.error(error)
  174. );
  175. this.fnc=new Fnc();
  176. this.router.navigate(['/nav-quali/home-quali']);
  177. }
  178. }
  179. @Pipe({ name: "filter" })
  180. export class ManualFilterPipe implements PipeTransform {
  181. transform(value: any, args?: any): any {
  182. // Remove the duplicate elements (this will remove duplicates
  183. let uniqueArray = value.filter(function (el, index, array) {
  184. return array.indexOf (el) == index;
  185. });
  186. return uniqueArray; }
  187. }`
  188. **Fnc form service**
  189. import { Injectable } from '@angular/core';
  190. import { HttpClient, HttpHeaders } from '@angular/common/http';
  191. import { Observable } from 'rxjs';
  192. import { Fnc } from '../models/fnc.model';
  193. const baseUrl = 'http://192.168.100.9:8081/fncs';
  194. @Injectable({
  195. providedIn: 'root'
  196. })
  197. export class FncService {
  198. obs:Observable<Object>;
  199. constructor(private http: HttpClient) {this.getAll(); }
  200. maptofnc(response,fncs:Fnc[]):void{
  201. let fnc:Fnc;
  202. response.forEach(element => {
  203. fnc =new Fnc();
  204. fnc.n_fnc=element.n_fnc;
  205. fnc.etat=element.etat;
  206. fnc.cloture_par=element.cloture_par;
  207. fnc.date_cloture=element.date_cloture;
  208. fnc.codeart=element.codeart;
  209. fnc.libelle_art=element.libelle_art;
  210. fnc.collection=element.collection;
  211. fnc.type=element.type;
  212. fnc.detect_par=element.detect_par;
  213. fnc.date_detection=element.date_detection;
  214. fnc.lieu_detection=element.lieu_detection;
  215. fnc.libelle_def=element.libelle_def;
  216. fnc.doc_ref=element.doc_ref;
  217. fnc.desc_non_confir=element.desc_non_confir;
  218. fnc.desc_principale=element.desc_principale;
  219. fnc.quantite_litigieuse=element.quantite_litigieuse;
  220. fnc.quantite_non_conf=element.quantite_non_conf;
  221. fnc.defaut_secon=element.defaut_secon;
  222. fnc.unite=element.unite;
  223. fnc.nature=element.nature;
  224. fnc.n_phase=element.n_phase;
  225. fnc.n_operation=element.n_operation;
  226. fncs.push(fnc);
  227. });
  228. }
  229. maptofncbyetat(response,fncs:Fnc[],etat:String):void{
  230. let fnc:Fnc;
  231. response.forEach(element => {
  232. if(element.etat==etat){
  233. fnc =new Fnc();
  234. fnc.n_fnc=element.n_fnc;
  235. fnc.etat=element.etat;
  236. fnc.cloture_par=element.cloture_par;
  237. fnc.date_cloture=element.date_cloture;
  238. fnc.codeart=element.codeart;
  239. fnc.libelle_art=element.libelle_art;
  240. fnc.collection=element.collection;
  241. fnc.type=element.type;
  242. fnc.detect_par=element.detect_par;
  243. fnc.date_detection=element.date_detection;
  244. fnc.lieu_detection=element.lieu_detection;
  245. fnc.libelle_def=element.libelle_def;
  246. fnc.doc_ref=element.doc_ref;
  247. fnc.desc_non_confir=element.desc_non_confir;
  248. fnc.desc_principale=element.desc_principale;
  249. fnc.quantite_litigieuse=element.quantite_litigieuse;
  250. fnc.quantite_non_conf=element.quantite_non_conf;
  251. fnc.defaut_secon=element.defaut_secon;
  252. fnc.unite=element.unite;
  253. fnc.nature=element.nature;
  254. fnc.n_phase=element.n_phase;
  255. fnc.n_operation=element.n_operation;
  256. fncs.push(fnc);
  257. }});
  258. }
  259. getAll(): Observable<any> {
  260. this.obs=this.http.get('http://192.168.100.9:8081/fncs');
  261. return this.http.get(baseUrl);
  262. }
  263. get(id): Observable<any> {
  264. return this.http.get(`${baseUrl}/${id}`);
  265. }
  266. create(data): Observable<any> {
  267. return this.http.post(baseUrl, data);
  268. }
  269. update(id:number, data): Observable<any> {
  270. const httpOptions = {
  271. headers: new HttpHeaders({
  272. 'Content-Type': 'application/json',
  273. 'Authorization': 'Basic bWFuYWdlcjptYW5hZ2Vy'
  274. })
  275. };
  276. return this.http.put(`${baseUrl}/${id}`, data,httpOptions);
  277. }
  278. delete(id): Observable<any> {
  279. return this.http.delete(`${baseUrl}/${id}`);
  280. }
  281. deleteAll(): Observable<any> {
  282. return this.http.delete(baseUrl);
  283. }
  284. findByTitle(title): Observable<any> {
  285. return this.http.get(`${baseUrl}?title=${title}`);
  286. }
  287. }
  288. `
  289. **Analyse_fnc service**
  290. import { Injectable } from '@angular/core';
  291. import { HttpClient } from '@angular/common/http';
  292. import { Observable } from 'rxjs';
  293. import { analyseFnc } from '../models/analysefnc.model';
  294. import { pluck } from 'rxjs/operators';
  295. const baseUrl = 'http://192.168.100.9:8081/analyseFnc';
  296. @Injectable({
  297. providedIn: 'root'
  298. })
  299. export class analyseFncService {
  300. obs:Observable<Object>;
  301. constructor(private http:HttpClient) {
  302. this.getAll();
  303. }
  304. maptoplanaction(response,analysefnc:analyseFnc[]):void{
  305. let pln:analyseFnc;
  306. response.forEach(element => {
  307. pln =new analyseFnc();
  308. pln.id=element.id;
  309. pln.classification=element.classification;
  310. pln.suivi_par=element.suivi_par;
  311. pln.cause_princ=element.cause_princ;
  312. pln.ouvert_par=element.ouvert_par;
  313. pln.cause_secon=element.cause_secon;
  314. pln.n_action=element.n_action;
  315. pln.action_libel=element.action_libel;
  316. pln.resp=element.resp;
  317. pln.etat=element.etat;
  318. pln.quantite=element.quantite;
  319. pln.date_fin_act=element.date_fin_act;
  320. pln.visa=element.visa;
  321. pln.n_fnc=element.n_fnc;
  322. analysefnc.push(pln);
  323. });
  324. }
  325. getAll(): Observable<any> {
  326. this.obs=this.http.get('http://192.168.100.9:8081/analyseFnc');
  327. return this.http.get(baseUrl);
  328. }
  329. get(id): Observable<any> {
  330. return this.http.get(`${baseUrl}/${id}`);
  331. }
  332. create(data): Observable<any> {
  333. return this.http.post(baseUrl, data);
  334. }
  335. update(id, data): Observable<any> {
  336. return this.http.put(`${baseUrl}/${id}`, data);
  337. }
  338. delete(id): Observable<any> {
  339. return this.http.delete(`${baseUrl}/${id}`);
  340. }
  341. deleteAll(): Observable<any> {
  342. return this.http.delete(baseUrl);
  343. }
  344. findByTitle(title): Observable<any> {
  345. return this.http.get(`${baseUrl}?title=${title}`);
  346. }
  347. }
  348. #back end FNc and analyse fnc is the same i just change /fncs to /analysefncs #
  349. private FncService fncservice;
  350. @RequestMapping(value="/fncs", method=RequestMethod.GET)
  351. public List<Fnc> getAllFncs() {
  352. return fncservice.getAllFnc();
  353. }
  354. @RequestMapping(value="/fncs/{id}", method=RequestMethod.GET)
  355. public Fnc getFnc(@PathVariable int id){
  356. return fncservice.getFnc(id);
  357. }
  358. @RequestMapping(value="/fncs", method=RequestMethod.POST)
  359. public void addFnc(@RequestBody Fnc fnc){
  360. fncservice.addFnc(fnc);
  361. }
  362. @RequestMapping(value="/fncs/{id}", method=RequestMethod.PUT)
  363. public void updateFnc(@PathVariable int id,@RequestBody Fnc fnc)
  364. {
  365. fncservice.updataFnc(id,fnc);
  366. }
  367. @RequestMapping(value="/fncs/{id}", method=RequestMethod.DELETE)
  368. public void deleteFnc(@PathVariable int id){
  369. fncservice.deleteFnc(id);
  370. }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题