How to pick a specific array object in IonicFramework? - ionic-framework

output sample: output
In the furtherModules function, under the main module Environmental Compliance Management there is two sub modules which is Chemical Management and PPE Management, so when the user click Chemical Management it should go to a new page and when the user click PPE Management it should go to another page.I don't know how to do that. I really need help on this.
home.html code:
<ion-content class="outer-content">
<ion-card>
<ion-card-header>
{{content}}
</ion-card-header>
<ion-card-content>
<ion-segment [(ngModel)]="content" color="dark">
<ion-segment-button value="Environment Compliace Management">
<ion-icon name="flask"></ion-icon>
</ion-segment-button>
<ion-segment-button value="Health & Safety Management">
<ion-icon name="medkit"></ion-icon>
</ion-segment-button>
</ion-segment>
<ion-list style="margin: 0" inset>
<button ion-item *ngFor="let sContent of getContentItems(content)" (click)="furtherModules()">
<ion-icon item-start [name]="sContent.icon" color= "primary"></ion-icon>
{{sContent.name}}
</button>
</ion-list>
</ion-card-content>
</ion-card>
</ion-content>
home.ts code:
import {Component} from '#angular/core';
import { IonicPage, NavController, NavParams, Platform } from 'ionic-angular';
import {ModuleListPage} from '../module-list/module-list';
import { identifierName } from '#angular/compiler';
#Component({
templateUrl: 'home.html'
})
export class HomePage{
content="MODULES";
constructor(public navCtrl: NavController, public navParams: NavParams) {}
items: any = {
'Environmental Compliance Management': [
{
name: 'Chemical Management',
icon: 'nuclear',
},
{
name: 'PPE Management',
icon: 'man'
}
],
'Health & Safety Management': [
{
name: 'Emergency Incident Management',
icon: 'alert'
},
{
name: 'Machinery Management',
icon: 'construct'
},
{
name: 'Risk Management',
icon: 'cog'
}
]
};
getContentItems(type:any){
return this.items[type];
}
furtherModules(){
}
}

First need to identify which page are we directing to, for an example in my case i generate a dummy page which is 'MyAwesomePage' and 'MyAnotherAwesomePage' then in .ts file need to do something like this
{
name: 'Chemical Management',
icon: 'nuclear',
goTo: 'MyAwesomePage'
},
{
name: 'PPE Management',
icon: 'man',
goTo: 'MyAnotherAwesomePage'
}
public furtherModules(sContent:any): void{
// Validate here if sContent.goTo is a valid page
this.navCtrl.push(sContent.goTo, {_sContent: sContent});
}
after that, in .html file need to do something like this
<ion-list style="margin: 0" inset>
<button ion-item *ngFor="let sContent of getContentItems(content)" (click)="furtherModules(sContent)">
<ion-icon item-start [name]="sContent.icon" color= "primary"></ion-icon>
{{sContent.name}}
</button>
</ion-list>

Ok i just understand what do you want to achieve
You must create a page for both Environment Compliace Management and Health & Safety Management using cli below
ionic g page Environment Compliace Management
ionic g page Health & Safety Management
refer to this link for more details,better to read the documentation first.
Add your page to your app.module.ts refer to this tutorial i dont need to explain it
third do something like below
In your html file put some input paramers to your furthermodules function
(click)="furtherModules(content)
In your ts file do something like below
furtherModules(type:any){
if('Environment Compliace Management'){
this.navCtrl.push(*name of your page to environment compliace management*)
}
else{
this.navCtrl.push(*name of your page to Health & Safety Management*)
}
}

Related

Make a profile page

