ช่องทำเครื่องหมายแสดงรายการค่าในแบบฟอร์ม

ฉันเพิ่งเริ่มใช้ Angular 2 ดังนั้นฉันต้องการคำแนะนำจากคุณเพื่อให้บรรลุการพัฒนา ฉันกำลังสร้าง/อัปเดตส่วนประกอบของฟอร์ม ฉันมีรายการช่องทำเครื่องหมายที่มีค่าเริ่มต้น (จะเลือกหรือไม่ ขึ้นอยู่กับบริการ) นี่คือรหัสของฉัน:

@Component({
  selector: 'app-campaign',
  template: `
  <form [formGroup]="campaignForm" (ngSubmit)="saveCampaign()" novalidate>
    <label for="formations">Formations</label>
    <ul *ngFor="let formation of formations">
      <li>
        <label>
          <input 
            type="checkbox" 
            [attr.checked]="formation.formationCochee == 1 ? 'checked' : null">
            {{formation.formation}}
        </label>
      </li>
    </ul>
    <input 
      type="submit" 
      class="btn btn-primary" 
      value="Save" 
      [disabled]="campaignForm.invalid"/>
  </form
  `,
  styleUrls: ['./campaign.component.css'],
  providers: [CampaignService, FormationService]
})
export class CampaignComponent implements OnInit {

  id: number = 0;
  campaignForm: FormGroup;        
  title: string = "New campaign";
  errorMessage: any;
  formations: Formation[];

  constructor(
    private _fb: FormBuilder,
    private _campaignService : CampaignService,
    private _formationService: FormationService
    ) { 
      this.campaignForm = this._fb.group({  
        id: ''
        , description: ["", Validators.required]
        // ...
      });

      if (this._avRoute.snapshot.params["id"]) {
        this.id = parseInt( this._avRoute.snapshot.params["id"]);
      }
    }

  ngOnInit() {
    // Case : Update Form
    if(this.id > 0){ 
      this.title = 'Update';    
      this._formationService.selectedFormations(this.id)
      .subscribe(formations =>
        this.formations=formations['records']
      );
    } 
    // Case : Create Form
    else {
      this._formationService.listActiveFormations()
      .subscribe(formations =>
        this.formations=formations['records']
      );
    }
  }

  saveCampaign(){ 
    this._campaignService.saveCampaign(this.campaignForm.value)
    .subscribe(campaign => {
        this._router.navigate(['campaigns']);
     }, error => this.errorMessage = error )
  }
}

คำถามของฉัน:

  • ฉันจะตั้งค่าข้อมูลเริ่มต้นสำหรับรายการช่องทำเครื่องหมายได้อย่างไร

  • เป็นไปได้หรือไม่ที่จะอัปเดตค่าเริ่มต้นเหล่านี้เมื่อผู้ใช้เลือกหรือยกเลิกการเลือกช่อง

  • FormArray เป็นตัวเลือกที่ดีในการแก้ไขปัญหาของฉันหรือไม่

  • และโปรดแจ้งให้เราทราบหากคุณเห็นพฤติกรรมที่ไม่ดีในโค้ดของฉัน ฉันไม่ได้รับผลตอบแทนใด ๆ ... ฉันถามมาก แต่ตอนนี้ฉันติดอยู่ระยะหนึ่งแล้ว ...

ป.ล. ฉันกำลังพยายามทำโดยไม่มีส่วนประกอบที่มีอยู่เพื่อพัฒนาทักษะ Angular2 ของฉัน

ขอบคุณสำหรับคำแนะนำของคุณ


person amt59    schedule 21.11.2017    source แหล่งที่มา


คำตอบ (2)


คุณสามารถใช้ FormArray สำหรับกรณีนี้ เพื่อวนซ้ำข้อมูลใน formations และพุชแต่ละออบเจ็กต์เป็น FormGroup ไปยัง FormArray ในแบบฟอร์มของคุณ

