How check if checkbox is checked? And how sum a value? - ionic-framework

In a code like this, for example, how can I check if the checkboxes were checked and show an alert with the values’ sum? There will be more checkbox than those two.
<ion-list>
<ion-item>
<ion-label>Pepperoni</ion-label>
<ion-checkbox [(ngModel)]=“pepperoni” value=“3.5”
<ion-item>
<ion-label>Sausage< /ion-label>
<ion-checkbox [(ngModel)]=“sausage” value=“4.2”</ion-checkbox>
</ion-item>
<ion-row center>
<ion-col text-center>
<ion-button click=“getValue()”>Value<ion-button>
</ion-col>
</ion-row>
</ion-list>

HTML
<ion-content padding>
<ion-list>
<ion-item *ngFor="let entry of form">
<ion-label>{{entry.val}}</ion-label>
<ion-checkbox slot="end" [(ngModel)]="entry.isChecked"></ion-checkbox>
</ion-item>
</ion-list>
<button ion-button (click)="getValue()">Value</button>
</ion-content>
TS
public form = [
{ val: 'Pepperoni', isChecked: true,value:'3.2' },
{ val: 'Sausage', isChecked: false,value:'4.2' },
{ val: 'Mushroom', isChecked: false,value:'5.2' }
];
getValue(){
let checkedControls = this.form.filter(result=>result.isChecked==true);
let sum = 0;
checkedControls.forEach(result=>{
sum = parseFloat(result.value) + sum;
});
alert(sum);
}
Working demo:https://stackblitz.com/edit/ionic-icvos9

Related

Ionic 5 Result can't show in console log

It can't show in console log, but I can run pass in ionic version 3 or 4. Now I use Ionic 5.
I can't get the point from select in radio. I don't know what wrong when run it's not show any problem.
in file html
<ion-item-sliding *ngFor = "let q of Questionlist ; let i = index" >
<p>{{ q.qname }}</p>
<ion-radio-group>
<ion-row>
<ion-col>
<ion-item text-wrap >
<ion-radio mode="md" value={{q.ansp}} (ionSelect)="mcqAnswer(q.ansp,i)"></ion-radio>
<ion-label> {{q.ans }}</ion-label>
</ion-item>
</ion-col>
<ion-col>
<ion-item text-wrap>
<ion-radio mode="md" value={{q.ans1p}} (ionSelect)="mcqAnswer(q.ans1p,i)" ></ion-radio>
<ion-label>{{q.ans1 }}</ion-label>
</ion-item>
</ion-col>
<ion-col>
<ion-item text-wrap>
<ion-radio mode="md" value = {{q.ans2p}} (ionSelect)="mcqAnswer(q.ans2p,i)"></ion-radio>
<ion-label class= "text-wrap">{{ q.ans2 }}</ion-label>
</ion-item>
</ion-col>
<ion-col>
<ion-item text-wrap>
<ion-radio mode="md" value={{q.ans3p}} (ionSelect)="mcqAnswer(q.ans3p,i)"></ion-radio>
<ion-label>{{q.ans3 }}</ion-label>
</ion-item>
</ion-col>
</ion-row>
</ion-radio-group>
</ion-item-sliding>
</div>
in file ts
mcqAnswer(value, index) {
let anws = { p: value, i: index }
if (this.anwsList[index] !== undefined && index == this.anwsList[index].i) {
this.anwsList[index].p = value
} else {
this.anwsList.push(anws)
}
console.log("=>", value, "=>", index)
console.log("===>", this.anwsList)
}
Thank you for helping me
if you want to get the value of the selected radio button you must use the ionChange event in the ion-radio-group tag.
then you have to pass the $event as a function argument to the .ts file.
I recommend you to log it in the console to see what happens.
I hope it helps.

Value in input field will not display in ionic

