AngularJS allows you to create nested tables and dynamically show or hide them using the

When I click on a row in my table, I want to display a new child table right below the clicked row. I am fetching new data from an API on row click, and I need to display it in a new table immediately after the clicked row. You can see how it should look in this snapshot: https://i.sstatic.net/LAiNd.jpg

After clicking on the first row, the new table should display data like 1, ABC_1, DEF. Below is the code for the template:

<table md-table class="md-primary md-data-table">
   <thead md-head md-order="vm.query.order">
   <tr md-row>
       <th md-column><span>S. No.</span></th>
       <th md-column><span>Project Name</span></th>
       <th md-column><span>Deadline</span></th>
   </tr>
   </thead>
   <tbody md-body>
   <tr md-row ng-click="vm.ShowDetailed($index)" ng-repeat="project in vm.projects | limitTo : vm.query.limit : (vm.query.page-1)*vm.query.limit">
      <td md-cell>{{($index+1)+(vm.query.page-1)*vm.query.limit}}</td>
      <td md-cell>{{project.fields.project_name}}</td>
      <td md-cell>{{project.fields.end_date}}</td>
      <table md-table ng-show="vm.detailedShow[$index]"> 
         <thead md-head>
            <tr md-row>
            <th md-column><span>Project Name</span></th>
            <th md-column><span>District</span></th>
            <th md-column><span>City</span></th>
            </tr>
         </thead>
         <tbody md-body>
            <tr md-row ng-repeat="site in sites">
                <td md-cell>{{site.projectName}}</td>
                <td md-cell>{{site.district}}</td>
                <td md-cell>{{site.city}}</td>
            </tr>
         </tbody md-body>
      </table>
     </tr>
   </tbody>
 </table>

And here are the functions for show/hide:

vm.detailedShow = [];
vm.ShowDetailed = function(index){
   vm.detailedShow[index] = !vm.detailedShow[index];
}

Even though the value of vm.detailedShow[$index] is toggling between true and false on click, I am still unable to display the table as intended.

Answer №1

One important thing to note is that you cannot insert a <table> in the same way as a <tr> or <td>. However, you can insert a table within a <td> tag.

Here is an example for you to try out.

<table md-table class="md-primary md-data-table">
   <thead md-head md-order="vm.query.order">
   <tr md-row>
       <th md-column><span>S. No.</span></th>
       <th md-column><span>Project Name</span></th>
       <th md-column><span>Deadline</span></th>
   </tr>
   </thead>
   <tbody md-body>
   <tr md-row ng-click="vm.ShowDetailed($index)" ng-repeat-start="project in vm.projects | limitTo : vm.query.limit : (vm.query.page-1)*vm.query.limit">
      <td md-cell>{{($index+1)+(vm.query.page-1)*vm.query.limit}}</td>
      <td md-cell>{{project.fields.project_name}}</td>
      <td md-cell>{{project.fields.end_date}}</td>
   </tr>
   <tr ng-show="vm.detailedShow[$index]" ng-repeat-end>
      <td> </td>
      <td colspan="2"><table md-table ng-show="vm.detailedShow[$index]"> 
         <thead md-head>
            <tr md-row>
            <th md-column><span>Project Name</span></th>
            <th md-column><span>District</span></th>
            <th md-column><span>City</span></th>
            </tr>
         </thead>
         <tbody md-body>
            <tr md-row ng-repeat="site in sites">
                <td md-cell>{{site.projectName}}</td>
                <td md-cell>{{site.district}}</td>
                <td md-cell>{{site.city}}</td>
            </tr>
         </tbody md-body>
      </table>
      </td>
   </tr>
   </tbody>
 </table>

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

What is the best way to display a single instance of a React component that is declared in multiple locations within the app?

Imagine I have 2 main components, A and B. Now, component C needs to be created inside both A and B. How can I guarantee that the same exact instance of C is generated in both places? Essentially, I want them to stay synchronized - any changes made to one ...

What are the best ways to improve ajax response in jQuery?

My database query is fetching around 5000 rows of data along with data from 5 relational tables, leading to performance issues. The network tab shows the size of the request resource as 9.1 mb. After that, I am dynamically adding this data to a table using ...

Using Vue.js to dynamically update values in JavaScript code or tags

I've encountered an issue that I'm having trouble articulating, but I'll do my best to explain. Upon loading the page, the following code snippet, which uses jinja-based placeholders, is executed: <div class="ui container"> ...

