I'm having problems with clicking the button upon taking a test.
Scenario:
Upon Logging in -> the user takes a test and submits -> the user is redirected on the home page. But on my home page i cant click on the "Menu button"
on my Login.ts
if (this.checker == "false" || this.checker == null) {
this.navCtrl.setRoot(LearnertestPage);
} else {
this.navCtrl.setRoot(SplashscreenPage);
}
on my test.ts
in the alert controller, I have this
{
text: 'Yes',
handler: data => {
this.learningStyles.push(
[
{style: "Auditory", value: AudioTotal},
{style: "Logical", value: LogicalTotal},
{style: "Physical", value: PhysicalTotal},
{style: "Social", value: SocialTotal},
{style: "Solitary", value: SolitaryTotal},
{style: "Visual", value: VisualTotal},
{style: "Verbal", value: VerbalTotal}]
);
this.userChecker.update( this.currentUser, { Checker: 'true' });
this.navCtrl.setRoot(SplashscreenPage);
}
}
And lastly on my Splash screen or home :
HTMl:
<ion-menu [content]="content">
<ion-content>
<ion-item style="background-color:#00aced">
<img src="./assets/img/adapt.png" height="100px" width="350px"/>
</ion-item>
<ion-list>
<button ion-item *ngFor="let p of pages" (click)="openPage(p)">
<ion-icon name="{{p.icon}}"></ion-icon> {{p.title}}
</button>
<button ion-item (click)="doConfirm()">
<ion-icon name="log-out"></ion-icon> Logout
</button>
</ion-list>
on the splashscreen.ts
#ViewChild(Nav) nav: Nav;
selectedItem: any;
rootPage: any = ListPage;
selectedTheme:String;
icons: string[];
pages: Array<{ title: string, component: any, icon: string }>
constructor(){
// used for an example of ngFor and navigation
this.pages = [
{ title: 'Home', component: SplashscreenPage, icon: this.icons[0], },
{ title: 'Earth Science', component: LessonPage, icon: this.icons[1] },
{ title: 'Progress', component: ProfilePage, icon: this.icons[2] }
];
}
openPage(page) {
// 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); }
I can't seem to click on this button. Hmm. What am I doing wrong?
try to custom toggle the menu. add the toggleMenu method to the (click) of the menu button
import { Component } from '#angular/core';
import { MenuController } from 'ionic-angular';
#Component({...})
export class MyPage {
constructor(public menuCtrl: MenuController) {
}
toggleMenu() {
this.menuCtrl.toggle();
}
}
Related
I want to translate primeNG menu bar component. But I don't know how to handle the translation inside of .ts file. I want to get translate data from the .json file.
import { TranslateService } from '#ngx-translate/core';
import { SocialAuthService, SocialUser } from 'angularx-social-login';
import { MenuItem, MessageService } from 'primeng/api';
#Component({
selector: 'app-admin',
templateUrl: './admin.component.html',
styleUrls: ['./admin.component.scss']
})
export class AdminComponent implements OnInit {
display: boolean = true;
socialUser: SocialUser = new SocialUser();
items: MenuItem[] = [];
constructor(private socialAuthService: SocialAuthService
, private toast: MessageService, private translateService: TranslateService
) {
this.items.map(item => item.label = this.translateService.instant(item.label));
}
ngOnInit(): void {
this.socialAuthService.authState.subscribe(_ => {
this.socialUser = _;
});
this.items = [
{ label: 'menu.pManagement', icon: 'pi pi-chart-line'},
{ label: 'menu.iList', icon: 'pi pi-wallet', routerLink: 'outsourcing' },
{ label: 'menu.iAnalysis, icon: 'pi pi-clock'}
];
}
addToast() {
this.toast.add({
severity: 'success',
summary: 'Success',
detail: ''
});
}
}
Upper you can see my menu bar items. I want to change the label name when I change the language from the language change dropdown.
here is the en.json file
{
"menu" : {
"pManagement" : "Project Management",
"iList" : "Item list",
"iAnalysis" : "Item analysis"
}
}
I have an issue with my Angular project. I can't get only one component of menu and put it on nav to show which menu is clicked.
This is my header.component.html
<mat-nav-list>
<mat-list-item>
<a style="color: white;" *ngFor="let menuitem of menuItems.getMenuitem()">{{ menuitem.name }}</a>
</mat-list-item>
</mat-nav-list>
This is my header.component.ts
import { MediaMatcher } from '#angular/cdk/layout';
import { Router } from '#angular/router';
import {
ChangeDetectorRef,
Component,
NgZone,
OnDestroy,
ViewChild,
HostListener,
Directive,
AfterViewInit
} from '#angular/core';
import { MenuItems } from '../../../shared/menu-items/menu-items';
#Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: []
})
export class AppHeaderComponent implements OnDestroy, AfterViewInit {
mobileQuery: MediaQueryList;
private _mobileQueryListener: () => void;
constructor(
changeDetectorRef: ChangeDetectorRef,
media: MediaMatcher,
public menuItems: MenuItems
) {
this.mobileQuery = media.matchMedia('(min-width: 768px)');
this._mobileQueryListener = () => changeDetectorRef.detectChanges();
this.mobileQuery.addListener(this._mobileQueryListener);
}
ngOnDestroy(): void {
this.mobileQuery.removeListener(this._mobileQueryListener);
}
ngAfterViewInit() {}
}
This is my menu-items.ts when i get menu names
import { Injectable } from '#angular/core';
export interface BadgeItem {
type: string;
value: string;
}
export interface Menu {
state: string;
name: string;
type: string;
icon: string;
badge?: BadgeItem[];
}
const MENUITEMS = [
{ state: 'home', type: 'link', name: 'HOME', icon: 'home' },
{ state: 'dashboard', name: 'DASHBOARD', type: 'link', icon: 'dashboard' },
{ state: 'organizzazioni', type: 'link', name: 'ORGANIZZAZIONI', icon: 'domain' },
{ state: 'registri', type: 'link', name: 'REGISTRI', icon: 'assignment' },
{
state: 'aprovazioni', type: 'link', name: 'APROVAZIONI', icon: 'view_headline', badge: [
{ type: 'red', value: '1' }]
},
{ state: 'impatto', type: 'link', name: 'IMPATTO', icon: 'tab' },
{ state: 'informative', type: 'link', name: 'INFORMATIVE', icon: 'web' },
{ state: 'violazioni', type: 'link', name: 'VIOLAZIONI', icon: 'vertical_align_center' },
];
#Injectable()
export class MenuItems {
getMenuitem(): Menu[] {
return MENUITEMS;
}
}
What am I missing?
How about adding a click event and react on it in the navigation bar:
<mat-nav-list>
<mat-list-item>
<a style="color: white;" *ngFor="let menuitem of menuItems.getMenuitem()" (click)="menuItemClick(menuitem)">{{ menuitem.name }}</a>
</mat-list-item>
</mat-nav-list>
Add click event on html file (... Is portion of your code)
<a ... (click)="handleMenuClick(menuitem)"></a>
In your header ts file add method
handleMenuClick (item) {
alert(item.name)
}
So inside that function get value, and do any desired task to change UI based on this selected item, I have just added alert to show current item
You need to define that method in your controller file
Add menuItemClick method to your menuItem class and this method should take the clicked item as a parameter then handle item click event in your html
Something like that
export class MenuItems {
getMenuitem(): Menu[] {
return MENUITEMS;
}
menuItemClick(MenuItem: Menu){
// Do something
}
}
And in your html
<mat-nav-list>
<mat-list-item>
<a style="color: white;" *ngFor="let
menuitem of menuItems.getMenuitem()" (click)="menuItems.menueItemClick(menuitem)">{{ menuitem.name }}</a>
</mat-list-item>
</mat-nav-list>
Please open it in full browser https://stackblitz.com/edit/angular-skzgno?file=src%2Fapp%2Fapp.component.html , Then click button/image of any li tag,The button will be change to different image as like active .Even if you refresh also this active will not be change since we are adding active=true into localstorage.Now the problem is,on page load when you click button of any li,except that button,buttons of other li should be disable and when we refresh also nothing will be change until you clear localstorage.Please find the code below
app.component.html
<hello name="{{ name }}"></hello>
<p>
Start editing to see some magic happen :)
</p>
<div>
<pre>
</pre>
<ul>
<li *ngFor="let item of statusdata">
<span>{{item.id}}</span>
<span>{{item.name}}</span>
<button style="margin-left:10px" (click)="toggleActive(item, !item.active)">
<img style="width:50px;margin-left:10px" *ngIf="!item?.active || item?.active === false" src ="https://dummyimage.com/qvga" />
<img style="width:50px;margin-left:10px" style="width:50px;margin-left:10px" *ngIf="item?.active === true" src ="https://dummyimage.com/300.png/09f/fff" />
</button>
</li>
</ul>
</div>
app.component.ts
import { Component } from '#angular/core';
#Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
statusdata: any;
ngOnInit() {
this.statusdata = [
{ id: 1, name: "Angular 2" },
{ id: 2, name: "Angular 4" },
{ id: 3, name: "Angular 5" },
{ id: 4, name: "Angular 6" },
{ id: 5, name: "Angular 7" }
];
this.statusdata.forEach(item => {
this.getCacheItemStatus(item);
});
}
toggleActive(item, activeStatus = true) {
if(!this.statusdata.some(d => d.active)){
item.active = activeStatus;
localStorage.setItem(`item:${item.id}`, JSON.stringify(item));
}
}
getCacheItemStatus(item) {
const cachedItem = localStorage.getItem(`item:${item.id}`);
if (cachedItem) {
const parse = JSON.parse(cachedItem); // Parse cached version
item.active = parse.active; // If the cached storage item is active
}
}
}
I'm so so sorry for not being able to adapt this to the code in the question. I faced this challenge and did not want to forget sharing.
say we have this in the typescript file;
movies: any[] = [
{ name: 'Wakanda', year: 2010, rating: 4.5 },
{ name: 'Super', year: 2000, rating: 5 },
{ name: 'Deli', year: 2002, rating: 3 },
{ name: 'Fury', year: 2020, rating: 4 },
];
isDisabled: boolean = false;
Then this in the HTML...
<div *ngFor="let movie of movies;index as i">
<div class="wrapper">
<button (click)="disableBtn('btn' + i)" [disabled]="isDisabled && isDisabled !== 'btn'+i"
id=btn{{i}}>Vote</button>
</div>
</div>
The disableBtn function takes the current button and assigns it to the isDisabled variable then [disabled] attribute checks if isDisabled is truthy and if isDisabled is strictly not equal to the current button. This will be true for all other buttons except the one clicked.
The function disables(toggles it) all other buttons except the one clicked.
disableBtn(btn) {
if (this.isDisabled === btn) { //also re-enables all the buttons
this.isDisabled = null;
return;
}
this.isDisabled = btn;
}
Have you tried the [disabled] property?
<button (click)="toggleActive(item, !item.active)" [disabled]="shouldDisable(item)">
shouldDisable(item): boolean {
return !item.active && this.statusdata.some((status) => status.active);
}
I'm using ionic 4 and I want to set custom buttons on ion-select through interfaceOptions
HTML
<ion-item>
<ion-label>Lines</ion-label>
<ion-select multiple="true" [(ngModel)]="SelectedLines" [interfaceOptions]="customAlertOptions">
<ion-select-option [value]="line" *ngFor="let line of Lines">{{linea.Name}}</ion-select-option>
</ion-select>
</ion-item>
TS
customAlertOptions: any = {
buttons: [
{
text: 'Select All',
handler: (blah) => {
console.log('Select All Clicked');
},
{
text: 'No',
handler: (blah) => {
console.log('Confirm Cancel: blah');
}
}, {
text: 'Okay',
handler: () => {
console.log('Confirm Okay');
}
}
]
};
However, only the default buttons are showing (Ok and Cancel)
Docs say it should be possible
https://ionicframework.com/docs/api/select
I can see this has been reported for previous versions of Ionic
https://forum.ionicframework.com/t/custom-button-for-ion-select-through-selectoptions-not-working/157305
Is it possible to make this work on Ionic 4? Is there a workaround?
EDIT: I tried with the PopOver interface with the same results
What you are trying to do isn't possible from what I can see.
The documentation actually only says you can set the buttons text:
ion-select#select-buttons - Ionic Documentation
By default, the alert has two buttons: Cancel and OK. Each button's text can be customized using the cancelText and okText properties.
It doesn't say that the buttons can be customised.
You can pass in the interfaceOptions but its overridden later by the default button set:
https://github.com/ionic-team/ionic/blob/master/core/src/components/select/select.tsx#L339
The code looks like this:
const alertOpts: AlertOptions = {
mode,
...interfaceOptions,
header: interfaceOptions.header ? interfaceOptions.header : labelText,
inputs: this.createAlertInputs(this.childOpts, inputType),
buttons: [
{
text: this.cancelText,
role: 'cancel',
handler: () => {
this.ionCancel.emit();
}
},
{
text: this.okText,
handler: (selectedValues: any) => {
this.value = selectedValues;
}
}
],
cssClass: ['select-alert', interfaceOptions.cssClass,
(this.multiple ? 'multiple-select-alert' : 'single-select-alert')]
};
return alertController.create(alertOpts);
So as you can see the ...interfaceOptions, is passed in at the start, then the buttons are set to the defaults, with the only customisation options being the ok or cancel text.
i am working with AlertController from ionic and i can custom it, just take a look at my screen .
You just need to import AlertController and after you can do something like this for exemple :
home.page.ts
async addAlertHome(adresse: string, lat: string, lon: string) {
const alert = await this.alertController.create({
header: 'Ajouter aux favoris',
message: 'Êtes vous sûr de vouloir ajouter cette adresse à vos favoris ?',
buttons: [
{
text: 'Non',
role: 'cancel',
cssClass: 'secondary'
}, {
text: 'Oui',
handler: () => {
alert.dismiss().then(() => {
this.addFavoriteHome(adresse, lat, lon);
});
console.log('Confirm Okay');
}
}
]
});
await alert.present();
}
And use it where you want on html :
home.page.html
<ion-icon name="heart-empty" (click)="addAlert(location.display_name, location.lat, location.lon)" end>
</ion-icon>
And don't forget on your constructor :
public alertController: AlertController
I start my project with tabs template and then I add side menu into it. Both tabs and side menu work, but if i click on menu item, page lost tabs view.
app.components.ts
#Component({
templateUrl: 'app.html'
})
export class MyApp {
#ViewChild(Nav) nav: Nav;
rootPage = TabsPage;
pages: Array<{title: string, component: any}>;
constructor(public platform: Platform) {
this.initializeApp();
// used for an example of ngFor and navigation
this.pages = [
{ title: 'Home', component: HomePage },
{ title: 'About', component: AboutPage },
{ title: 'Contact', component: ContactPage }
];
}
initializeApp() {
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) {
// 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" push persistent=true>
<ion-header>
<ion-toolbar>
<ion-title>Menu</ion-title>
</ion-toolbar>
</ion-header>
<ion-content class="outer-content">
<ion-list>
<button menuClose ion-item *ngFor="let p of pages" (click)="openPage(p)">
{{p.title}}
</button>
</ion-list>
</ion-content>
</ion-menu>
<ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>
The issue is you are setting the component selected from the menu as root.
this.nav.setRoot(page.component);
This replaces the existing TabsPage with the selected page as root rather than change the selected tab of the TabsPage.
A solution is to send the page object from openPage() function to TabsPage using Events API or a service with Observable/Subject.
openPage(page) {
this.events.publish('menu selected',page);
}
In your TabsPage,
ngOnInit(){
this.events.subscribe('menu selected',this.openPage.bind(this))
}
openPage(page){
this.tabs.select(page.index);//set an additional property with index same as tab order.
}