如何在Angular中显示所选复选框时去掉 ‘逗号’。

huangapple go评论76阅读模式
英文:

How to remove 'commas' while displaying the selected checkboxes in angular

问题

你可以在处理 checkbox 值的时候,将空字符串('')过滤掉,以避免生成额外的逗号。你可以在 onSubmit 方法中修改 checkboxValues 的代码,如下所示:

onSubmit() {
  if (this.form.valid) {
    // ...

    this.checkboxValues = this.checkboxesFormArray.value.filter(value => value !== ''); // Filter out empty strings
    this.form.controls['checkbox'].setValue(this.checkboxValues);

    this.carServiceService.addCar(this.form.value).subscribe(response => {
      // ...
    });

  } else {
    // Form is invalid, display error messages
    this.form.markAllAsTouched();
  }
}

这样,checkboxValues 数组将只包含被选中的复选框的值,而不会有空字符串。这应该解决你在表格中得到额外逗号的问题。

英文:

form-page.component.html :

<h1>CAR SERVICE FORM</h1>

<form [formGroup]="form" (submit)="onSubmit()">
    <div>
        <label for="carmodel">Car Model:</label>
        <input type="text" class="form-control" formControlName="carmodel">
        <div *ngIf="form.controls['carmodel'].touched && form.controls['carmodel'].errors">
          <div *ngIf="form.controls['carmodel'].hasError('required')" class="error">Carmodel is required.</div>
          <div *ngIf="form.controls['carmodel'].hasError('minlength')">Carmodel should be minimum 3 characters.</div>
        </div>
    </div>

    <div>
        <label for="carnum">Car Number:</label>
        <input type="text" class="form-control" formControlName="carnum">
        <div *ngIf="form.controls['carnum'].touched && form.controls['carnum'].errors">
          <div *ngIf="form.controls['carnum'].hasError('required')" class="error">carnum is required.</div>
        </div>
      </div>



    <div>
        <label for="contactNumber">Contact Number:</label>
        <input type="text" class="form-control" formControlName="contactNumber">
        <div *ngIf="form.controls['contactNumber'].touched && form.controls['contactNumber'].errors">
          <div *ngIf="form.controls['contactNumber'].hasError('required')" class="error">Contact number is required.</div>
        </div>
    </div>
  
    <div>
      <label>Type of Service:</label>
      <div>
        <label><input type="radio" name="option" value="Waterwash" formControlName="option"> Waterwash </label>
      </div>
      <div>
        <label><input type="radio" name="option" value="Fullservice" formControlName="option"> Fullservice </label>
      </div>
      <div *ngIf="form.controls['option'].touched && form.controls['option'].invalid">
        <div class="error">Please select an option</div>
      </div>
    </div>
<div>
    <label>Addons:</label>
    <div *ngFor="let cbx of checkboxesFormArray.controls; let i = index">
      <label formArrayName="checkbox">
        <input type="checkbox" [formControlName]="i" (change)="checkedChanged($event)" [value]="checkboxes[i].value">
        {{checkboxes[i].name}}
      </label>
    </div>  
  </div>

    <div>
      <label>State:</label>
      <select formControlName="state" (change)="onStateChange()">
        <option *ngFor="let state of states" [value]="state">{{state}}</option>
      </select>
      <div *ngIf="form.controls['state'].touched && form.controls['state'].invalid">
        <div class="error">Please select a state</div>
      </div>
    </div>
 
    <div>
        <label>City:</label>
        <select formControlName="city">
            <option *ngFor="let city of cities[form.controls['state'].value]" [value]="city">{{city}}</option>
        </select>
        <div *ngIf="form.controls['city'].touched && form.controls['city'].invalid">
            <div class="error">Please select a city</div>
        </div>
    </div>

    <button type="submit" class="btn btn-primary">Submit</button>
    <button type="button" (click)="Reset()">Reset</button>
    <button (click)="goBack()">back</button>


    