I prefilled ion-input with a value but it is not displaying. I am new to ionic but I understand little of it concepts.
Here is what I tried.
.ts
async getSimData() {
try {
let simPermission = await this.sim.requestReadPermission();
if (simPermission == "OK") {
let simData = await this.sim.getSimInfo();
this.simInfo = simData;
this.cards = simData.cards;
}
} catch (error) {
console.log(error);
}
}
html
<ion-card *ngFor= "let card of cards" >
<ion-card-content>
<ion-list>
<ion-item>
<ion-input type="number" name="phone" value="{{ card.phoneNumber }}" #phone disabled="true"></ion-input>
</ion-item>
</ion-list>
</ion-card-content>
</ion-card>
How can I make this appear in the ion-input?
you can also use with ngModel, i checked your code it is also working with value.
In below code both are working, check this : https://stackblitz.com/edit/ionic-eu7tcl
<ion-card *ngFor= "let card of cards" >
<ion-card-content>
<ion-list>
<ion-item>
<ion-input type="number" name="phone" [(ngModel)]="card.phoneNumber" #phone disabled="true"></ion-input>
<ion-input type="number" name="phone" value="{{ card.phoneNumber }}" #phone disabled="true"></ion-input>
</ion-item>
</ion-list>
</ion-card-content>
</ion-card>

ionic 4: <ion-reorder> not working as expected

i'm working with ionic 4 angular 7. I'm using <ion-reorder> to reorder list. Drag n Drop works for the first time fine but when I release the click, item got stuck. After first reorder everything freezes. And I'm unable to attempt reorder for the second time.
Here my .html file
<ion-list lines="none">
<ion-reorder-group disabled="false">
<ion-reorder>
<ion-item>
<ion-thumbnail no-margin item-start>
<img src="../assets/images/5.jpg">
</ion-thumbnail>
<div class="pl-2">
<h5 no-margin>Multan</h5>
<p no-margin>Historical place...</p>
</div>
<ion-buttons slot="end">
<ion-button>
<ion-icon slot="icon-only" name="close"></ion-icon>
</ion-button>
</ion-buttons>
</ion-item>
</ion-reorder>
<ion-reorder>
<ion-item>
<ion-thumbnail no-margin item-start>
<img src="../assets/images/5.jpg">
</ion-thumbnail>
<div class="pl-2">
<h5 no-margin>Multan</h5>
<p no-margin>Historical place...</p>
</div>
<ion-buttons slot="end">
<ion-button>
<ion-icon slot="icon-only" name="close"></ion-icon>
</ion-button>
</ion-buttons>
</ion-item>
</ion-reorder>
<ion-reorder>
<ion-item>
<ion-thumbnail no-margin item-start>
<img src="../assets/images/5.jpg">
</ion-thumbnail>
<div class="pl-2">
<h5 no-margin>Multan</h5>
<p no-margin>Historical place...</p>
</div>
<ion-buttons slot="end">
<ion-button>
<ion-icon slot="icon-only" name="close"></ion-icon>
</ion-button>
</ion-buttons>
</ion-item>
</ion-reorder>
<ion-reorder>
<ion-item>
<ion-thumbnail no-margin item-start>
<img src="../assets/images/5.jpg">
</ion-thumbnail>
<div class="pl-2">
<h5 no-margin>Multan</h5>
<p no-margin>Historical place...</p>
</div>
<ion-buttons slot="end">
<ion-button>
<ion-icon slot="icon-only" name="close"></ion-icon>
</ion-button>
</ion-buttons>
</ion-item>
</ion-reorder>
</ion-reorder-group>
</ion-list>
When I drag n Drop Item. It get stuck when I drop it. After this, everything freezes.
Any Help...?
I'm using
Ionic: 4.10.2
Angular: 7.3.0
I think you need to store your data in a variable and ngFor on these data to build your reorder items.
this.items: Array<img: string; title: string; description: string; icon:
string> = [yourArrayOfObjects];
I think then you need to catch the ionItemReorder event like this
<ion-reorder-group (ionItemReorder)="reorderItems($event)" disabled="false">
and in your .ts the reorderItems() function could be
reorderItems(ev) {
const itemMove = this.items.splice(ev.detail.from, 1)[0];
this.items.splice(ev.detail.to, 0, itemMove);
ev.detail.complete();
}
The key here is to complete the event and you have to do it manually. So the ionItemReorder event callback is a must. So something as simple as this should do the trick:
Typescript :
public onItemReorder({ detail }) {
detail.complete(true);
}
HTML :
<ion-reorder-group (ionItemReorder)="onItemReorder($event)" [disabled]="false">
ionic4 version:
html code:
<ion-content>
<ion-list>
<ion-list-header>
<ion-label>INCLUDE</ion-label>
</ion-list-header>
<ion-reorder-group (ionItemReorder)="reorder($event)" [disabled]="false">
<ion-item *ngFor="let accessory of accessories">
<ion-label>{{accessory}}</ion-label>
<ion-reorder></ion-reorder>
</ion-item>
</ion-reorder-group>
</ion-list>
</ion-content>
typescript code:
accessories = ['test', 'test1', 'test2'];
reorder(event) {
const itemToMove = this.accessories.splice(event.detail.from, 1)[0];
this.accessories.splice(event.detail.to, 0, itemToMove);
}
This will never throw any type of error, it works. I am 100% sure, have used 3-4 times. I hope, this will also help you.
<ion-list >
<ion-item-group (ionItemReorder)="reorder($event)" reorder='true' >
<ion-item *ngFor="let item of file_uri" (click)="openSubMenu(item.bunch)" style="background-color: #F0F0F0">
<ion-avatar item-left >
<img src="assets/{{item.bunch}}.svg">
</ion-avatar>
<h2 color="primary_secound">{{item.bunch}}
</h2>
<p>Click To See Menu of {{item.bunch}}
</p>
<ion-icon name="arrow-dropright" item-right></ion-icon>
</ion-item>
</ion-item-group>
</ion-list>
reorder(event) {
const itemToMove = this.file_uri.splice(event.from, 1)[0];
this.file_uri.splice(event.to, 0, itemToMove);
}
With Angular - Ionic - Typescript
in HTML file/Code:
<ion-reorder-group (ionItemReorder)="reorderItems($event)" disabled="false">
<ion-item *ngFor="let item of yourArray">
<ion-label> {{ item.name }} </ion-label>
<ion-reorder slot="end"></ion-reorder>
</ion-item>
</ion-reorder-group>
Controller or .ts Code:
reorderItems(ev): void {
const itemMove = this.yourArray.splice(ev.detail.from, 1)[0];
this.yourArray.splice(ev.detail.to, 0, itemMove);
ev.detail.complete();
}
As per the above example, If you did not add reorderItems() in your controller it will not work. you can change function name as you like.

