4

I'm using angularjs and bootstrap-ui to build some tab.

I want two tabs:

Grades and Entrance exam. My JSON looks like this (I can change this if I need to):

[
      {
        "name":"University of Aberdeen",
        "sections": [
          {
            "title": "Grades",
            "data" : [
              {
                "heading": "GCE - AS Levels",
                "paragraph": "Do not currently form part of academic requirement. AS module resits are permitted providing the final three A levels are undertaken simultaneously over two years of study. GCSE English and Maths needed at grade C minimum."
              },
              {
                "heading": "Re-application",
                "paragraph": "Reapplication is accepted with no disadvantage to the applicant."
              }
            ]
          },
           {
            "title": "Entrance exam",
            "data" : [
              {
                "heading": "GCE - AS Levels",
                "paragraph": "Do not currently form part of academic requirement. AS module resits are permitted providing the final three A levels are undertaken simultaneously over two years of study. GCSE English and Maths needed at grade C minimum."
              },
              {
                "heading": "Re-application",
                "paragraph": "Reapplication is accepted with no disadvantage to the applicant."
              }
            ]
          }
        ]
      }

]

What I'm trying to do is use ng-repeat and in the Grades tab you only see the first set of data and in the second tab you only see the second data array.

<tabset type="pills">
 <tab ng-repeat="tab in tabs" heading="{{tab.title}}" select="getContent()" active="tab.active" disabled="tab.disabled">
      <h1>{{tab.title}}</h1>
        <div ng-repeat="item in tabs.content">
          <div ng-repeat="item in item.sections">
            <div ng-repeat="item in item.data">
              <h3>{{item.heading}}</h3>
              <p>{{item.paragraph}}</p>
            </div>
          </div>
        </div>
      </tab>
    </tabset>

Currently both sets of data are being pulled into both tabs. Any advice will be appreciated. Thanks in advance.

0

2 Answers 2

3

Alright, after looking at your Plunkr I understand your problem. Try the following:

  <tab ng-repeat="tab in tabs" heading="{{tab.title}}" select="getContent()" active="tab.active" disabled="tab.disabled">
    <div ng-hide="!tabs.isLoaded">
      <h1>{{tab.title}}</h1>
      <div ng-repeat="item in tabs.content">
        <p>{{item.fruit[$parent.$index]}}</p>
      </div>
    </div>
    <div ng-hide="tabs.isLoaded"><h3>Loading...</h3></div>
  </tab>

This solution will display the key-value pair at index = $parent.$index. This will look like: {"Apple":"Apple content goes here"} for the apple tab and {"Pear":"Pear content goes here"} for the pear tab. In order to narrow it down to the value only, you'll have to say item.fruit[$parent.$index]["Apple"] for the apple tab to display Apple content goes here, and item.fruit[$parent.$index]["Pear"] for the tab pair. That's not going to help us though, so I recommend refactoring.

Maybe something more abstract? Maybe something like...

[
 {
   "fruits":
      [
        {
          "name": "Apple",
          "content" : "content goes here"
        },
        {
          "name": "Pear",
          "content": "pear content goes here"
        }
      ]
 }
]

An object that has a property called "fruits" that is an array of fruit objects. Each fruit object has name and content as keys. This way you can always use something like item.fruit[$parent.$index]["Name"] in an ng-repeat and it'll always output the corresponding name.

Let me know if you have any questions.

Sign up to request clarification or add additional context in comments.

Comments

1

Try the following:

<tabset type="pills">
  <tab ng-repeat="section in sections"
       heading="{{section.title}}"
       select="getContent()"
       active="tab.active" 
       disabled="tab.disabled">
    <div ng-repeat="class in section.data">
      <p>{{class.heading}}</p>
      <p>{{class.paragraph}}</p>
    </div>
  </tab>
</tabset>

Does this give you the result you're looking for?

3 Comments

Thanks Patrick but unfortunately not. I know it would be easier to identify if I set up a fiddle, will try do that when I get a chance.
Alright I'll try to keep an eye on this page and wait for the fiddle.
Hi Patrick, here is a Plunker to simplify this: plnkr.co/edit/iIJVuIxspDRedN7ZXiTK?p=preview

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.