Dynamic linking of tab pages in Ionic 2 using the root function

One interesting feature in Ionic 2 is the ability to dynamically create tabs using JSON responses. By linking these tabs with their corresponding pages dynamically, we can enhance user experience. Here's a code snippet that demonstrates this:

<ion-tab *ngFor="let rew of people" [tabTitle]="rew.name"></ion-tab>

Let's take a look at how this is achieved in the 'tabs.ts' file:

export class TabsPage {

public people: any;
public result: String;
public pep: any;
public tabs : any;

 tab1Root = HomePage;
 constructor(public navCtrl: NavController, public peopleSearch: PeopleSearch) {
 this.loadPeople();   
 }

 loadPeople() {

this.peopleSearch.load()
    .then(data1 => { 
      this.people = data1.children[0].children

      var result =JSON.stringify(this.people);
                console.log("Tabs : ",result);

     }
    );
  }
}

The JSON response obtained for creating tabs dynamically looks like this:

  Tabs :  [{"category_id":5,"parent_id":2,"name":"Masala Mix","is_active":1,"position":1,"level":2,"children":[]},{"category_id":3,"parent_id":2,"name":"Pure spices","is_active":1,"position":2,"level":2,"children":[]},{"category_id":6,"parent_id":2,"name":"Pickles","is_active":1,"position":3,"level":2,"children":[]},{"category_id":8,"parent_id":2,"name":"Rice Mix","is_active":1,"position":4,"level":2,"children":[]},{"category_id":4,"parent_id":2,"name":"Asafoetida","is_active":1,"position":5,"level":2,"children":[]},{"category_id":9,"parent_id":2,"name":"Pappad","is_active":1,"position":6,"level":2,"children":[]},{"category_id":11,"parent_id":2,"name":"Become A Stockist ","is_active":1,"position":7,"level":2,"children":[]}]

Answer №1

To achieve this, simply set the index for each page and then use 'selectedIndex' in ion-tabs to select the desired tab:

<ion-tabs selectedIndex={{index}}>
   <ion-tab [root]="tab1Root" tabTitle="PageOne"></ion-tab> // Page 0
   <ion-tab [root]="tab2Root" tabTitle="PageTwo"></ion-tab> // Page 1
   <ion-tab [root]="tab3Root" tabTitle="PageThree"></ion-tab> // Page 2
</ion-tabs>

Hopefully that explanation clears things up!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can Angular 4 manage an object containing other objects most effectively?

Who can guide me on the best way to handle a data structure like this: { "1":{ "id":"1", "name":"Facebook", "created_at":"", "updated_at":"", "fields":{ "1":{ "id":"1" ...

The helper method `current_user` is not compatible with devise and cannot be used

I'm in the process of developing a basic todo application using nextjs and rails. User authentication is being handled with devise, but I'm encountering issues with accessing current_user. To address this, I am utilizing JSON API Serializer to de ...

Standard layout for a project with equally important server and client components

We are in the process of developing an open-source library that will consist of a server-side component written in C# for Web API, meta-data extraction, DB operations, etc., and a client-side component written in TypeScript for UI development. Typically, ...

Node.js built-ins require shims, while global variable names are absent

After updating my project using npm-check-updates, I encountered a strange error that stumped me. Despite following the suggested terminal command to install polyfill-node, the issue persisted with no resolution in sight online. The error displayed on the ...

How to use Typescript to catch the open event of a Bootstrap 4 modal

My goal is to trigger some code when the bootstrap model opens, but everything I have attempted so far has been unsuccessful. Modal: <div class="modal fade" id="mobileModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidde ...

What is the best way to eliminate or substitute Unicode Characters in Node.js 16?

Currently, I have a file that is being read into a JSON Object. { "city": "Delicias", "address": "FRANCISCO DOMÍN\u0002GUEZ 9" } We are using this address to send it to the Google Maps API in order to ...

The ngModel directive in Angular/Typescript is unable to bind to an object

I am encountering some issues with linking my data to ngModel. Check out the data and code example below: data = { "id": 0, "name": "", "subitem": { "subName": "", } }; When I use: [(ngModel)]="data.name" it works fine ...

Parsing JSON data retrieved from an aspx file

I recently created an aspx file to act as a JSON result. Response.Clear() Response.ContentType = "application/json; charset=utf-8" On another page from a different domain, I attempted to read the JSON data. However, upon calling the JSON value, I encount ...

Error in Postman occurs when the request is invalid, even if there are no syntax errors

I have been facing an issue while trying to send a post request to my server using Postman. Despite having no errors in the JSON body syntax, I keep receiving the 400 Bad Request Error stating "The request cannot be fulfilled due to bad syntax." This error ...

Managing JSON communications in Scala Play Framework and forwarding them to AngularJS JavaScript

Working on the implementation of a library system using Play Framework and AngularJS. To search for a book in the library, the user enters a keyword value in the input field. This value is received by the controller from a GET request. The task is to searc ...

Convert all keys from underscore to camelCase for JSON objects using Circe

Source { "fName" : "foo", "lName" : "bar", "father" : { "fName" : "baz", "lName" : "bazz", } } Desired Output { "fName" : "foo", "lName" : "bar", "father" : { "fName" : "baz", "lName" : "bazz", } ...

Extract data from JSON using jQuery

Is there a way to extract data from JSON files using AJAX? I am currently fetching JSON data with AJAX and then converting the retrieved data into HTML. The values in the JSON output are not constant, so hardcoding is not an option. Question: How do I pr ...

Issue in Typescript: The type 'RegExpMatchArray' cannot be assigned to a parameter of type 'string'

Here is the code snippet I am working with: import { persistState } from 'redux-devtools'; const enhancer = compose( applyMiddleware(thunk, router, logger), DevTools.instrument(), persistState( window.location.href.match(/[?&]debu ...

Search through Facebook Graph API to retrieve JSON objects

I need a link similar to this in order to fetch data from the Facebook graph API search. After attempting to use this link, I encountered an error: { "error": { "message": "(#200) Must have a valid access_token to access this endpoint", "t ...

What is the best way to convert one array of types to another array of types in Typescript?

Imagine you have the following: type AwesomeTuple = [string, number, boolean] Now, you're looking to transform that type using a generic approach: type AmazingGeneric<T extends any[]> = ... In this scenario: AmazingGeneric<AwesomeType> w ...

Access licensedContent property on YouTube API using PHP

Trying to improve the consistency of music search results on the Youtube API, I decided to categorize content into licensed and non-licensed. Here is the Json response from the API call: { "kind": "youtube#videoListResponse", "etag": "\"XI7nbFXulY ...

The reactivity of arrays in Vue components' props is limited

I've got an array with component data that I'm attempting to render using v-for <div :style="style" class="editor-component" v-for="(component, index) in components"> <Component :is="component.name" v-bind="component.o ...

Guidance on dividing children in an object into distinct arrays

So I have an interesting challenge while working on my project. I need help figuring out how to split a Javascript Object like the one below: { field1: { field2: { field3: "value 1", field4: "value 2" ...

Combining Angular 2 expressions with jQuery功能

My code snippet is currently functional: <div class="circle" data-value="0.8"%></div> However, I encounter an issue when attempting to bind the attribute: [data-value]="result/100" An error message is displayed: Can't bind to 'da ...

Sending image bytes through a query string can be achieved by encoding the image data

As a beginner in developing Rest services using web API in ASP.NET, I attempted to send an image byte within the query string but encountered difficulties. Unfortunately, I was unable to successfully send the image byte within the query string. While I ha ...