</form>
<table>
    <thead>
      <tr>
        <th>Car Model</th>
        <th>Car Number</th>
        <th>Contact Number</th>
        <th>Type of Service</th>
        <th>Options</th>
        <th>State</th>
        <th>City</th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let car of cars">
        <td>{{ car.carmodel }}</td>
        <td>{{ car.carnum }}</td>
        <td>{{ car.contactNumber }}</td>
        <td>{{ car.option }}</td>
        <td>{{ car.checkbox }}</td>
        <td>{{ car.state }}</td>
        <td>{{ car.city }}</td>
      </tr>
    </tbody>
  </table>

form-page.component.ts :

import { Component } from '@angular/core';
import { CommonModule, Location } from '@angular/common';
 
import { FormGroup, FormBuilder,FormControl, Validators, FormArray } from '@angular/forms';
 
import { CarServiceService } from 'src/app/services/car-service.service';
@Component({
  selector: 'app-form-page',
  templateUrl: './form-page.component.html',
  styleUrls: ['./form-page.component.css']
})
export class FormPageComponent {
//
  form: FormGroup;
  states: string[] = ['Tamilnadu', 'Kerala', 'Karnataka','Maharastra'];
  cities: {[key: string]: string[]} = {
    'Tamilnadu': ['Chennai', 'Coimbatore','Madurai'],
    'Kerala': ['Trivandrum','Kochi','Kollam'],
    'Karnataka': ['Bangalore', 'Mysore'],
    'Maharastra': ['Mumbai', 'Pune']
  };
  checkboxes = [
    { value: '10%off First service visit', name: '10%off First service visit' },
    { value: '10%off Waterwash', name: '10%off Waterwash' },
    { value: 'Free AC Inspection', name: 'Free AC Inspection' },
  ];
//  
  cars: any[] = [];
//
  checkboxValues: string[]=[];
//
 
  get checkboxesFormArray() {
    return this.form.controls['checkbox'] as FormArray;
  }
 
  constructor(private fb: FormBuilder,private location : Location,private carServiceService :CarServiceService) {
 
 
    this.form = this.fb.group({
      carmodel :['', [Validators.required, Validators.minLength(3)]],
      carnum :['', [Validators.required]],
      contactNumber: ['', [Validators.required, Validators.pattern(/^\d{10}$/)]],
      option: ['', Validators.required],
      checkbox: new FormArray([], Validators.required),
      state: ['', Validators.required],
      city: ['', Validators.required]
    });
    this.addCheckboxes();
  }
 
 
  addCheckboxes() {
    this.checkboxes.forEach(() =>
      this.checkboxesFormArray.push(new FormControl(''))
    );
  }
 
  goBack():void{
    this.location.back();
  }
 

  onSubmit() {
    if (this.form.valid) {
//      console.log(this.form.value);
 
 
      this.carServiceService.addCar(this.form.value).subscribe(response =>{
        console.log(response);
        this.carServiceService.getCars().subscribe(cars => {
          this.cars = cars; // Set the list of cars to the response data
          console.log(this.cars);
        });  
      });
 
    } else {
      // Form is invalid, display error messages
      this.form.markAllAsTouched();
    }
  }


 
 
  Reset(){
    this.form.reset();
  }
 
  onStateChange() {
    const state = this.form.controls['state'].value;
    this.form.controls['city'].setValue('');
    if (state) {
      this.form.controls['city'].enable();
    } else {
      this.form.controls['city'].disable();
    }
  }
  checkedChanged(e:any) {
    let index = this.checkboxes.findIndex(
      (item) => item.value === e.target.value
    );
    if (e.target.checked) {
      this.checkboxesFormArray.controls[index].setValue(e.target.value);
    } else {
      this.checkboxesFormArray.controls[index].setValue('');
    }
  }

 
}

also got this service named , car-service.service.ts:


import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Form } from '@angular/forms';

@Injectable({
  providedIn: 'root'
})
export class CarServiceService {

  constructor(private http: HttpClient) { }

  addCar(formData : any): Observable<any>{
    return this.http.post<any>('https://localhost:7095/api/Forms/submit-form',formData);
  }
  getCars(): Observable<any[]> {
    return this.http.get<any[]>('https://localhost:7095/api/Forms/get-forms');
  }
  
}

