Extend the row of the table according to the drop-down menu choice

I am working on a feature where a dropdown menu controls the expansion of rows in a table. Depending on the option selected from the dropdown, different levels of items need to be displayed in the table. For example, selecting level 1 will expand the first row, level 2 will expand up to the second level (dish items), and selecting "All" will expand all rows in the table.

              <select [(ngModel)]="expandModel"
                (ngModelChange)="getoptionedExpansion($event)">
                <option value="lvl1">level 1 </option>
                <option value="lvl2">level 2</option>
                <option value="lvl3"> Level 3 </option>
                <option value="lvl4">level 4 </option>
                <option value="all">All</option>
              </select>

Table :

    <table style="width:100%">
      <thead>
      </thead>
      <tbody>
        <ng-container *ngFor="let data of users">
          <tr (click)="data.hideThis= !data.hideThis" style="border-bottom: 1px solid #DDE3ED;">
            <td class="mainTitleFont cursor-pointer" style="font-size:16px">
              <span *ngIf="data.hideThis">
                -
              </span>
              <span *ngIf="!data.hideThis">
                +
              </span>
              {{data.itemName}}</td>
          </tr>
          <ng-container *ngFor="let item of data.subItemsList">
            <tr (click)="item.hideThis= !item.hideThis" style="font-size:15px; background-color: #edf2ef"
              [hidden]="!data.hideThis">
              <td *ngIf="data.hideThis">
                <span *ngIf="item.hideThis">
                  &nbsp; &nbsp; -
                </span>
                <span *ngIf="!item.hideThis">
                  &nbsp; &nbsp; +
                </span>
                {{item.subitemName}}
              </td>
            </tr>
            <ng-container *ngFor="let prod of item.items">
              <tr (click)="prod.hideThis= !prod.hideThis" style="font-size:14px; background-color: #f2f2f2"
                [hidden]="!data.hideThis || !item.hideThis">
                <td>
                  <span *ngIf="prod.hideThis">
                    &nbsp; &nbsp; &nbsp; &nbsp;-
                  </span>
                  <span *ngIf="!prod.hideThis">
                    &nbsp; &nbsp; &nbsp; &nbsp; +
                  </span>
                  {{prod.itemName}}
                </td>
              </tr>
              <ng-container *ngFor="let stock of prod.stocksList">
                <tr (click)="stock.hideThis= !stock.hideThis" style="font-size:13px; background:#f5f7f6;"
                  [hidden]="!data.hideThis || !item.hideThis || !prod.hideThis">
                  <td>
                    <span *ngIf="stock.hideThis">
                      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-
                    </span>
                    <span *ngIf="!stock.hideThis">
                      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; +
                    </span>
                    {{stock.stockName}}
                  </td>
                </tr>
                <ng-container *ngFor="let prodItem of stock.itemItemsList">
                  <tr (click)="prodItem.hideThis= !prodItem.hideThis" style="font-size:12px"
                    [hidden]="!data.hideThis || !item.hideThis || !stock.hideThis || !prod.hideThis">
                    <td style="width:100%">
                      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{prodItem.itemName}} <br>
                      <div class="list-class">
                        <span>
                          <ul>
                            <li> Portion</li>
                            <li>{{prodItem.date}} </li>
                          </ul>
                        </span>
                      </div>
                    </td>
                  </tr>
                </ng-container>
              </ng-container>
            </ng-container>
          </ng-container>
        </ng-container>
      </tbody>
    </table>

Access the DEMO for more information.

Answer №1

To hide each "children", simply use forEach loop and assign a value to hideThis, for example:

expandOptions(event)
{
  if (event=="lvl1")
    this.users.forEach(user=>user.hideThis=true)

  if (event=="lvl2")
  {
    this.users.forEach(user=>user.hideThis=true)
    this.users.forEach(user=>{
      user.subItemsList.forEach(subItem=>{
        subItem.hideThis=true
      })
    })
  }
  ...repeat the same process for other options and elements...
}

Personally, I prefer using consistent variable names for clarity. It can be confusing when variables have random names.

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

Implement a callback function for unchecked checkboxes on change

