How can I remove specific items from a PrimeNG PickList?

Currently, I'm working on a page where updates are made using PrimeNG PickList. The initial state of the target list is not empty, but when selected items are moved from source to target list, they are not removed from the source list as expected.

From my understanding, PrimeNG internally uses the "==" operator which cannot be overridden. Is there a way to customize this behavior and exclude the selected items from the source list?

Answer №1

Although this solution may be a bit outdated, it could still benefit someone in need! I implemented a filter on the source array like this:

this.currentRoles = allRoles.filter(selectedRole => {
    return !this.user.selectedUserRoles.some(role => role.id === selectedRole.id && role.name === selectedRole.name);
});

It's important to note that I executed this code only after subscribing to certain essential objects (such as retrieving the list of available roles in the system and fetching the necessary user along with their associated roles).

Afterwards, within the HTML template, I connected [source] to currentRoles and linked [target] to the userRoles property on the respective user object.

Answer №2

I implemented a solution for this problem, although it might not be the most efficient since it's O(n)^2. However, it works well for shorter source lists.

for(let i = 0; i < this.list2.length; i++){
  for(let j = 0; j < this.list1.length; j++){
    if(this.list1[j].name === this.list2[i].name){
      this.list2.splice(i,1);
    }
  }
}
this.newList2 = this.list2;

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

Issue with accessing the 'subscribe' property in nested calls within Angular 2 due to it being undefined

I am trying to implement a subscription in company-list.component using the method getCompanies() from the company.service. However, I am encountering the following error: Cannot read property 'subscribe' of undefined Here is the code snippet ...

Using TypeScript and controllerAs with $rootScope

I am currently developing an application using Angular 1 and Typescript. Here is the code snippet for my Login Controller: module TheHub { /** * Controller for the login page. */ export class LoginController { static $inject = [ ...

What causes the HTML to not evaluate the values when the pipe is used?

I am currently utilizing a pipe for currency conversion, ensuring that the HTML values remain unevaluated. Let's take a look at the following pipe: transform(value: number, selectedCurrency: string, baseCurrency: string): number { if (selectedCu ...

Achieving a delayed refetch in React-Query following a POST请求

Two requests, POST and GET, need to work together. The POST request creates data, and once that data is created, the GET request fetches it to display somewhere. The component imports these hooks: const { mutate: postTrigger } = usePostTrigger(); cons ...

Retrieve the duplicated items from an array by comparing two specific properties in JavaScript

I need assistance in identifying and retrieving duplicate objects within an array that share similarities in 2 specific properties. Consider the object structure below: let arry = [ {Level: "A-1", Status: "approved"}, {Level: &q ...

Exporting key/value objects with React components as values in Typescript

I have a .tsx file where I need to export an object containing key/value pairs. Each value is going to be a React component used in another file. While I will have multiple key/value pairs, I'm focusing on just one at the moment. object.tsx import { ...

Injecting Dependencies in Angular 2 Without Using the Constructor

Exploring DI in Angular 2 has led me to implement a REST-Client using generic subtypes for concrete Datatypes like this: class RESTClient<T>{ constructor() { var inj = ReflectiveInjector.resolveAndCreate([HTTP_PROVIDERS]); this. ...

Angular's custom reactive form validator fails to function as intended

Struggling to incorporate a customized angular validator to check for date ranges. The validator is functioning correctly and throwing a validation error. However, the issue lies in the fact that nothing is being displayed on the client side - there are n ...

Revamping Tab Styles with CSS

Currently, I am working on a mat tab project. The code snippet I am using is: <mat-tab-group> <mat-tab [label]="tab" *ngFor="let tab of lotsOfTabs">Content</mat-tab> </mat-tab-group> If you want to see the liv ...

Issue: Module compilation unsuccessful (from ./node_modules/css-loader/dist/cjs.js) during upgrade from Angular 11 to Angular 16

Summary: Can anyone help with a strange error I encountered while updating my Angular application? Situation: After finally updating my Angular 11 app to Angular 16, I faced several fixable errors along the way. However, one particular error is now hind ...

Solutions for Utilizing Generic Mixins in Typescript

As a newcomer to Typescript, I have encountered an issue with mixins and generics. The problem became apparent when working on the following example: (Edit: I have incorporated Titian's answer into approach 2 and included setValue() to better showcas ...

Ensure the proper sequence of field initialization within a TypeScript class constructor

Is there a way to ensure the proper initialization order of class fields in TypeScript (4.0) constructors? In this example (run), this.x is accessed in the method initY before it's initialized in the constructor: class A { readonly x: number rea ...

The TypeError encountered in an Ionic pipe states that the property 'toString' cannot be read as it is undefined

I have a news application built in Ionic 4. The pubDate property of the UutinenPage class is being asynchronously assigned a value of data.items[this.id].pubDate in the uutinen.page.ts file. The expected format of this value is something like 2019-02-19 04 ...

Having trouble locating the .ts module when executing a Node script with experimental modules enabled

I am currently developing a node express project and I need to run a node script from the terminal. Within my project, there are some .ts files that I want to include in the script (MyScript.js). Below is the content of MyScript.js: import get from &apos ...

I'm having trouble getting angular/material to function properly

I've used the following command to install: npm install --save @angular/material @angular/cdk Afterwards, I made additions to app.module.ts import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { MatButto ...

Exploring the Power of SectionList in Typescript

How should SectionList be properly typed? I am encountering an issue where this code works (taken from the official documentation example): <SectionList renderItem={({item, index}) => <Text key={index}>{item}</Text>} renderSectionHea ...

Is there a user-friendly interface in Typescript for basic dictionaries?

I'm not inquiring about the implementation of a dictionary in Typescript; rather, I'm curious: "Does Typescript provide predefined interfaces for common dictionary scenarios?" For instance: In my project, I require a dictionary with elements of ...

Integrating Angular with Oracle

Exploring the possibility of building applications with Angular/ORDS/Oracle, as I am relatively new to this particular web stack. From what I've gathered so far, it seems like we need a front end, back end, and database in order to create a complete a ...

Replicating the Angular project folder is not effective

Instructions for using Angular's quickstart script are as follows: git clone https://github.com/angular/quickstart.git quickstart cd quickstart npm install npm start If I follow these steps, everything works perfectly. However, if I duplicate this d ...

Embarking on a New Project with Cutting-Edge Technologies: Angular, Node.js/Express, Webpack, and Types

Recently, I've been following tutorials by Maximilian on Udemy for guidance. However, I have encountered a roadblock while trying to set up a new project from scratch involving a Node/Express and Angular 4 application. The issue seems to stem from the ...