Organizing a series of objects into groups of four for processing

I have a task of organizing an array of objects that represent game players by assigning each player to a group number based on their current group value. The challenge is to ensure that each group has as close to four players as possible, while also acco ...

What is the best way to assign a default value in mat-select when using an ngFor loop?

Is there a different approach to setting a default value in mat-select within an *ngFor loop? I'm having trouble accessing the element in the array from the loop as desired. .ts file: persons: Person[] = .. //consist of Person with name and age .htm ...

The AngularJS templates' use of the ternary operator

Is there a way to implement a ternary operation in AngularJS templates? I am looking for a way to apply conditionals directly in HTML attributes such as classes and styles, without having to create a separate function in the controller. Any suggestions wo ...

Utilizing individual values from JSON in Knockout JS through single-use options

I am currently receiving data from another view model which I want to display in a dropdown menu. Each option from the other view model needs to be unique per line, so that once a user adds an item to the list, that specific option is no longer available. ...

Avoid adding any empty entries to the list using jQuery

I have implemented a code to prevent any blank entries from being added to my list, and it seems to be working fine. However, I can't shake the feeling that there might be a better way to achieve this. Even though it functions correctly, it doesn&apos ...

The File Filter feature of Angular's ng2-file-upload is not functioning correctly for PNG files in the Internet Explorer

I am encountering an issue with the file uploader plugin ng2-file-upload when trying to upload a PNG file using Internet Explorer 11. For some reason, the PNG files are not being added to the queue, even though all other allowed file types work perfectly f ...

React and Material UI: Ensuring Proper Whitespace Handling in Strings

Exploring the use of Typography component from Material UI (https://material-ui.com/api/typography/) The main objective is to maintain the new lines and spaces in a saved string. For instance, if the string contains leading spaces and new lines, it shoul ...

Enhance your data retrieval from XPATH using Javascript

Here is the layout of an HTML template I am working with: <div class="item"> <span class="light">Date</span> <a class="link" href="">2018</a> (4pop) </div> <div class="item"> <span class="light">From</sp ...

Unable to choose anything within a draggable modal window

Can someone please assist me? I am struggling to figure this out. I can't select text inside the modal or click on the input to type text into it. I suspect it may be due to z-index issues, but I'm having trouble locating them. var currentZ = n ...

Context failing to refresh value upon route changes

My current context setup is as follows: import { createContext, ReactNode, useState } from "react"; type props = { children: ReactNode; }; type GlobalContextType = { name: string; setName: (value: string) => void; }; export const Glob ...

Tips for implementing an onClick event within a NavBar component in a React application

Embarking on my first React project has been an exciting journey for me. I am relatively new to JavaScript, HTML, CSS, and the world of web app programming. My goal is to showcase some information upon clicking a button. In my main default component, App ...

Setting up Material UI Icons in your React js project

I've been having trouble installing the Material UI Icons package with these commands: npm install @material-ui/icons npm install @material-ui/icons --force npm i @mui/icons-material @mui/material Error messages keep popping up and I can't see ...

Picture in a clear background without any alteration

Is there a way to create a background-color with opacity inside an image without affecting the image itself? The issue lies in the fact that the transparent background-color makes everything underneath it, including text and the image, transparent. Below ...

The angular view is lacking a CSS class

index.html: <div ng-view="myview.html"></div> myview.html: <table class="table table-striped table-hover"> <tr> <td>Name</td> <td>Product</td> </tr> <tr ng-repeat="deal in deals" class="clickableR ...

What is the best way to execute NPM commands within the terminal of Visual Studio Code?

I recently installed npm in VSCode from extensions, but I am having trouble activating it. When I try to run the command 'npm init' in the terminal, I receive the following error message: "The term 'npm' is not recognized as the name of ...

Tips for customizing table cell styling in editable cells with React material table

My code utilizes a material table with editable cells. However, I am encountering a strange style issue when I edit a cell in the table. Please refer to the image below. Can anyone suggest a solution to fix this problem? https://i.sstatic.net/Miiov.png ...

Tips on viewing class object values within the `useEffect` hook

App.js import React, { useRef, useEffect } from "react"; import Token from "./Token"; export default function App() { const tokenRef = useRef(new Token()); useEffect(() => { console.log("current index of token: ", ...