Display the div according to the $index selected from the AngularJS list

Below is the structure of my HTML code:

 <div class="col-md-4">
    <ul class="list-group text-left">
        <a href="#" class="list-group-item" ng-click="showData($index)" ng-repeat="field in Data">
         {{ field.name }}</a>
    </ul>
 </div>
<div class="col-md-8">
    <div ng-repeat="field in Data" >
        <div class="panel panel-primary" ng-repeat="scenario in field.scenarios track by $index">
            <div class="panel-heading ">
                {{ scenario.name }}
            </div>
            <ul class="list-group p-body">
                <li class="list-group-item" ng-repeat="values in scenarios.values track by $index">{{ value }}</li>
            </ul>
        </div>
</div>

I am trying to display data from the col-md-8 div based on the index selected from the list group with field.name in the col-md-4.

Here is an example of some Sample Data:

{
    "scenarios": [{
        "values": ["value 1",
        "value 2",
        "value 3"],
        "title": "some title"
    },
    {
        "values": ["value 1",
        "value 2",
        "value 3"],
        "title": "some title"
    },
    {
        "values": ["value 1",
        "value 2",
        "value 3"],
        "title": "some title"
    }],
    "description": "",
    "name": "Some name  "
}

The goal is to have the name displayed on the left side (in col-md-4) and then show the corresponding data on the right side (in col-md-8). I'm currently stuck and would appreciate any guidance!

Answer №1

Create a variable within the scope to hold the selected index, and depending on the selected index, display another div.

<div class="col-md-4">
  <ul class="list-group text-left">
    <a href="#" class="list-group-item" ng-click="selectedIndex = $index" ng-repeat="field in Data">
         {{ field.name }}</a>
  </ul>
</div>
<div class="col-md-8">
  <div ng-show="selectedIndex == $index" ng-repeat="field in Data">
    <div class="panel panel-primary" ng-repeat="scenario in field.scenarios track by $index">
      <div class="panel-heading ">
        {{ scenario.name }}
      </div>
      <ul class="list-group p-body">
        <li class="list-group-item" ng-repeat="values in scenarios.values track by $index">{{ value }}</li>
      </ul>
    </div>
  </div>

Answer №2

If you plan on showcasing just one field object, the most efficient way to do so would be:

<div class="col-md-4">
    <ul class="list-group text-left">
        <a href="#" class="list-group-item" 
           ng-click="selectedField = field" 
           ng-repeat="field in Data">
         {{ field.name }}</a>
    </ul>
 </div>
<div class="col-md-8">
    <!-- display only if one is selected -->
    <div ng-if="selectedField">
        <div class="panel panel-primary" 
             <!-- extract data from 'selectedField' -->
             ng-repeat="scenario in selectedField.scenarios track by $index">
            <!-- additional content -->
        </div>
    </div>
</div>

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

Display the length of an array using AngularJS

Hey there, I'm currently having an issue with printing a list where each entry is supposed to follow the format "element X of TOTAL". However, instead of displaying the total value, it appears as blank text. It seems like a simple mistake, but for som ...

Learn how to execute JavaScript code in Selenium without launching a web browser

I am currently using the Selenium API for web scraping on pages that contain JavaScript code. Is there a way to retrieve the code without having to open a web browser? I am still learning how to use this API Is this even feasible? ...

When multiple instances are present, the functionality of dynamically generated jQuery functions ceases to operate effectively

I've developed a chat application similar to hangouts where clicking on a user generates the chat div. One feature I have is allowing users to press enter in a textarea to send text, but when multiple dynamically generated jQuery functions are present ...

After a span of two minutes, the Node.js and Express server terminates the connection

I am currently working with Express 4.X and Node.js 0.12. One of the routes in my application is responsible for uploading and processing files. However, I have encountered an issue where some file uploads are taking longer than the default 2-minute timeo ...

Accessing the 'window' object within an Angular expression is not permitted unless using a controller

