2017-06-16 44 views
0
rowData: any = [{ 
    '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); 
}); 

我在我的組件中的一個定義的對象的陣列,我想calc下子總但我得到錯誤說重複標識符「rowData」。 如果可能的話,任何人都可以建議我如何使用foreach或者即使有可能嗎?我正在使用cli創建角4。組件的角4的forEach()上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 ...'。順便說一句,我認爲你應該使用'Array.reduce'而不是'forEach' – 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()函數 –

0

你不能在你的班級中間放一個隨機的JS語句。類包含屬性定義和方法。代碼進入方法。一堂課中隨機的JS聲明意味着什麼?何時執行?誰會打電話給他?

把你的rowData.forEach放在一個名爲calcMaterialSubtotal的方法裏面,然後從你要調用它的地方調用它。

+0

肯定的事情。但我想要綁定數組與多個輸入字段,可以從前端添加,這意味着,我只是不能從所有這些輸入字段調用,所以,我需要計算它在組件作爲綁定。 ?你說的話。?怎麼做。需要建議? –

+0

對不起,我不明白你的問題。 – 2017-06-16 14:21:46

+0

讓我解釋一下:用戶可以添加更多的對象到數組中。對象的所有元素都有輸入字段。一些領域得到數字輸入,這是需要一起計算所有。所以我不能在每個人上添加(keydown)=「calc()」。它可能是100s,因爲每個字段都有3 4個輸入字段。所以,我需要綁定數組,它將更新colz對象綁定到由ngModel輸入。我應該爲他們的每一個添加(keydown)=「calc()」,還是有另一種方法來做到這一點。 –