Im trying to post my input values of html form from angular to asp.net core web API and then getting those values back to print them below the html form in a table format. I'm able to post every fields and get back those values but there's a small thing that bothers me. In the checkbox field, If I selected the last two checkboxes, I get the values as

,10%off Waterwash,Free AC Inspection

and on selecting just the last checkbox, I get them like

,,Free AC Inspection

or on selecting the second checkbox alone, I get the values like,

,10%off Waterwash,

Basically I want to get rid of the comma in the form array. Can someone tell me how to do that

答案1

得分: 1

你需要创建一个对象,删除数组中的"empty"值。因此,在提交函数中,您可以使用以下代码:

onSubmit() {
    if (this.form.valid) {
        // 创建一个新对象
        const data = {
            ...this.form.value, //<--表单的所有值
            checkbox: this.form.value.checkbox.filter(x => x)
        };
        // 发送此对象,而不是form.value
        this.carServiceService.addCar(data).subscribe(response => {
            // ...
        });
    }
}

但是,实际上,您的复选框表单数组的值应始终是一个包含true或false的数组,例如[true, true, false]

如果您想要根据复选框的数组值传递给API,您只需要在提交函数中创建一个新对象,将表单的值与"checkbox属性"更改如下:

onSubmit() {
    if (this.form.valid) {
        const data = {
            ...this.form.value, //<--表单的所有值
            checkbox: this.form.value.checkbox
                // 将会是,例如 [true, false, true]
                .map((x: boolean, index: number) => x ? this.checkboxes[index].value : '')
                // 将会是,例如 ['10%off First service visit', '', 'Free AC Inspection']
                .filter((x: string) => x)
                // 将会是,例如 ['10%off First service visit', 'Free AC Inspection']
                .join(',')
                // 将会是 '10%off First service visit,Free AC Inspection'
        };

        this.carServiceService.addCar(data).subscribe(response => {
            // ...
        });
    }
}

这样,您就不需要使用您的checkedChanged函数。

注意:我不知道您是想发送数组还是一个由逗号分隔的字符串(如果需要API中的字符串数组,请删除"join")。

注意2:还有另一种方法,不使用FormArray,而是使用一个简单的FormControl,并使用[(ngModel)]来更改FormControl。可以参考这个Stack Overflow链接

英文:

You need create an object removing the "empty" values of the array. So in submit function you can use

onSubmit() {
    if (this.form.valid) {
      //create a new object
      const data={
        ...this.form.value, //&lt;--all the value of your form
        checkbox:this.form.value.checkbox.filter(x=&gt;x)
      }
      //send this object, not the form.value
      this.carServiceService.addCar(data).subscribe(response =&gt;{
         ...
      }
 }

But really the value of your checkboxesFormArray should be always an array with true or false, e.g. [true,true,false]

As you want to pass to your API the value based in your array checkboxes the only you need are, in submit function, create a new object with the values of the form but the "checkbox property" changed

onSubmit() {
    if (this.form.valid) {
      const data={
        ...this.form.value, //&lt;--all the value of your form
        checkbox:this.form.value.checkbox
             //will be,. e.g. [true,false,true]
             .map((x:boolean,index:number)=&gt;x?this.checkboxes[index].value:&#39;&#39;)
             //will be, e.g. [&#39;10%off First service visit&#39;,&#39;&#39;,&#39;Free AC Inspection&#39;]
             
             .filter((x:string)=&gt;x)
             //will be,e.g. [&#39;10%off First service visit&#39;,&#39;Free AC Inspection&#39;]
             .join(&#39;,&#39;)
             //will be &#39;10%off First service visit,Free AC Inspection&#39;
        }

     this.carServiceService.addCar(data).subscribe(response =&gt;{
        ...
     }
} 

So we needn't use your function checkedChanged

NOTE: I don't know if you want to send the array or a string separate by commas (remove the "join" if you need an array of string in your API.

NOTE2: There're another approach that it's not use a FormArray else a simple FormControl, and use [(ngModel)] to change the FormControl. See this SO

huangapple
  • 本文由 发表于 2023年4月4日 13:59:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定