I want to create a profile page that has an image with a button to switch the image.
That button goes to a “definitions page” and there is a group of avatars for the user to select and when the avatar is selected it appears in the profile page.
I already have the definitions page but I don't know how the information of the image selected goes to the profile page. I'm trying with NavigationExtras but seems it doesn't work.
import { Component, OnInit } from '#angular/core';
import { NavController } from '#ionic/angular';
import { Router, NavigationExtras } from '#angular/router';
#Component({
selector: 'app-defini',
templateUrl: './defini.page.html',
styleUrls: ['./defini.page.scss'],
})
export class DefiniPage{
selectArray = [
{
"image":"assets/imgs/avatar/badass.jpg",
"isSelected": false
},
{
"image":"assets/imgs/avatar/chld.jpg",
"isSelected": false
},
{
"image":'assets/imgs/avatar/man.jpg',
"isSelected": false
},
{
"image":'assets/imgs/avatar/panda.jpg',
"isSelected": false
},
{
"image":"assets/imgs/avatar/pguim.jpg",
"isSelected": false
},
{
"image":"assets/imgs/avatar/woman.jpg",
"isSelected": false
},
{
"image":"assets/imgs/avatar/pinguim.jpg",
"isSelected": false
}
];
constructor(public navCtrl: NavController, private router: Router) {
}
btnActivate(ind) {
for(let i=0;i<this.selectArray.length;i++){
this.selectArray[i].isSelected = false;
}
this.selectArray[ind].isSelected = true;
}
public verAvatar(image) {
let navigationExtras: NavigationExtras;
navigationExtras = {
state: {
avatar: this.selectArray[image]
}
};
this.router.navigate(['selectArray'], navigationExtras);
}
ion-button{
border-radius: 0%;
height:80px;
width: 80px;
padding: 0%;
}
h5{
margin-left: 12px;
}
.selected{
background-color:#fe00334f;
}
<ion-header>
<ion-toolbar>
<ion-buttons slot="start">
<ion-back-button defaultHref="/tab3" text="Alterar Avatar"></ion-back-button>
</ion-buttons>
</ion-toolbar>
</ion-header>
<ion-content [fullscreen]="true">
<ion-text>
<h5>Selecione o seu Avatar</h5>
</ion-text>
<ion-grid >
<ion-row>
<ion-col >
<ion-button fill="clear" [ngClass]="{'selected':avatar.isSelected}" *ngFor="let avatar of selectArray;let i = index" (click)="btnActivate(i)" >
<img src="{{avatar.image}}">
</ion-button>
</ion-col>
</ion-row>
</ion-grid>
</ion-content>
Remember that you are in Ionic framework. Do not use the angular router. Use the ionic controller.
NOTE: Without seeing your routing module definitions, I am simply presuming that 'selectArray' is the correct path. This could also be why your app is not working. Run your app in browser and look for errors from console.log.
this.navController.navigateForward('selectArray', navigationExtras);
Maybe you can try with passing data with the use of a modal to choose the avatar. Or you can create a service with all your profiles data (avatars included) and modify it there. Services are really good if you'll need those data in multiples places in your app, not only your profile page.
There is a lot of tutorial for service in ionic or passing data between page or modal.

How to load other page inside of an ionic segment?

anyone knows how to load other page inside ion-segment. I;m using ionic v4
I have three pages which i am trying to display inside of ionic segments (profile, vaccination, development). I want to keep the functionality of the pages segregated for easier maintainability.
Here is the childdetails page
childdetails.page.html
<ion-content padding>
<ion-toolbar>
<ion-segment (ionChange)="segmentChanged($event)" [(ngModel)]="segment" color="warning">
<ion-segment-button value="profile" (ionSelect)="goToProfilePage()">
Profile
</ion-segment-button>
<ion-segment-button value="vaccination">
Vaccination
</ion-segment-button>
<ion-segment-button value="development">
Development
</ion-segment-button>
</ion-segment>
</ion-toolbar>
<div [ngSwitch]="segment">
<ion-list *ngSwitchCase="'profile'">
<<**I would like to include addchild.html here**>>
</ion-list>
</div>
</ion-content>
childdetails.page.ts
import { Component, OnInit, ViewChild } from '#angular/core';
#Component({
selector: 'app-childdetails',
templateUrl: './childdetails.page.html',
styleUrls: ['./childdetails.page.scss'],
})
export class ChilddetailsPage implements OnInit {
/**
segment = 0;
*/
segment: string = "vaccination";
constructor() {}
ngOnInit() {
}
async segmentChanged() {
this.segment}
}
I don’t want the content to be loading from inside the same page that contains the segment like below code. since each of those will be near about 150-200 lines of code. its better to keep them in separate pages.
<div [ngSwitch]="segment">
<ion-list *ngSwitchCase="'profile'">
<p> I dont want it to be like this </p>
</ion-list>
</div>
Anyone know how to do this ? Thank you in advance

How may I create a dynamic nav bar with a common button and title in ionic 3?