Ionic Validation Array Form

I have the below form that is generate dynamically, I am trying to make a validation but is fail
Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'model: sdaDAd'. Current value: 'model: '.
HTML
<div [formGroup]="someForm">
<ion-item-group *ngFor="let att of day; let idx = index">
<ion-item >
<ion-label color="primary">{{att.label}}{{idx+1}}. Day</ion-
label>
<ion-input formControlName="day" type="text" text-right
[(ngModel)]="day[idx].value"></ion-input>
</ion-item>
<ion-item >
<ion-label color="primary">{{att.label}}{{idx+1}}. Exc</ion-label>
<ion-input type="text" formControlName="exc" text-right
[(ngModel)]="exc[idx].value"></ion-input>
</ion-item>
<ion-item >
<ion-label color="primary">{{att.label}}{{idx+1}}.
Hint/Repeats</ion-label>
<ion-input type="text" formControlName="hint" text-right
[(ngModel)]="hint[idx].value"></ion-input>
</ion-item>
<span color=danger float-right ion-button icon-left clear
*ngIf="exc.length > 0"
(click)="removeInputField(idx)"><ion-icon name="close"></ion-icon>
Remove
</span>
</ion-item-group>
<ion-card *ngIf="data_exc">
<ion-item *ngFor="let att of exc; let i=index">
<div class="card-title">Exc: {{att.value}}</div>
<div class="card-title">Hint/Repeats: {{hint[i].value}}</div>
<div class="card-title">Day: {{day[i].value}}</div>
</ion-item>
<div class="card-title">Notes: {{workoutData.notes}}</div>
</ion-card>
<button ion-button (click)="Add()">+Add</button>
<button ion-button (click)="goTo()" >Preview</button> <br>
<ion-toolbar>
<ion-item>
<ion-textarea placeholder="Tap here to enter a new note"
[(ngModel)]="workoutData.notes" formControlName="notes"
autocomplete="on" autocorrect="on"></ion-textarea>
</ion-item>
</ion-toolbar>
<button [disabled]="!someForm.valid" ion-button full
(click)="presentConfirmCustomWorkout()" type="submit" >Submit</button>
</div>
Validation to check if the form is empty so I will not get empty values in array
this.someForm = formbuilder.group({
'day': [this.day.value, Validators.compose([Validators.required])],
'exc': [this.exc.value, Validators.compose([Validators.required])],
'hint': [this.hint.value, Validators.compose([Validators.required])],
'notes': ['', Validators.compose([Validators.required])],
});

Ionic radio-group with Angular Reactive Forms