หรือคุณสามารถเพียงแค่ส่งแบบฟอร์ม โดยเติม FormArray ของคุณด้วยค่า formations

เมื่อใช้ตัวเลือกหลัง คุณจะวนซ้ำอาร์เรย์และแสดงช่องทำเครื่องหมายเหมือนกับที่คุณทำ คุณสามารถใช้ [attr.checked] แทน [(ngModel)] แทนได้ ด้วยเหตุนี้ คุณจะต้องเพิ่ม [ngModelOptions]="{standalone: true} เนื่องจากเป็นรูปแบบที่โต้ตอบได้ ดังนั้นรายการของคุณควรมีลักษณะดังนี้:

<ul>
  <li *ngFor="let formation of formations">
    <label>
      <input 
        type="checkbox" 
        [(ngModel)]="formation.formationCochee"
        [ngModelOptions]="{standalone: true}">
        {{formation.formation}}
    </label>
  </li>
</ul>

เมื่อคุณสร้างแบบฟอร์ม ให้เพิ่ม FormArray ว่างสำหรับอาร์เรย์ของคุณ:

this.campaignForm = this.fb.group({
  formations: this.fb.array([])
});

จากนั้นเมื่อคุณส่งแบบฟอร์ม คุณสามารถแทนที่ FormArray ที่ว่างเปล่าด้วย formations ของคุณ:

saveCampaign(){ 
  this.campaignForm.setControl('formations', this.fb.array(this.formations))
  this._campaignService.saveCampaign(this.campaignForm.value)
    .subscribe(campaign => {
       this._router.navigate(['campaigns']);
    }, error => this.errorMessage = error )
}
person AJT82    schedule 22.11.2017

  • ฉันจะตั้งค่าข้อมูลเริ่มต้นสำหรับรายการช่องทำเครื่องหมายของฉันได้อย่างไร

     [attr.checked]="formation.formationCochee">
    

ใน JS คุณมีค่า ความจริง และ เท็จ ตัวอย่างเช่น 1 เป็นค่าความจริง ซึ่งก็คือ is true Angular จะเข้าใจสิ่งที่คุณต้องการทำ และทำเครื่องหมายในช่องสำหรับคุณ

  • เป็นไปได้หรือไม่ที่จะอัปเดตค่าเริ่มต้นเหล่านี้เมื่อผู้ใช้ทำเครื่องหมายหรือยกเลิกการทำเครื่องหมายในช่อง ?

ไม่ หากผู้ใช้อัปเดตค่าเริ่มต้น แสดงว่าไม่ใช่ค่าเริ่มต้นอีกต่อไป ! ไม่เช่นนั้นฉันไม่ได้รับคำถามของคุณ คุณช่วยเรียบเรียงใหม่ได้ไหม

  • FormArray เป็นตัวเลือกที่ดีในการแก้ไขปัญหาของฉันหรือไม่

ปัญหาของคุณคืออะไรกันแน่? คุณไม่ได้ระบุประเด็นใด ๆ คุณเพียงถามคำถาม ...

  • และโปรดแจ้งให้เราทราบหากคุณเห็นพฤติกรรมที่ไม่ดีในโค้ดของฉัน

    <ul *ngFor="let formation of formations">

คุณควรทำซ้ำบนแท็ก li แทน : แท็ก ul/ol เป็นเพียง คอนเทนเนอร์รายการ ในขณะที่ li คือ รายการ

<label>
      <input 
        type="checkbox" 
        [attr.checked]="formation.formationCochee == 1 ? 'checked' : null">
        {{formation.formation}}
    </label>

ป้ายกำกับควร ไม่ มีอินพุต ป้ายกำกับมีไว้เพื่ออธิบายอินพุต ซึ่งหมายความว่าสามารถโต้ตอบกับอินพุตด้วยแอตทริบิวต์ for ได้

person Community    schedule 21.11.2017