I am developing an IONIC-3 application, whether I want a dynamic nav bar with a title and a common button. For this reason, I have created a .ts file named
commonBtn.ts. Here is the code.
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
import { LoginPage } from '../login/login';
#Component({
selector: 'common-button',
templateUrl: 'commonBtn.html'
})
export class CommonBtnPage {
constructor(public navCtrl: NavController) { }
logOut() {
this.navCtrl.setRoot(LoginPage);
}
}
And its html is :-
<ion-header>
<ion-navbar>
<ion-title> I want to create a dynamic name </ion-title>
<ion-buttons end>
<button ion-button icon-only (click)="logOut()">
<ion-icon name="log-out"></ion-icon>
</button>
</ion-buttons>
</ion-navbar>
</ion-header>
Now, my question is that, How may I change the title name dynamically means when user come into "Home Page", title will be "Home" or user come into "About" page, title will be "About".
We can manage this in ionic v1 via $rootScope but here in Ionic v3, how can I resolve this ?
Also I am giving the html of Home page
<ion-header>
<ion-navbar>
<ion-title>Home</ion-title>
<common-button></common-button>
</ion-navbar>
</ion-header>
<ion-content padding>
<button ion-button (click) = 'clickHere()'> Click </button>
</ion-content>
Please suggest me what to do.
Generally, you need to use Input decorator. Check Component Interactions.
In CommonBtnPage,
import { Component,Input } from '#angular/core';
import { NavController } from 'ionic-angular';
import { LoginPage } from '../login/login';
#Component({
selector: 'common-button',
templateUrl: 'commonBtn.html'
})
export class CommonBtnPage {
#Input()title:string;//here
constructor(public navCtrl: NavController) { }
logOut() {
this.navCtrl.setRoot(LoginPage);
}
}
In the html, set the title,
<ion-header>
<ion-navbar>
<ion-title>{{title}}</ion-title>
<ion-buttons end>
<button ion-button icon-only (click)="logOut()">
<ion-icon name="log-out"></ion-icon>
</button>
</ion-buttons>
</ion-navbar>
</ion-header>
You can set the component and send the title as:
<common-button [title]="'home'"></common-button>

Ionic 2: View does not update based on model change

I have a very simple page with a couple of controls.
My issue is that the page does not pickup changes to the model when the icon in upper right corner is clicked. This toggles the showFilterPane variable, which again should show or hide a div based on *ngIf="showFilterPane".
I have another page just like this one working, and I can not figure out why this isn't.
Any tips?
(I've tried using the ChangeDetectorRef.detectChanges(); which works, but then the rangeslider will not work. The draggable point doesn't update, or does not move to where you tap.)
The page:
<ion-header>
<ion-navbar>
<ion-title>MY AO</ion-title>
<ion-buttons end>
<button ion-button (click)="toggleFilterPane()" icon-only>
<ion-icon name="options"></ion-icon>
</button>
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content>
<div class="container">
<div class="left">
<div *ngIf="isSearching" class="spinner-container">
<ion-spinner></ion-spinner>
</div>
<!-- put content here -->
</div>
<div class="right" *ngIf="showFilterPane">
<ion-list inset>
<ion-list-header>BANA</ion-list-header>
<ion-item>
<ion-select multiple="true" [(ngModel)]="woTrackFilter">
<ion-option>1</ion-option>
<ion-option>2</ion-option>
<ion-option>3</ion-option>
<ion-option>4</ion-option>
<ion-option>5</ion-option>
</ion-select>
</ion-item>
</ion-list>
<ion-list inset>
<ion-list-header>TEKNIKSLAG</ion-list-header>
<ion-item>
<ion-select multiple="true" [(ngModel)]="woDisciplineFilter">
<ion-option>Signal</ion-option>
<ion-option>Bana</ion-option>
<ion-option>EL</ion-option>
<ion-option>Tele</ion-option>
</ion-select>
</ion-item>
</ion-list>
<ion-list inset>
<ion-list-header>DAGAR</ion-list-header>
<ion-item>
<ion-range min="10" max="80" step="4" [(ngModel)]="woDaysFilter">
<ion-label range-left>10</ion-label>
<ion-label range-right>80</ion-label>>
</ion-range>
</ion-item>
</ion-list>
<button ion-button (click)="doSearch()">Search</button>
</div>
</div>
</ion-content>
The component:
import { Component } from '#angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { WorkOrderDashboardPage } from "../work-order-dashboard/work-order-dashboard";
#Component({
selector: 'page-work-order-list',
templateUrl: 'work-order-list.html'
})
export class WorkOrderListPage {
private isSearching: boolean = false;
private showFilterPane: boolean=false;
private woTrackFilter: string[];
private woDisciplineFilter: string[];
private woDaysFilter: number;
constructor(public navCtrl: NavController, public navParams: NavParams) {
// Initialize storage providers here
}
ionViewDidLoad() {
console.log('ionViewDidLoad WorkOrderListPage');
}
toggleFilterPane(): void {
this.showFilterPane = !this.showFilterPane;
}
viewWorkOrder(event, workOrder): void {
this.navCtrl.push(WorkOrderDashboardPage, { workOrder: workOrder });
}
doSearch(): void {
console.log(this.woTrackFilter);
console.log(this.woDisciplineFilter);
console.log(this.woDaysFilter);
}
}
UPDATE: Found workaround
I tried creating a separate app, where the exact same code is working. That lead me to think something wasn't right on the LoginPage, the page that called setRoot() to the above page.
The login code looked like this:
WLAuthorizationManager.login("UserLogin", data).then(() => {
// Success
console.log("Logged in");
this.navCtrl.setRoot(WorkOrderListPage);
},
(err) => {
// failed
console.error(err);
this.showError("Username or password is incorrect");
})
I then figured it might be some Zone issue, and wrapped the setRoot call in zone.run() like this:
WLAuthorizationManager.login("UserLogin", data).then(() => {
// Success
console.log("Logged in");
this.zone.run(() =>
this.navCtrl.setRoot(WorkOrderListPage)
);
},
(err) => {
// failed
console.error(err);
this.showError("Username or password is incorrect");
})
After that the view started to respond as expected. I feel this is a bit of a hack. Can someone shed some light as to what is happening here?
Seems like you are basically using ngZone to make sure Angular knows you changed things so it will reload that part of the DOM to reflect the changes. I don't feel like it's a hack, because you are just making sure that it works as intended.
Angular 2 has some optimization features that help make your app run smoother and one of those is avoiding DOM updates whenever and wherever possible. By using zones (or ngZones) you are basically telling Angular "pay attention to this part, it changes and I need that change to be reflected in the DOM".
I've run into that sort of problem before myself and using zones is usually your best bet. Got into situations where a part of the interface would be stuck unless you touched a button or somesuch.
Another workaround (at least for range sliders) is using the AppllicationRef tick() method, which forces a DOM update. More info about it here.

