Display a sublist when a list item is clicked

I am receiving a JSON object in my view that looks like this:

$scope.mockData = [
  {
    "folder": "folder1",
    "reports": [{ "name": "report1" }, { "name": "report2" }, { "name": "report3" }]
  },
  {
    "folder": "folder2",
    "reports": [{ "name": "report5" }, { "name": "report6" }, { "name": "report7" }]
  },
  {
    "folder": "folder3",
    "reports": [{ "name": "report8" }, { "name": "report9" }, { "name": "report10" }]
  }
];

In order to loop through the folder list, I have included the following code snippet in my view:

<div class="row" ng-repeat="item in mockData | filter:query">
  <div class="col-md-6">
    <a href="" class="list-group-item" ng-cloak >
      <i class="glyphicon glyphicon-folder-close"></i>
      &nbsp;{{item.folder}}
    </a>
  </div>
</div>

Now, I want to add a functionality where clicking on the folder name opens up a new div displaying the items in that folder. The structure of the new div should be similar to:

"reports": [{ "name": "report1" }, { "name": "report2" }, { "name": "report3" }

How can I achieve this?

Answer №1

If you want to enhance your MOCK JSON, consider adding a boolean parameter like 'showFolderContents':false. Implement an ng-click action on your folder icon to toggle this parameter between true and false. Then, in your ng-repeat='item in mockData', create a nested ng-repeat='report in item.reports' with an ng-show='item.showFolderContents'.

To handle the ng-click functionality, use something similar to this:

<i class="glyphicon glyphicon-folder-close" ng-click='toggleShowingFolderContents(item)'></i>

In Angular, define a function like:

$scope.toggleShowingFolderContents = function(item){
    item.showFolderContents = !item.showFolderContents
}

As a result, your HTML structure will look like this:

<div class="row" ng-repeat="item in mockData | filter:query">
<div class="col-md-6">
<a href="" class="list-group-item" ng-cloak >
  <i class="glyphicon glyphicon-folder-close" ng-click='toggleShowingFolderContents(item)'></i>
<div ng-show='item.showFolderContents'>
<div ng-repeat='report in item.reports'>
{{report.name}}
</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

Which files should be included to define the variable $? in jQuery/JavaScript?

I am looking to create a sliding Div over another div in a simple way. The example provided in this Fiddle is exactly what I have in mind, but as someone new to coding, I encountered the warning Uncaught ReferenceError: $ is not defined after transferring ...

JavaScript tool package with non-JavaScript alternative

Currently in search of a reliable Javascript widget toolkit that boasts a modern UI design like Dojo, incorporates AJAX for navigation, and effects. Essential requirement is flexibility and seamless fallback to an HTML-only version for console users. Whi ...

Utilizing Filepond to extract base64 data in a React JS environment

Struggling to extract a base64 from an image upload and send it along with other values to the server. Due to API limitations allowing only 2 files at a time, I'm unable to process uploads individually. Having difficulty in mapping the 'data&apo ...

Why am I receiving an error message stating "undefined is not a function" when trying

I am attempting to create a popover using the Angular UI pop over feature. I have loaded my HTML and compiled it, but when I run my program and click on the star icon (where I display the popover), I receive an error stating that 'undefined is not a f ...

Attempting to concatenate a missing closing curly brace to an invalid JSON entity

There is a json file with a line missing a closing bracket ('}') at the end. Example input: {"title_text": "Malformed JSON", "createdAt": "2020-10-17T02:56:51+0700", "text": "Some post conte ...

Encountering difficulty when trying to initiate a new project using the Nest CLI

Currently, I am using a tutorial from here to assist me in creating a Nest project. To start off, I have successfully installed the Nest CLI by executing this command: npm i -g @nestjs/cli https://i.stack.imgur.com/3aVd1.png To confirm the installation, ...

Unstyled Cards Failing to Receive Design

I am currently working on creating a prototype that utilizes two Bootstrap 4 cards to display information from a form and store related information from another form in the second card. The current layout of this setup can be observed below: This JSFiddle ...

Is there a way to dynamically adjust height from 0 to 'auto' using @react-spring useTransition?

When utilizing the useTransition hook to incorporate animation into a list element, I encountered an issue where the height of its child elements is not fixed. If I specify {from:{height:0},enter:{height:'auto'}, the animation effect is lost. Is ...

Search for elements once they have been dynamically loaded using AJAX with the

I have a function called getItemID which searches for all the IDs under a specific parent ID (#search-output). This function works well when the ID (#test) is already loaded when the page loads. However, I am dynamically generating these IDs (#test) using ...

What could be the reason for Angular showing the raw HTML code instead of

I'm currently diving into Angular and encountering an issue where my HTML isn't properly displaying the values I've set. It appears to only show the raw HTML. Here's a snippet from my HTML page: <p>to-do-items works!</p> < ...

Achieve the highest character count possible within HTML elements

Is it possible for a standard HTML element with defined width and height to manage characters using JavaScript, as shown in the code snippet below? <div id="text-habdler-with-width-and-height" ></div> <script> $("element& ...

Utilizing the power of Datatables through AJAX calls with HTML Forms

Hello, I am currently working on incorporating djangorestframework-datatables with the datatables' JQuery plugin. My task involves loading a large table (consisting of approximately 15000 entries, paginated) with the serverSide option enabled. Enabli ...

Issue with Material UI React JS Select component: Unable to deselect multiple values when more than one item is selected

Implementing a multiselect dropdown in material ui with specific conditions: The dropdown will contain [0 Apples, 1 Oranges, 2 Grapes]. By default, 0 Apples should be selected. None of the options can be unselected. If 0 Apples is selected and the user se ...

Shopping Directive Coverage Discrepancy

In my application, there is a shop item directive that includes a price attribute. When a user interacts with the directive, the total cost (a variable stored in the main controller) should be updated by the price of the item that was clicked. Unfortunate ...

Exploring the capabilities of Google Apps Script for looping and generating output

I have two spreadsheets named rawData and processedData. The content of rawData is as follows: Status Title Options Live Title1 Option1, Option2, Option3, Option4 Live Title2 Option1, Option2, Option3, Option4, Option5 Live Title3 Option1, O ...

AngularJS is a powerful tool for implementing URL restrictions in web applications

After logging in, if I copy the URL of the welcome page and paste it after logging out, it correctly redirects me to the login page as expected because I check for the user's name in local storage. However, the issue now is that I cannot access the re ...

Automatically initiate a click event when the page is loaded

I'm having trouble getting a click event to trigger on my controller when the page loads. I really just want the checkboxes to be clicked automatically. <!DOCTYPE html> <html > <head> <link rel="stylesheet" type="text/css" ...

Discover the country's three-letter code with the power of HTML and Javascript!

I am in search of a way to determine the location of a user based on the country they are browsing from. I want to achieve this using HTML and Javascript. While researching on stackoverflow, I came across various suggestions to use different API's for ...

Having issues retrieving a JSON array in PHP with the json_decode function

Can someone assist me with passing and returning an array to a PHP script? I have successfully tested the json_encode portion, but I am facing issues with the json_decode on the PHP side. Javascript scid_list = []; $('.filter_on').each ...

Is it possible to overlay color on top of a div background? (i.e. modify div color without altering the 'background-color' property)

Can you apply a color "on top of" a div's background? I'm looking to change the displayed color of a div without altering the 'background-color' value. I need to keep the background-color for comparison when divs are clicked: var pick ...