I am building an angular app where I will be adding an item to a table. The added row will be based on the input of an item name, and its quantity.
I am working on preventing the items being added into the table if:
An error message will show up if any of these conditions were not satisfied. I am able to get the error messages to show up, but a row with invalid fields were added.
Note: I am not allowed to disable the "Add Item" button if the conditions are not met.
Here is my code:
<form #order="ngForm">
<table>
<tr>
<td>
<button type="submit" class="btn btn-default" (click)="addItem()">Add
Item</button>
</td>
<td>
<select [(ngModel)]="newItem.name" required name="newItemName" #newItemName="ngModel" minlength="1">
<option *ngFor="let o of options" [ngValue]="o">{{o.name}}</option>
</select>
</td>
<td>Qty</td>
<td>
<input type="text" pattern="[0-9]*" required [(ngModel)]="newItem.quantity"
name="newItemQuantity" #newItemQuantity="ngModel">
</td>
</tr>
</table>
<p *ngIf="newItemName?.errors?.required && order.submitted">Please select an item.</p>
<p *ngIf="newItemQuantity?.errors?.required && order.submitted">Quantity is required.</p>
</form>
<table>
<thead>
<tr>
<th>Item</th>
<th>Qty</th>
<th>Unit Price</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of items; let i = index">
<td><label class="form-control">{{item.name.name}}</label></td>
<td><label class="form-control">{{item.quantity}}</label></td>
<td><label class="form-control">{{item.name.price}}</label></td>
<td></td>
<td><input type="button" value="delete" (click)="removeItem(i)"></td>
</tr>
</tbody>
</table>
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
})
export class AppComponent {
items: Array<any> = [];
newItem: any = {};
options = [
{name: "apples", price: 1.19},
{name: "peaches", price: 1.39},
{name: "pears", price: 1.69},
{name: "plums", price: 1.59}
];
addItem() {
if (this.newItem != undefined) {
this.items.push(this.newItem);
this.newItem = {};
}
}
removeItem(index) {
this.items.splice(index, 1); // remove 1 item at ith place
}
}
Just add a conditional at the begining of your add method:
addItem() {
if(!this.newItem || !this.newItem.name ||
(this.newItem && !this.newItem.name)) {
return;
}
this.items.push(this.newItem);
this.newItem = {};
}
You have to modify addItem()
function to check if both name
and quantity
are defined and are valid. And only add the item if the condition is true.
addItem() {
if (this.newItem && this.newItem.name && this.newItem.quantity &&
this.newItem.quantity > 0 && Number.isInteger(Number(this.newItem.quantity))) {
this.items.push(this.newItem);
this.newItem = {};
}
}
Number.isInteger() method can be used to check if quantity
is a valid integer. This ensures that fractional numbers are not allowed.
Live demo on StackBlitz: https://stackblitz.com/edit/angular-vqcikf
Here is your answer, check wether the newItem object exists (not equal to undefined), then check for the name key & value exists (not equal to undefined and empty string ''), then check if quantity key & value exists (not equal to undefined and empty string ''), then make sure the quantity value is greater than 0.
addItem() {
if(this.newItem && this.newItem.name && this.newItem.quantity && this.newItem.quantity > 0) {
this.newItem.quantity = Number(this.newItem.quantity);
this.items.push(this.newItem);
this.newItem = {};
}
}
Note: JS will take care of comparison between string data and numerical values
Example:
"1.11" > 1 //true
"0.01" < 0 //false
"1.0a" > 0 // undefined (which is evaluated as false)
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.