I am attempting to display a <p> only in the context of the HTTP protocol. This is my current progress: angular .module('myApp',[]) .controller('myCtrl', ['$scope', '$window', function($scope, $window){ ...

Guidance on establishing an Array data type for a field in GraphQL Type declarations

Hey there! Currently, I'm working on my Nodejs project and facing a little dilemma. Specifically, I am trying to specify the type for graphQL in the code snippet below. However, I seem to be struggling with defining a field that should have a Javascri ...

Having trouble with the $.get function in AJAX with Rails 4? It seems

After working with Rails for a few years, I decided to dip my toes into the world of AJAX. With a Rails 4 app in hand, I'm currently testing out some functions. My end goal is to reload a partial on the edit page located at app/views/stories/edit.html ...

Having trouble transferring data from JavaScript to PHP via POST method

Hi there, this is my first time posting and I could really use some assistance. I have a bit of a roadblock with two Symfony forms on a page that don't seem to be working properly. The first form is for a contract, while the second one is used to pop ...

Is it possible to conditionally redirect using Vue router?

I am in the process of creating a straightforward Vue application where the router links will be determined by the data retrieved from the server. The format of the data looks something like this: id: 1 path: "category_image/About.jpg" slug: &quo ...

Is it feasible to dynamically insert and delete elements in React?

I'm currently in the process of rebuilding a project, specifically a drag and drop website builder, using React after it was originally built with a heavy reliance on jQuery. My main challenge lies in being able to dynamically add and remove elements ...

Notify users with a prompt when a modal or popup is closed on Google Chrome extensions

I have developed a Google Chrome extension for setting timers and receiving alerts. Currently, the alert only goes off when the extension is open, but I want it to fire even when the extension is closed. This extension currently requires the window to be ...

Is it possible to convert a blob to an image file using the FileReader in HTML

client side code <head> <script> var reader = new FileReader(); var objVal; var image = new Image(); reader.onload = function(e) { document.getElementById('propertyImg').setAttribute('src', e.target.result); }; fun ...

Nested function unable to assign values to variables

I am currently working on a function in Discord.js that collects users who reacted to a message. I have managed to gather all the user IDs inside a nested function, but when trying to return the array to the caller, it does not seem to work as expected. He ...

Having trouble with jQuery focus not functioning properly?

I've been attempting to implement a focus function on a specific input in order to display a div with the class name .search_by_name when focused. However, I'm encountering issues and would appreciate it if someone could review my code to identif ...

Tips to successfully utilize addEventListener on a submit action

Having issues getting this to work on submit, it functions properly when using document.getElementById("gets").addEventListener("click", b_button); but does not work when I try document.getElementById("gets").addEventListener ...

Navigating through React-router with multiple child elements

Creating one of my routes requires the presence of two children in order to function properly. The main application page, where I set up all the routes, might look something like this: <Route path="/logs" component={Logs}> <Route path="/logs ...

Having trouble with ion-item click not functioning in Ionic 3?

Working on my Ionic 3 project, I have encountered an issue with the ion-item click listener. The scenario is that I am adding text over a canvas and then attempting to change the style of the text (such as font-family, font-size, bold, and italic). The UI ...

handling events by invoking wrapped asynchronous functions within the Meteor framework

Dealing with the issue of making a synchronous call using Meteor, we have the following scenario: var twitPost = Meteor._wrapAsync(twit.post.bind(twit)); function process(screen_name) { twitGet('users/show', {'screen_name': screen_n ...

What is the best approach to determine the numerical equivalents of options in a dropdown menu using PHP and JS

Hey guys, I'm really stuck on this problem and I can't seem to find a helpful tutorial anywhere. I've got a form with two drop-down menus, each with a "value" attribute. What I want is for users to be able to select an item from both drop-do ...

Dynamic Rendering of Object Arrays in Table Columns using JavaScript

In the process of developing an appointment slot selection grid, I have successfully grouped all appointments by dates. However, I am facing challenges in displaying this array of Objects as a clickable grid with columns. The current output can be viewed h ...