2017-10-17 9 views
-1

Y at-il un problème avec mon code? Y at-il une erreur de syntaxe puisque mon erreur indique la balise de fermeture inattendue "tr". Cela peut arriver lorsque l'étiquette a déjà été fermée par une autre étiquette. Pour plus d'informations, voir https://www.w3.org/TR/html5 syntax.html # closure-elements-that-have-implied-end-tags ("" fa fa-trash-o "aria-hidden =" true "> Supprimer [ERROR ->]. . ont la fermeture que je voulais afficher mat_order dans une liste quelqu'un peut-il aider à ceAffichage des données sur Tr en Angulaire 4

<form class="form-horizontal" [formGroup]="myForm" (ngSubmit)="onCreateOrders()" > 
    <div class="card-block" formArrayName="rows"> 
    <table class="table table-bordered table-striped"> 
     <thead> 
     <tr> 
      <th>Material Name</th> 
      <th>Unit</th> 
      <th>Quantity</th> 
      <th>Total</th> 
      <th>Action</th> 
     </tr> 
     </thead> 
     <tbody> 
     <tr *ngFor="let row of myForm.controls.rows.controls; let i = index [formGroupName]="i"; let mat_order of mat_orders"> 
      <td> 
      {{mat_order.name}} 
      </td> 
      <td><input type="text" class="col-md-10" formControlName="unit" required></td> 
      <td><input type="number" class="col-md-6" formControlName="quantity" required></td> 
      <td><input type="number" class="col-md-6" formControlName="quantity" required></td> 
      <td> 
      <button type="button" class="btn btn-sm btn-danger" (click)="onDeleteRow(i)"><i class="fa fa-trash-o" aria-hidden="true"></i> Remove</button> 
      </td> 
     </tr> 
     </tbody> 
    </table> 
    <button type="submit" class="btn btn-primary float-right" [disabled]="!myForm.valid">Save Order</button> 
    </div> 
</form> 

ts

export class PurchaseOrderCreateComponent implements OnInit { 
    myForm: FormGroup; 
    formData; 
    subscription: any; 
    id: number; 
    projects: any; 
    orders: any; 
    suppliers: any; 
    mat_orders: any; 

    constructor(private fb: FormBuilder, 
       private route: ActivatedRoute, 
       private router: Router, 
       private injector: Injector, 
       private projectsService: ProjectsService, 
       private purchaseOrderService: PurchaseOrderService, 
       private suppliersService: SuppliersService) { 

    this.myForm = this.fb.group({ 
     rows: this.fb.array([]) 
    }) 

    } 

    ngOnInit() { 
    this.route.params 
     .subscribe((params: Params) => { 
     this.id = +params['id']; 
     this.projectsService = this.injector.get(ProjectsService); 
     this.projectsService.getProject(this.id) 
     .subscribe(
      (data:any) => { 
      this.projects = data; 
      console.log(data); 

      }, 
      error => { 
      alert("ERROR"); 
      }) 
     }); 

     this.initGroup(); 
     this.getAllOrders(); 
     this.getAllSuppliers(); 
     // this.getSupplierByProjID(this.id); 
    } 


    initGroup() { 
    let rows = this.myForm.get('rows') as FormArray; 
    rows.push(this.fb.group({ 
     project_id: [this.id], 
     material_id: [''], 
     unit: [''], 
     quantity: [''] 
    })) 
    } 

    getAllOrders(){ 
    this.subscription = this.purchaseOrderService.getAll() 
     .subscribe(
      (data:any) => { 
      this.orders = data.orders; 
      console.log(data); 
      }, 
      error => { 
      alert("Error"); 
      console.log(error); 
      }); 
    } 

    getAllSuppliers(){ 
    this.subscription = this.suppliersService.getAll() 
     .subscribe(
      (data:any) => { 
      this.suppliers = data.suppliers; 
      console.log(data); 
      }, 
      error => { 
      alert("Error"); 
      console.log(error); 
      }) 
    } 

    onSelectSupplier(form: NgForm) { 
    const project_id = this.id; 
    const supplier_id = form.value.supplier; 
    this.subscription = this.purchaseOrderService.selectSupplier(project_id, supplier_id) 
    .subscribe(
      (data:any) => { 
      this.mat_orders = data.supplies; 
      let mat_orders = data.supplies; 
      console.log(mat_orders); 
      // location.reload(); 
      }, 
      error => { 
      alert("Error"); 
      console.log(error); 
      }) 
    } 

    onDeleteRow(rowIndex) { 
    let rows = this.myForm.get('rows') as FormArray; 
    rows.removeAt(rowIndex) 
    } 

    onCreateOrders(){ 
    this.formData = this.myForm.get('rows').value 
    console.log(this.formData) 
    }; 
} 
+0

@Sajeetharan je n'en ai qu'un. La ligne de myForm.controls.rows.controls est pour les lignes tandis que le mat_order doit afficher ses données. –

+0

Pouvez-vous fournir un plunkr ou stackblitz? – brijmcq

+0

Je trouve que "laisse mat_order de mat_orders" n'est pas correct. Pouvez-vous essayer après avoir enlevé cela? –

Répondre

0

mise à jour réponse:.?

tag Changer tr comme ci-dessous :

<tr [formGroupName]="i" *ngFor="let row of myForm.controls.rows.controls; let i = index; let mat_order of mat_orders"></tr> 

utiliser dans votre balise tr:

Le problème est [formGroupName]="i" format non valide de l'intérieur de la tr Tag

<form class="form-horizontal" [formGroup]="myForm" (ngSubmit)="onCreateOrders()" > 
    <div class="card-block" formArrayName="rows"> 
     <table class="table table-bordered table-striped"> 
      <thead> 
       <tr> 
        <th>Material Name</th> 
        <th>Unit</th> 
        <th>Quantity</th> 
        <th>Total</th> 
        <th>Action</th> 
       </tr> 
      </thead> 
      <tbody> 
       <tr *ngFor="let row of myForm.controls.rows.controls; let i = index; let mat_order of mat_orders"> 
        <td> 
         {{mat_order.name}} 
        </td> 
        <td><input type="text" class="col-md-10" formControlName="unit" required></td> 
        <td><input type="number" class="col-md-6" formControlName="quantity" required></td> 
        <td><input type="number" class="col-md-6" formControlName="quantity" required></td> 
        <td> 
         <button type="button" class="btn btn-sm btn-danger" (click)="onDeleteRow(i)"><i class="fa fa-trash-o" aria-hidden="true"></i> Remove</button> 
        </td> 
       </tr> 
      </tbody> 
     </table> 
     <button type="submit" class="btn btn-primary float-right" [disabled]="!myForm.valid">Save Order</button> 
    </div> 
</form> 
+0

Je ne peux plus ajouter la ligne –

+0

Je vais mettre à jour ma réponse le problème est d'utiliser un format invalide à l'intérieur de la balise tr '[formGroupName] =" i "' – Chandru

+0

pouvez-vous partager votre déclaration formGroup en tapuscrit? – Chandru

0

Mettez votre [formGroupName] = "i" côté de * ngFor et supprime mat_orders dans * ngFor (itérer deux tableaux dans ngFor n'est pas correct). Ci-dessous extrait vous aidera

<tr *ngFor="let row of myForm.controls.rows.controls; let i = index" [formGroupName]="i"> 
    <td> 
      {{mat_orders[i].name}} 
    </td> 
</tr> 
+0

Je ne peux pas maintenant afficher {{mat_order.name}} –

+0

ok, Êtes-vous en mesure d'ajouter une nouvelle ligne? –

+0

Est un tableau/objet mat_order. –