HTML code for adding a dropdown menu inside a table cell

I am trying to set up a table where one cell functions as a dropdown menu.

The data.field is being fetched from the backend and I want it to be displayed during rendering.

Additionally, a fList is also retrieved from the backend. When a user clicks on the cell, it should open the list as a dropdown menu. I have included the code below, but unfortunately, no dropdown menu is appearing.

 <tr class="dropdown" *ngFor="let data of dataList | dataPipe">

     <td class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">{{data.field}}
         <ul class="dropdown-menu" *ngFor="let f of fList">
             <li>{{f}}</li>
                    
         </ul>
     </td>
                
 </tr>

Answer №1

It's essential to iterate through dropdown values instead of the dropdown menu itself. For your situation, the dropdown values are represented by <li>. Give this a try:

<tr class="dropdown" *ngFor="let data of dataList | dataPipe">

     <td class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">{{data.field}}
         <ul class="dropdown-menu">
             <li *ngFor="let f of fList" (click)="checkValue(f)">{{f}}</li>
                    
         </ul>
     </td>
                
 </tr>

Make sure to implement this in your .ts file:

public checkValue(value){
console.log(value);
alert(value);
}

This approach might be more effective:

<select (change)="onChange($event.target.value)">
    <option *ngFor="let f of fList">{{f}}</option>
</select>
onChange(value) {
    console.log(value);
}

Answer №2

If you're looking to update a table cell with a value selected from a dropdown:

<tr class="dropdown" *ngFor="let data of dataList | dataPipe; let i = index;">

 <td class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">{{data.field}}
         <td class="btn btn-primary dropdown-toggle" type="button" data- 
     toggle="dropdown">{{data.field}}
     <ul class="dropdown-menu">
         <li *ngFor="let f of fList" (click)="onSelect(f, i)">{{f}}</li>
                
     </ul>
 </td>

 </td>
            
onSelect(selectedValue, index){
this.dataList[index].field=selectedValue;
}

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

Storing information from a table into LocalStorage

$('.new_customer').click(function () { var table = $("table"); var number = table.find('tr').length; table.append('<tr id="' + number + '"><td><input type="button" class="btn btn-success btn-xs" ...

The CSS scrollbar is visible on the screen, but for some reason,

I'm seeking a solution to enable scrolling within a div container that can scroll both horizontally and vertically. body { height: 100%; width: 100%; overflow: hidden; } .wrapper { width: 75%; padding: 0px 5px 0px 8px; top: 20px; whit ...

Guide to utilizing props conditionally in a Material UI TextField component

I am looking to build a comprehensive component using Material UI TextField along with Formik While I have managed all other props, I am facing difficulty in dealing with value and onChange, I have tried using 2 functions for onChange, but find it cumber ...

Encountering an error while trying to implement strong typing in a function on a Node API service: 'Unexpected token ":"'

I've developed a TypeScript Node API service and here's a snippet of my code: class dataStreamConfig { constructor() { } conclaveObj = (firstParam: string, secondParam: number, thirdParam: any): any => { //my ...

Toggling Bootstrap Tooltip divs within a nested structure triggers the Tooltip of the parent div

I have a setup with 2 nested <div> elements, both utilizing the tooltip() function. When I hover over the inner <div>, the tooltip of the outer <div> also displays. To resolve this issue, I attempted to clear the title of the inner <di ...

Can you explain the significance of the "--" within CSS selectors?

My CSS skills are not the greatest and I stumbled upon a snippet of code that is puzzling to me. .grid-product__title.grid-product__title--body:hover { text-decoration: underline; } I checked MDN and it usually defines a reusable property, like a vari ...

Exploring the Applications of @NgModule

The new @NgModule system has me puzzled. In the past, I could easily specify a @Component's directive dependencies using the directives: [] attribute within the @Component meta object. For example: @Component({ /* ... */ }) export class Cmp1 {} @Com ...

Managing input and output using a collaborative service

I've been working on refactoring some code that contains a significant amount of duplicate methods between two components. Component A is a child of component B, and they can be separate instances as intended. The issue I'm facing revolves around ...

HTML5 enables users to pick their preferred font style

In my JavaScript and HTML5 course, I am working on developing a website where users can choose the background color and decide between using SANS SERIF or SANS fonts. The background color selection feature is already functioning successfully -- var inputC ...

AngularJS allows us to easily include the route provider templateUrl path in the view div, making

I have the following route provider setup: $routeProvider .when('/', { templateUrl: '/slapppoc/Style Library/TAC/Views/tarification/home.html', controller: 'homeController' /* resolve: { // This f ...

Exploring the capabilities of google-diff-match-patch within the Angular framework

Seeking a way to incorporate the google diff/match/patch lib into an Angular application for displaying the variance between two texts. Here's how I plan on using it: public ContentHtml: SafeHtml; compare(text1: string, text2: string):void { var ...

Implementing a JavaScript file and ensuring W3C compliance

I recently purchased a template that included a javascript file in the main page with the following code: <script src="thefile.js?v=v1.9.6&sv=v0.0.1"></script> Upon inspection, I noticed there are two arguments at the end of the file ...

How come the subitems of a second-level nested list do not appear when hovering over a hyperlink?

Show "News" in French when hovering over the French option. ul#topmenu li a#HyperLink:hover ul { background-color: pink; display: list-item; } <ul id="topmenu"> <li class="langHorzMenu"> <a href="#" id="HyperLink">French</ ...

Arrange the angular datatables in a specific order without displaying the usual user interaction triangles

I am looking to arrange the data in a fixed manner without any user interaction for sorting. However, it seems that I can either completely disable ordering like this: this.dtOptions = { paging: false, lengthChange: false, searching: false, orderi ...

The RxJs Observer connected to a websocket only triggers for a single subscriber

Currently, I am encapsulating a websocket within an RxJS observable in the following manner: this.wsObserver = Observable.create(observer=>{ this.websocket.onmessage = (evt) => { console.info("ws.onmessage: " + evt); ...

Modify table cell content based on boolean value - HTML

Currently, I am working with a table that displays properties from an object. Is there a simple method to change the text displayed in a cell instead of the true/false values it is currently set to? For example, one of the columns indicates whether the ob ...

Ways to add items to an array adjacent to items sharing a common property value

I have an array consisting of various objects const allRecords = [ { type: 'fruit', name: 'apple' }, { type: 'vegetable', name: 'celery' }, { type: 'meat', name: 'chi ...

How can I prevent the installation of my Ionic 2 application on devices that have been rooted or jailbroken?

I am currently working on a project involving an Ionic 2 and Angular 2 application. I need to implement a feature that checks whether the device is rooted (in the case of Android) or jailbroken (in the case of iOS). I have experimented with various packag ...

When you initiate a prevent default, both the Angular and Bootstrap UI tabs will become active simultaneously

I am facing a CSS issue while utilizing Angular UI-Tab. The problem arises when I have two tabs and need to stop tab switching based on a specific condition. To achieve this, I implemented prevent default. However, the CSS displays both tabs as active bec ...

"Unveiling the invisible: Revealing hidden characters in PHP

Currently I am dealing with some code extracted from a MySQL column. Within this code are hidden characters like "\t" and "\n". I am attempting to showcase the raw code in a DIV while also making sure these hidden characters are visible. The cod ...