Currently, I am working with a gridview that includes a checkbox field. My goal is to use jQuery to create a function that activates when an unchecked checkbox is checked. function clickAllSize() { alert("helloy"); } $(document).ready(function () { ...

"Oops! Looks like there's a reference error - the function you're trying

Javascript: function insertSmiley(a) { var $img = $(a).children('img'); $("#message").insertAtCursor(($("#message").data("wbb").options.bbmode) ? $("#message").data("wbb").toBB($(a)): $(a).html()); return false; } HTML: <a href= ...

Implementing a Collapse and Expand All feature within an Accordion Component

Hey there! I've been attempting to implement a Collapse All feature on my accordion but am having trouble figuring it out. The resource I've been referencing is this one. I've searched around and noticed that this accordion setup is a bit d ...

What is the best way to conceal a specific list item in an Angular 2 template?

I need help figuring out how to hide a selected list item that has been added to another list. I want the added item to be hidden from view in the original list. <div class="countries-list"> <span class="countries-list__header">{{'G ...

Content must be concealed following the third paragraph

Dealing with an API that generates content in p tags, which can become excessively long. Considered hiding the content after 400 characters, but it poses a risk of cutting through HTML tags. Instead, looking to hide the excess content after 3 paragraphs a ...

Access my web application to easily download Jira tickets in XML format with just a single click

In order to retrieve all tickets from Jira within a specific time period, I currently have to manually extract them by clicking on the extract button in XML format. Subsequently, I download this XML file onto my web application and save it into the databa ...

Falling rain animation in JavaScript

I'm a beginner in the world of JavaScript and I'm trying to create a div on a webpage with a rain effect. I've managed to generate random blue points within the div, but I'm struggling to make them move downwards. Here's the code I ...

Asynchronous Return in NodeJS Class Methods

Currently, I am in the process of developing a JavaScript class that includes a login method. Here is an overview of my code: const EventEmitter = require('events'); const util = require('util'); const Settings = require('./config ...

Utilizing AJAX for showcasing the data from an XML document

Our professor gave a brief explanation of AJAX, expecting us to showcase the data from an XML file in a scrollable text area on our website. Unfortunately, I am facing issues with loading the XML file into the designated div area. Any assistance or suggest ...

Utilizing React Material UI for form validation with error handling and informative helper text

I recently created a form in which I needed to validate the inputs. During my research, I came across the material UI library that has an attribute called error boolean, and another attribute called helperText for the TextField input of the form. However ...

What is the proper way to request permission for allowing others to access the mailto function?

I want to create a feature where users can open email on click using JavaScript. However, I have encountered an issue with using the mailto function in Chrome, as it requires changing the handlers settings to make it work. My query is whether it is possib ...

Having trouble understanding how to receive a response from an AJAX request

Here is the code that I am having an issue with: render() { var urlstr : string = 'http://localhost:8081/dashboard2/sustain-master/resources/data/search_energy_performance_by_region.php'; urlstr = urlstr + "?division=sdsdfdsf"; urlst ...

Encountering an error with [object%20Object] when utilizing ajaxFileUpload

I wrote a JavaSscript script that looks like this: $.ajaxFileUpload({ url: url, secureuri: false, fileElementId: ['upload-file'], dataType: "JSON", data:{ "sample_path":$(".demo-view-container-left .vie ...

Extract data from JSON object on the server side

Within my web service, I have a method that returns a JSON object: {name:'xx'} When making an Ajax request and parsing the response with 'eval', I encountered an issue. onComplete:function(req){ var data=eval(req.responseText); / ...

The parameter type must be a string, but the argument can be a string, an array of strings, a ParsedQs object, or an array of ParsedQs objects

Still learning when it comes to handling errors. I encountered a (Type 'undefined' is not assignable to type 'string') error in my code Update: I added the entire page of code for better understanding of the issue. type AuthClient = C ...

Using JavaScript's document.write method to display HTML code, along with a variable retrieved from a Flash application

Can I ask two questions at once? Firstly, how can I achieve the following? document.write(' <div id="jwplayer"> <center> <div id='mediaplayer'></div> <script type="text/javascript"> jwplayer('mediapl ...

What's the best way to use the like query in Mongoose?

Struggling with applying a Like query using Mongoose in the MEAN stack. Despite trying various solutions found online, nothing seems to work for me. Here is the model schema: const mongoose = require('mongoose'); const ItemTransactionSchema = ...

Sorting an array of objects in TypeScript may result in a type error

My data includes various categories, ages, and countries... const data = [ { category: 'Fish', age: 10, country: 'United Kingdom' }, { category: 'Fish', age: 9, country: 'United Kingdom' }, { category: ...

Is the Angular 7 router '**' wildcard feature failing to work properly when used in conjunction with lazy loaded modules and child routes?

I'm currently working on setting up a default route using the wildcard '**' with Angular's router. The intention is for this default route to load a lazy module and then handle its own routes internally. However, I have run into an issu ...

Can you suggest an alternative for the "return" statement within a "for loop" in order to retrieve all values from the loop?

When using numInOrder + " : " + nameInOrder;, the console only displays one value: "1 : a". However, I would like to see: "1 : a 2 : b 3 : c 4 : d 5 : e 6 : f" in the console.log output. Additionally, I do not want to use consol.log(numInOrder + " ...