Interactive checkbox menu with expandable sections in Angular

I am trying to design a select menu with dynamic accordion-style checkbox options similar to the images linked below:

https://i.sstatic.net/lUsyD.png

or like this:

https://i.sstatic.net/rJngZ.png

The data structure I am working with is as follows:

 let allowedShifts = [{
    category: "Days",
    name: "D"
},
{
    category: "Evenings",
    name: "E"
},
{
    category: "Nights",
    name: "N"
},
{
    category: "Days",
    name: "d"
},
{
    category: "Nights",
    name: "n"
}];

I attempted to achieve the desired functionality using multiple select menus, but faced challenges filtering the data based on category. Here is a snippet of my code for reference:

HTML:

<select multiple class="form-control" name="shifts" id="exampleSelect2" [(ngModel)]="allowedShifts">
<optgroup label="Days">
    <option [value]="shiftIcon.name" *ngRepeat="let shiftIcon in allowedShifts | filter: {category: 'Days'}">
        {{shiftIcon.name}}
    </option>
</optgroup>
<optgroup label="Evenings">
    <option [value]="shiftIcon.name" *ngFor="let shiftIcon in allowedShifts | filter: {category: 'Evenings'}">
        {{shiftIcon.name}}
    </option>
</optgroup>
<optgroup label="Nights">
    <option [value]="shiftIcon.name" *ngFor="let shiftIcon in allowedShifts | filter: {category: 'Nights'}">
        {{shiftIcon.name}}
    </option>
</optgroup>

Answer №1

Your current code seems to be a mixture of Angular2 and AngularJS, which are actually quite different as you may have noticed during your editing process. In Angular2, there is no ngRepeat or filter pipe functionality. I would suggest restructuring your allowedShifts array, although without knowing more about your specific use case, it's hard for me to provide a concrete solution.
I recommend diving deeper into Angular documentation, particularly exploring *ngFor. Check out this tutorial from the Angular team (https://angular.io/tutorial) to help guide you in the right direction quickly.
If you're looking to implement a multi-select feature, like shown in the second image, consider using Angular Material (https://material.angular.io/components/select/overview). It's user-friendly and caters to your needs, developed by the same team behind Angular. It might take some time if you're new to it, but the investment will definitely pay off.

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

Angular date filter with no visual modifications

I need help with formatting a date. 2016-05-22 08:00:00 I have been trying to apply a filter, but it's not working as expected. <td>{{ event._source.event_date | date : "dd.MM.y"}}</td> Could someone please point out what I am missing ...

The MongoDB entity is failing to save, despite a successful attempt

This Represents the Model var mongoose = require('mongoose'), Schema = mongoose.Schema; var PostSchema = new Schema({ post_author: { type: Schema.ObjectId, ref: "User" }, post_text: String, total_comments ...

Deploying Angular 2 with CLI

I developed a web application using Angular 2 CLI and now need to deploy it on a local server for others to preview. I am facing an issue where the 'dist' folder, which should contain all the generated files, is only created when I run 'ng b ...

Creating a custom interface in TypeScript allows you to define and enforce specific

Looking for guidance on how to properly declare an array of objects using a custom interface in TypeScript. Below is the interface I am working with: export interface Member { name: string, isLoggedIn: boolean, loginTime: Date } I attempted to decl ...

Angular Kendo Grid (version 1.4.1)

I am currently encountering an issue where I want to select the first row in a Kendo Grid. Here is the code snippet that I have implemented: In my Component file: export class SampleComponent{ gridView: GridDataResult; mySelection: number[] = [0]; ...

Changing Minute to HH:MM:SS in Angular 2

Instructions:- Minutes = 1220; Solution:- Time = 20:20:00; Is there a way to convert the minute value into a time format in Angular 2? ...

Tips for managing 'single click' and 'double click' events on a specific html element in typescript:Angular 2 or 4

My problem lies in the fact that both events are activated when I double click. For instance, I want distinct functionality to occur for each event trigger. <a (click)="method1()" (dblclick)="method2()"> Unfortunately, both method1() and method2() ...

Ways to prevent redundancy of code within a class

I have created two classes that extend an abstract class: class SubstitutionTeacher extends SubstitutionAbstract { abstract _save(); } class SubstitutionFree extends SubstitutionAbstract { public _save() { } } class ...

When following the Redux container pattern, where do you think 'connect' should be placed - in the components or the containers?

Currently, I have all my connect statements in the containers, which requires me to connect all individual states and actions in one large @connect statement: @connect( (state: RootState): Pick<App.Props, 'state1' & 'state2' & ...

Refresh the Angular view only when there are changes to the object's properties

I have a situation where I am fetching data every 15 seconds from my web API in my Angular application. This continuous polling is causing the Angular Material expansion panel to reset to its default position, resulting in a slow website performance and in ...

Limit potential property values based on the existing keys within the object

My structure looks like this: export interface AppConfig { encryptionKey: string; db: TypeOrmModuleOptions; } export interface BrandsConfig { /** * Brand name */ [key: string]: AppConfig; } export interface IConfig { brands: BrandsConfig ...

What is the best way to transfer data received from an observable function to use as an input for another observable function?

After carefully declaring all the variables, I am facing an issue with passing the value obtained from the first observable function (this.acNum) as a parameter to resolve the second observable function within the ngOnInit method. Despite displaying correc ...

Utilizing Angular: Leveraging the template reference within ng-content

I am looking for a way to incorporate dynamic content around an input in my Angular application. My approach involves creating a custom component and utilizing ng-content to connect the behavior with the input. Here is an example: <my-wrapper> ...

Create a TypeScript interface that represents an object type

I have a Data Structure, and I am looking to create an interface for it. This is how it looks: const TransitReport: { title: string; client: string; data: { overdueReviews: number; outstandingCovenantBreaches ...

Trigger an event in Angular using TypeScript when a key is pressed

Is it possible to activate a function when the user presses the / key in Angular directly from the keydown event? <div (keydown.\)="alerting()"> </div> <div (keydown.+)="alerting()"> </div> Both of these ...

What causes a union with a conditionally assigned property to lead to more relaxed types than anticipated?

Take a look at this TypeScript code snippet: const test = Math.random() < 0.5 ? { a: 1, b: 2 } : {}; Based on the code above, I would assume the type of object 'test' to be: const test: { a: number; b: number; } | {} This is the most str ...

Tips for retrieving the checked status of a Material UI <Checkbox/> component and changing it to false upon clicking a different icon (such as a delete icon)

Greetings! I have encountered a problem that I am hoping someone can assist me with. I am looking for information or guidance on how to handle a specific issue. The challenge I am facing involves accessing the checked property of a material-ui <Checkbo ...

Guide to aligning a component in the middle of the screen - Angular

As I delve into my project using Angular, I find myself unsure about the best approach to rendering a component within the main component. Check out the repository: https://github.com/jrsbaum/crud-angular See the demo here: Login credentials: Email: [e ...

Error message "After the upgrade to Angular 15, the property 'selectedIndex' is not recognized in the type 'AppComponent'."

My Ionic 6 app with capacitor has been updated in the package.json file. These are the changes: "dependencies": { "@angular/common": "^15.1.0", "@angular/core": "^15.1.0", "@angular/forms": "^15.1.0", "@angular/platform-browser": "^15.1. ...

Express app throws an error when testing with Jest due to Multer being undefined

I am currently using jest to run tests on my TypeScript Express application that utilizes multer for file uploads. However, I encounter an error specifically during testing which says "TypeError: Cannot read properties of undefined (reading 'disk ...