Dynamically Populate Tabs from JSON in AngularJS

Created: 04 December 2015

In general, the data presented in the tabs is already included in the web page. In cases where we want to change the content for specific tab or load tabs in whole from a remote data source, we can use a JSON file as our data source. In following sections, we will show you how to do using the IntegralUI TabStrip directive for AngularJS.

TabStrip directive is part of IntegralUI Studio for Weba suite of UI Components for development of web apps

As above demo shows, the content of tabstrip is fully loaded from a JSON file. Tab headers show the name of the continent, while tab content shows a list of countries with their population.

Create a JSON Data Structure for Tabs

At first, we need to specify the content of our tabs, their header and content. In our example we are showing a list of continents with some countries and their population. For this purpose, the tab header will display the continent name, and as tab content we will show a list of countries with their population number. Therefore, our JSON will look like:

Tabs HTML Specification

Now, we need to specify how our data from JSON file will be displayed to the end user. For tab header we are only showing a single lined text, so we only need to specify the heading property for each tab. As for the tab content, we will create a list showing the country name and its population.

<iui-tabng-repeat="tab in tabs"name="{{tab.name}}"heading="{{tab.continent}}">

<ulclass="country-list">

<ling-repeat="country in tab.content">

<span>{{country.name}}</span>

<span>{{country.population}}</span>

</li>

</ul>

</iui-tab>

</iui-tabstrip>

.directive

{

width: 450px;

height: 250px;

}

.iui-tab-content

{

padding: 10px;

}

.country-list

{

width: 95%;

}

.country-list li

{

border-bottom: thin solid gray;

margin: 10px 0;

}

.country-list span:last-child

{

float: right;

}

Load Data from JSON File into the TabStrip

Finally, we will use $http to load the data from JSON file to the tabstrip. Because tab objects are already specified in our application controller, and there are linked with the TabStrip directive properties, we only need to populate them with some data.

Conclusion

Populating the Angular TabStrip directive dynamically with data from a remote data source, in this case a JSON file, is simple. We only need to create the data structure for the tabs, and then link this data with the TabStrip properties. When data is loaded, the tabs will appear.

Tab content switching

Newsletter

Sign-up to our newsletter and you will receive news on upcoming events, latest articles, samples and special offers.

Name:

Email: *

*By checking this box, I agree to receive a newsletter from Lidor Systems in accordance with the Privacy Policy. I understand that I can unsubscribe from these communications at any time by clicking on the unsubscribe link in all emails.