Ionic 2 Maximum call stack size exceeded Error

Trying to figure out this problem. I am getting a maxmimum call stack size error and the link below is the js output.
I have added print statements and worked out the main app file is calling page1 as it should but then page1 is calling the main app file and this continues.
I am new to ionic 2 and would really appreciate a solution, thanks.
Javascript Output
page1.ts
import { Component } from '#angular/core';
import { Data } from '../../providers/data';
import { NewListPage } from '../new-list/new-list';
import { NavController } from 'ionic-angular';
#Component({
selector: 'page-page1',
templateUrl: 'page1.html',
})
export class Page1 {
public list: any[] = [];
constructor(public navCtrl: NavController, private _data: Data) {
console.log('Page1BEFORE');
let that = this;
this._data.list.subscribe((data) => {that.list.push(data);}, (err) => {console.error(err);});
}
newList() {
console.log('NEWLIST1');
this.navCtrl.push(NewListPage);
}
}
page1.html
<ion-app>
<ion-header>
<ion-navbar>
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title>Page One</ion-title>
<ion-buttons end>
<ion-icon ios="ios-contact" md="md-contact"></ion-icon>
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content class="grid-basic-page">
<ion-col width-100><progress class="progressBar" max="100" value="80"></progress></ion-col>
<ion-row>
<ion-col width-50><div>col</div></ion-col>
<ion-col width-50><div>col</div></ion-col>
</ion-row>
<ion-row>
<ion-col width-50><div>col</div></ion-col>
<ion-col width-50><div>col</div></ion-col>
</ion-row>
<ion-row>
<ion-col width-50><div>col</div></ion-col>
<ion-col width-50><div>col</div></ion-col>
</ion-row>
<ion-list *ngIf="list">
<ion-item *ngFor="let item of list">
<ion-label>{{item.title}}</ion-label>
</ion-item>
</ion-list>
<p *ngIf="!list"> No Lists </p>
<button fab fab-bottom fab-right (click)="newList()"> New </button>
</ion-content>
</ion-app>
app.component.ts
import { Component, ViewChild } from '#angular/core';
import { Nav, Platform } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';
import { Page1 } from '../pages/page1/page1';
import { Page2 } from '../pages/page2/page2';
import { Data } from '../providers/data';
#Component({
templateUrl: 'app.html',
providers: [Data],
})
export class MyApp {
#ViewChild(Nav) nav: Nav;
rootPage: any = Page1;
pages: Array<{title: string, component: any}>;
constructor(public platform: Platform) {
console.log('PreAPP');
this.initializeApp();
console.log('PostApp');
// used for an example of ngFor and navigation
this.pages = [
{ title: 'Page One', component: Page1 },
{ title: 'Page Two', component: Page2 }
];
console.log('pages');
}
initializeApp() {
console.log('APP');
this.platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
Splashscreen.hide();
});
}
openPage(page) {
console.log('OpenPAGE');
// Reset the content nav to have just this page
// we wouldn't want the back button to show in this scenario
this.nav.setRoot(page.component);
}
}
app.html
<ion-menu [content]="content">
<ion-header>
<ion-toolbar>
<ion-title>Menu</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-list>
<button menuClose ion-item *ngFor="let p of pages" (click)="openPage(p)">
{{ p.title }}
</button>
</ion-list>
</ion-content>
</ion-menu>
<!-- Disable swipe-to-go-back because it's poor UX to combine STGB with side menus -->
<ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>
Removing the <ion-app> element from page1.html fixed the issue
In my case I had not declared and added routes constant in imports array of the module. Once declared and imported error gone.
I am using IONIC 4