"Unable to select all items in Mat-Selection-List with the selectAll()

I am encountering an issue while trying to incorporate selectAll() and deselectAll() functions. The error message that I am receiving can be viewed here.

My implementation involves a template-driven form, and below is the snippet of my code:

<mat-selection-list name="role" *ngIf='userActions === AssignmentTypesForProductivityAppsEnum.UserRole'
                    [(ngModel)]="assignProductivityApplication.roleIds" #UserRoles="ngModel" multiple required>
                    <mat-list-option #allSelected (click)="selectAll(allSelected.selected)" [value]="0"
                        checkboxPosition="before">All</mat-list-option>
                    <mat-list-option *ngFor="let role of userRoles | searchFilter : searchTerm : 'role'; let i=index"
                        checkboxPosition="before" [value]='role.guId'>
                        {{role.role}}
                    </mat-list-option>
                </mat-selection-list> 

Below is the corresponding logic in the TypeScript file:

@ViewChild('allSelected', {static: true}) private allSelected: MatSelectionList; 
selectAll(checkAll) {
        if (checkAll) {
            this.assignProductivityApplication.UserRoles = [];
            this.assignProductivityApplication.UserRoles.push(... this.userRoles.map(item => item.guId));
            this.allSelected.selectAll();
        }
      }

Answer №1

Export matSelectionList in the mat-selectin-list component by moving the allSelected template variable to mat-selection-list.

Give this a try:

<mat-selection-list name="role" #allSelected  *ngIf='userActions === AssignmentTypesForProductivityAppsEnum.UserRole'
                        [(ngModel)]="assignProductivityApplication.roleIds" #UserRoles="ngModel" multiple required>
                        <mat-list-option (click)="selectAll(allSelected.selected)" [value]="0"
                            checkboxPosition="before">All</mat-list-option>
                        <mat-list-option *ngFor="let role of userRoles | searchFilter : searchTerm : 'role'; let i=index"
                            checkboxPosition="before" [value]='role.guId'>
                            {{role.role}}
                        </mat-list-option>
</mat-selection-list>

Check out an example here

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

The decision between using multiple then statements or consolidating your code into a single then statement depends on the specific requirements

I am facing a situation where an api call returns data asynchronously and I have 2 methods that need to execute after the call is completed. However, these methods do not generate new promises on their own. Can const foo = new Promise(…); foo.then(() = ...

nsIProcess - Launch with Background Execution and Deferred Activation

Currently, my method of launching Firefox is as follows: var exe = FileUtils.getFile('XREExeF', []); //this provides the path to the executable var process = Cc['@mozilla.org/process/util;1'].createInstance(Ci.nsIProcess); process.init ...

Differences in disabled option value selection between jQuery legacy and updated versions

Recently, I upgraded the jQuery version in my project from 1.7.2 to 2.1.1 and included jquery migrate for fallback support. During testing, I encountered an issue with the value of a select element in jQuery versions 2.1.1 and 1.7.2: For version 1.7.2, t ...

routerLinkActive maintains its active state even after another link has been clicked

<ul> <li routerLinkActive="active"><a routerLink="/">One</a></li> <li routerLinkActive="active"><a routerLink="/somewhere">Two</a></li> </ul> Upon clicking the link Two, both links are being hi ...

What is the best way to transfer a value to v-model using emit?

I'm having trouble passing the selected value from a select field to v-model. Currently, the code only seems to work with a v-text-field. <script> export default { name: 'FormSelect', props: { model ...

Concealing tooltip when button is clicked using jQuery

I am facing an issue where the tooltip does not hide on button click. Below is the code for the button in question: <button class="btn btn-outline-inverse ajax_addtocart additems ...

The URL provided for the Ajax HTTP request is not accurate

Consider the following JavaScript code: <script type="text/javascript" charset="utf-8> function goForLogin() { var xmlhttp; xmlhttp=new XMLHttpRequest(); xmlhttp.open("POST","/account/login",true); xmlhttp.s ...

A step-by-step guide to creating a clipboard stream using guacamole-common.js

When utilizing Guacamole.client.createClipboardStream to generate a clipboard stream and sending the stream on paste event, I noticed that the remote clipboard remains empty. const customStream = client.createClipboardStream("text/plain"); cust ...

Reloading the page using ajax technology

What is the best way to handle logging out on a webpage without reloading the page? Thanks in advance!) My perspective: def logout(request): auth.logout(request) html = render(request, 'base.html') return html Using Ajax: $('a[hre ...

Determine the frequency of keys and transform the data into an array of objects, with each object corresponding to a key and its frequency

Illustration of Frequency Count [ { language: 'JavaScript' },{ language: 'JavaScript' },{ language: 'TypeScript' }, ] CONVERSION TO = [ { language: 'JavaScript', count: 2 }, { language: 'C++', count: 1 ...

Best practices for setting up a select dropdown menu?

What is the best way to create a selection list using three different models? Here is my approach: HTML: <ng-container [(ngModel)]='user.id' name="id" > <select *ngFor="let userRole of userRoles" required> <option *ngFor="l ...

An error is encountered when using the require() function to import: Unable to access the property 'createsocket' as it is undefined

After using require, the import function works as expected: let dgram = require('react-native-udp') However, when attempting to use the same module with import: import dgram from 'react-native-udp' An error occurs: Cannot read prope ...

Modify the status of the variable specified within the ng-repeat loop from the controller perspective

Is it possible to modify the value of a variable declared in ng-repeat from the controller? I am attempting to set an initial value for a basic variable called "opened" which is used for toggling within an ng-repeat, directly inside the controller. <d ...

What is the reason behind the non-reversible nature of atob and btoa

Looking for a simple way to obscure answers to quiz questions in Markdown temporarily? The idea is to reveal the answers during the presentation, no need for secure encryption. Considered using atob('message I want to obfuscate') and letting stu ...

Alphabetical icons designed with Angular Material styling

I've been on the hunt for angular material icons representing each letter of the alphabet, specifically "R," "A," and "V." Unfortunately, I haven't had any luck finding them here or here. What I envision are simple circles with a capital letter ...

Collapsible list in Angular2 sidenav: ensuring only one sublist remains open

Presenting a functional sidenav demo with Angular 2, TypeScript, and Material Design components. The sidenav features a UL, with the Sites and Users anchors expanding to display their own sub-list. Check out the Plunker here Here is the HTML code for the ...

Is it possible to verify .0 with regular expressions?

In my project, I have a field that requires whole numbers only. To validate this, I used a regex validation /^\d{1,3}$/ which successfully validates whole number entry and rejects decimal points starting from .1. However, I encountered an issue where ...

Activate a drop-down feature to refresh the page with a link attached

When I click on a different language, the drop down does not change to that specific language. My code was functioning properly until I added an href attribute to it. Here, you can see my JavaScript function which handles the language selection logic: $(d ...

Working with JSON data retrieved from a PHP and MySQL backend in an AngularJS application

Having difficulty handling the JSON response from PHP. I am using AngularJs to display the received JSON data. As a newcomer to Angular, I attempted a basic exercise and would appreciate some assistance. Thank you in advance. index.html <!DOCTYPE HTML ...

Angular @Input set function not being activated during unit testing

Within my Component @Input('price') set setPrice(price) { this.price = price; this.modifyTotalAmount(); } Unit Testing (component.spec.ts) it('should execute function ', () => { spyOn(fixture.componentInstance, ' ...