2017-06-16 1 views
0

私のコンポーネントの1つにオブジェクトを含む配列を定義しましたが、合計を計算したいのですが、重複する識別子 'rowData'を指定するとエラーが発生します。 可能であれば誰でも私にforeachの使い方を教えてもらえますか、それが可能であっても?私はcliで作成された角度4を使用しています。コンポーネントの角4 forEach()on component.ts

完全なコードは、エラーを得た理由のコンストラクタの前にここに

import { Component, OnInit } from '@angular/core'; 
import { FormBuilder, Validators } from '@angular/forms'; 
import { Router, ActivatedRoute } from '@angular/router'; 
import { Http } from '@angular/http'; 
import { MdSnackBar } from '@angular/material'; 
import { SlimLoadingBarService } from 'ng2-slim-loading-bar'; 
import { LaravelService } from './../laravel.service'; 

@Component({ 
    selector: 'app-single-estimate-section', 
    templateUrl: './single-estimate-section.component.html', 
    styleUrls: ['./single-estimate-section.component.css'] 
}) 
export class SingleEstimateSectionComponent implements OnInit { 

    section: any = []; 
    estimate_id: number; 
    section_id: number; 
    laravelResponse: any; 
    submitButton = false; 
    buttonClicked: string; 

    material_sub_total: number = 0; 
    labour_sub_total: number = 0; 

    rowData: Array<Object> = [{ 
    'title': 'Product' as string, 
    'qty': null as number, 
    'material_margin': null as number, 
    'material_unit_rate': null as number, 
    'labour_unit_rate': null as number 
    }]; 


    rowData.forEach(item => { 
    this.material_sub_total = this.material_sub_total + item.material_unit_rate * item.qty + (item.material_margin/100 * item.material_unit_rate * item.qty); 
    }); 


    public postForm = this.formBuilder.group({ 
    'title': '', 
    'qty': '', 
    'material_margin': '', 
    'material_unit_rate': '', 
    'labour_unit_rate': '' 
    }); 

    onButtonClick(event: string) { 
    this.buttonClicked = event; 
    } 

    onAddNewClick() { 
    this.rowData.push({ 
     'title': '' as string, 
     'qty': null as number, 
     'material_margin': null as number, 
     'material_unit_rate': null as number, 
     'labour_unit_rate': null as number 
    }); 
    } 

    onRemoveRowClick(i: number) { 
    this.rowData.splice(i, 1); 
    } 

    onSubmit(data) { 
    let formData = this.postForm.value; 
    this.laravel.post_request('/edit-estimate-section/' + this.estimate_id + '/' + this.section_id, formData).subscribe(
     (data) => { 
     this.laravelResponse = data.json(); 

     if (this.laravelResponse.status == 1) { 
      this.snackBar.open(this.laravelResponse.message, 'OK', { duration: 3000 }); 
      if (this.buttonClicked == 'update-continue') { 
      this.router.navigateByUrl('/estimates/' + this.estimate_id + '/edit-section/' + this.section_id); 
      } else { 
      this.router.navigateByUrl('/estimates/' + this.estimate_id); 
      } 
     } else { 
      this.snackBar.open(this.laravelResponse.message, 'OK', { duration: 6000 }); 
     } 

     } 
    ); 
    } 

    constructor(
    private router: Router, 
    private activatedRoute: ActivatedRoute, 
    private http: Http, 
    private formBuilder: FormBuilder, 
    private laravel: LaravelService, 
    private snackBar: MdSnackBar, 
    private slimLoadingBarService: SlimLoadingBarService 
) { } 

    ngOnInit() { 

    this.slimLoadingBarService.start(); 

    this.activatedRoute.params.subscribe(
     (params) => { 
     this.estimate_id = params.id; 
     this.section_id = params.section_id; 
     } 
    ); 

    this.laravel.get_request('/get-estimate-section/' + this.estimate_id + '/' + this.section_id).subscribe(
     (data) => { 
     this.laravelResponse = data.json(); 

     if (this.laravelResponse.status == 1) { 
      this.section = this.laravelResponse.data; 

      this.postForm = this.formBuilder.group({ 
      }); 

     } else { 
      this.snackBar.open(this.laravelResponse.message, 'OK', { duration: 6000 }); 
      this.router.navigateByUrl('/404'); 
     } 
     this.slimLoadingBarService.complete(); 

     } 
    ); 

    } 

} 
+3

'this.rowData.forEach ... 'を試しましたか?ところで、あなたは 'forEach'の代わりに' Array.reduce'を使うべきだと思います。 – Diego

+1

もう一つのオプションはパイプです。 https://github.com/fknop/angular-pipes – cgTag

+0

this.rowData.forEachを使用しましたが、まだ動作していません。VSコードで予期しないトークンが表示される@Diego –

答えて

0

あなたはforeachのをしようとして行きます。

あなたのforeachはそうすべきです。このthis.countSubtotal(); wriet int onNgInitメソッド();

countSubtotal(){ 
    this.rowData.forEach(item => { 
     this.material_sub_total = this.material_sub_total + item.material_unit_rate * item.qty + (item.material_margin/100 * item.material_unit_rate * item.qty); 
     }); 
} 

あなたはrowdataにデータを格納していますがrowDataに格納するデータの後​​を呼び出す必要があるとき、私は理解できません。

onAddNewClick() { 
    this.rowData.push({ 
     'title': '' as string, 
     'qty': null as number, 
     'material_margin': null as number, 
     'material_unit_rate': null as number, 
     'labour_unit_rate': null as number 
    }); 
    this.material_sub_total = 0; 
    this.countSubtotal(); 
    } 
+0

ありがとうございます。確かに、それはそこでうまく動作します。しかし、配列の更新があれば計算をバインドできますか?私はできますか? @Shailesh –

+0

実際には、rowDataはフロントエンドテンプレートからバインドされています。フロントエンドから追加すると配列がたくさんあります。 @Shailesh –

+0

@susonwaibaはいその時に配列をバインドすると、この関数は再び値を再計算します。 countSubtotal()関数を呼び出す前にmaterial_sub_totalをリセットする必要があります –

0

ランダムなJS文をクラスの途中に置くことはできません。クラスにはプロパティ定義とメソッドが含まれています。コードはメソッドに入ります。クラス内のランダムなJS文はどういう意味ですか?それはいつ実行されますか?誰がそれを呼ぶだろうか?

calcMaterialSubtotalなどのメソッドの中にrowData.forEachを入れて、どこから呼び出すかを呼び出します。

+0

確かに。しかし、私はフロントエンドから追加できる複数の入力フィールドと配列をバインドしたい、つまり、私はすべての入力フィールドから呼び出すことはできませんので、バインドされたコンポーネントで計算する必要があります。 ?あなたが言うこと。?どうやってするの。提案が必要ですか? –

+0

申し訳ありませんが、あなたの質問を理解できません。 –

+0

私は説明しましょう:ユーザは配列にオブジェクトを追加できます。オブジェクトのすべての要素には入力フィールドがあります。いくつかのフィールドはすべて一緒に計算するために必要な数値入力を取得します。だから私は(keydown)= "calc()"をそれに追加することはできない。各フィールドは3 4入力フィールドを取得するため、100秒になることがあります。だから、私はcolzオブジェクトを更新する配列をバインドする必要があります。ngModelによって入力にバインドされています。それらのすべてに(keydown)= "calc()"を追加するか、これを行う別の方法があります。 –