I'm working on a project in Ionic and need to use reactive forms from Angular 5. I need to allow a user to select a language preference and I'm doing that with radio-group and ion-radio, but when I make changes to the form, I'm getting
ERROR Error: There is no FormControl instance attached to form control element with name: 'preferredLanguage'
Here is my template with the form
<ion-header>
<ion-navbar>
<ion-title>User Profile</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<form *ngIf="user" [formGroup]="profileForm" (ngSubmit)="saveUser()">
<ion-item>
<ion-label>Display Name</ion-label>
<ion-input type="text" formControlName="displayName"></ion-input>
</ion-item>
<ion-item>
<ion-label>First Name</ion-label>
<ion-input type="text" formControlName="firstName"></ion-input>
</ion-item>
<ion-item>
<ion-label>Last Name</ion-label>
<ion-input type="text" formControlName="lastName"></ion-input>
</ion-item>
<ion-item>
<ion-label>Email</ion-label>
<ion-input type="text" formControlName="email"></ion-input>
</ion-item>
<ion-item>
<ion-label>Street</ion-label>
<ion-input type="text" formControlName="street"></ion-input>
</ion-item>
<ion-item>
<ion-label>City</ion-label>
<ion-input type="text" formControlName="city"></ion-input>
</ion-item>
<ion-item>
<ion-label>State</ion-label>
<ion-input type="text" formControlName="state"></ion-input>
</ion-item>
<ion-item>
<ion-label>Zip Code</ion-label>
<ion-input type="text" formControlName="zip"></ion-input>
</ion-item>
<ion-list radio-group formControlName="preferredLanguage">
<ion-list-header>
Select Preferred Language
</ion-list-header>
<ion-item>
<ion-label>English</ion-label>
<ion-radio value="en-US"></ion-radio>
</ion-item>
<ion-item>
<ion-label>Spanish</ion-label>
<ion-radio value="es-US"></ion-radio>
</ion-item>
</ion-list>
<button type="submit" ion-button>Submit Profile</button>
</form>
</ion-content>
Here is my method that initializes the form
initProfileForm() {
this.profileForm = this.formBuilder.group({
displayName: [this.user.profile.displayName, Validators.required],
firstName: [this.user.profile.firstName, Validators.required],
lastName: [this.user.profile.lastName, Validators.required],
email: [this.user.profile.email, Validators.required],
street: [this.user.profile.street, Validators.required],
city: [this.user.profile.city, Validators.required],
state: [this.user.profile.state, Validators.required],
zip: [this.user.profile.zip, Validators.required] ,
preferredLanguage: [this.user.profile.preferredLanguage,
Validators.required]
});
}
Has anyone else had this issue and how did you solve it?
I ended up coming up with a solution. This blog helped me come up with an answer: https://robferguson.org/blog/2017/11/19/ionic-3-and-forms/ Apparently, radio-group doesn't work with formControlName so instead I ended up using this:
<ion-list radio-group [formControl]="profileForm.controls['preferredLanguage']">
<ion-list-header>
Select Preferred Language
</ion-list-header>
<ion-item>
<ion-label>English</ion-label>
<ion-radio value="en-US"></ion-radio>
</ion-item>
<ion-item>
<ion-label>Spanish</ion-label>
<ion-radio value="es-US"></ion-radio>
</ion-item>
</ion-list>
This fixed the issue so there is no longer an error.
Make some amendments in your HTML code:
<ion-item>
<ion-label>English</ion-label>
<ion-radio value="en-US" formControlName="preferredLanguage"></ion-radio>
</ion-item>
<ion-item>
<ion-label>Spanish</ion-label>
<ion-radio value="es-US" formControlName="preferredLanguage"></ion-radio>
</ion-item>
So basically instead of using it on parent use it individually for each radio button. This worked for me.
Hope it helps!
get preferredLanguage() {
return this.profileForm.get('preferredLanguage').value;
}
set preferredLanguage(ev: CustomEvent) {
this.profileForm.get('preferredLanguage').patchValue(ev.detail.value);
}
<ion-radio-group [value]="preferredLanguage" (ionChange)="preferredLanguage = $event">
<ion-item>
<ion-label>English</ion-label>
<ion-radio value="en-US"></ion-radio>
</ion-item>
<ion-item>
<ion-label>Spanish</ion-label>
<ion-radio value="es-US"></ion-radio>
</ion-item>
</ion-radio-group>