Skip to content

Instantly share code, notes, and snippets.

@tithi021
Last active October 24, 2019 08:24
Show Gist options
  • Save tithi021/3cf11532a30f2954b5831e2366ce2ea9 to your computer and use it in GitHub Desktop.
Save tithi021/3cf11532a30f2954b5831e2366ce2ea9 to your computer and use it in GitHub Desktop.
Ionic 3 Accordion Sidemenu
import { Component, ViewChild } from '@angular/core';
import { Nav, Platform, MenuController } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { MenuProvider } from '../providers/menu/menu';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
@ViewChild(Nav) nav: Nav;
rootPage: any = 'HomePage';
pages: any;
// Selected Side Menu
selectedMenu: any;
constructor(public platform: Platform,
public statusBar: StatusBar,
public splashScreen: SplashScreen,
public menuProvider: MenuProvider,
public menuCtrl: MenuController) {
this.initializeApp();
}
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.
this.getSideMenuData();
this.statusBar.styleDefault();
this.splashScreen.hide();
});
}
getSideMenuData() {
this.pages = this.menuProvider.getSideMenus();
}
openPage(page, index) {
// Reset the content nav to have just this page
// we wouldn't want the back button to show in this scenario
if (page.component) {
this.nav.setRoot(page.component);
this.menuCtrl.close();
} else {
if (this.selectedMenu) {
this.selectedMenu = 0;
} else {
this.selectedMenu = index;
}
}
}
}
<ion-menu [content]="content">
<ion-header>
<ion-toolbar>
<ion-title>Menu</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-list>
<ion-item ion-item *ngFor="let p of pages; let i=index" (click)="openPage(p, i);">
<ion-row>
<!-- Parent Pages -->
<ion-col col-9 class="menu-name">
<span ion-text>
{{p.title}}
<ion-icon [name]="selectedMenu == i? 'arrow-down' : 'arrow-forward'" *ngIf="p.subPages" float-right></ion-icon>
</span>
<!-- Child Pages -->
<ion-list no-lines [hidden]="selectedMenu != i">
<ion-item no-border *ngFor="let subPage of p.subPages;let i2=index" text-wrap (click)="openPage(subPage)">
<ion-row>
<ion-col col-10 class="menu-name"><span ion-text color="color2">&nbsp;{{subPage.title}}</span></ion-col>
</ion-row>
</ion-item>
</ion-list>
</ion-col>
</ion-row>
</ion-item>
</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>
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable()
export class MenuProvider {
constructor(public http: HttpClient) { }
getSideMenus() {
return [{
title: 'Home', component: 'HomePage'
},
{
title: 'Topics',
subPages: [{
title: 'Topic1',
component: 'Topic1Page',
}, {
title: 'Topic1',
component: 'Topic2Page',
}]
}];
}
}
@shwetashambhargade
Copy link

Thank u so much it is working absolutely.....

@govindaraj7845
Copy link

Thank You

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment