简体   繁体   中英

Change checkbox use 'Y' and 'N' instead of false and true

I'm using mat-checkbox angular material 7 and I want to change from true false to 'Y' and 'N' when to submit to backend. I make like this but not working.

HTML

    <form #userForm="ngForm" (ngSubmit)="onSubmit(userForm)" class="form-horizontal row-border">
    <input matInput type="text" class="form-control form-input" name="userId"
                                        [ngModel]="selectedRow?.userId" [readonly]="selectedRow?.userId" id="userId"
                                        required>
    <input matInput type="text" class="form-control form-input" name="userName"
                                                [ngModel]="selectedRow?.userName" id="userName" required>
    <mat-checkbox type="text" name="blocked" [ngModel]="selectedRow?.blocked =='true'? 'Y':'N'"
                                                    id="blocked">{{selectedRow?.blocked}}</mat-checkbox>
<button type="submit" mat-flat-button color="primary"                                    ">Submit</button>
</form>

Component

 onSubmit(form: NgForm ){
    let value = this.selectedRow.blocked == true ? 'Y' : 'N';
    let data = form.value;

}
<mat-checkbox type="text" name="blocked" [(ngModel)]="selectedRow?.blocked" id="blocked">{{selectedRow?.blocked}}</mat-checkbox>

Instead to send an object, try to create a class and use it. and in your method which send the data to server (as your "selectedRow"). Anyway, you can get the data from "selectedRow" object, instead of the form. In future, try to use ReactiveForms

// create this class in a new file, outside of your component
    export class User {
      userId: number;
      userName: string;
      blocked: string;
    }

send() {
 let user = new User();
 user.userId = this.selectedRow.userId;     
 user.userName = this.selectedRow.userName;
 user.blocked = this.selectedRow.blocked == true ? 'Y' : 'N';
 // send user to api
}    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM