What is the best method for transferring chosen items to a different multiple select using Angular?

How can I transfer selected items to another list, in order to manipulate the final object/array later on?

student.component.ts:

students= [];
studentsFinal = [];

onGetStudents() {
    this.studentService.getStudents()
          .subscribe(
          (students: any[]) => this.students = students,
          (error) => console.log(error),
        );
      }

student.component.html:

        <h5>Final list of students</h5>
        <div class="col-xs-6">
            <select name="multiselect1" multiple class="form-control" name="myselecttsms1">
                <option *ngFor="let studentFinal of studentsFinal" value="{{ studentFinal.Id }}">{{ studentFinal.Name }}</option>
            </select>
            <br>
        </div>

        <h5>Students</h5>
        <div class="col-xs-6">
            <select name="multiselect2" multiple class="form-control" name="myselecttsms2">
                <option *ngFor="let student of students" value="{{ student.Id }}">{{ student.Name }}</option>
            </select>
            <br>
        </div>

Answer №1

To enable multiple selection, add [ngModel] and (ngModelChange)="selectedStudents($event)" event to your select element

<select [ngModel]="assignedStudents" (ngModelChange)="selectedStudents($event)" name="multiselect2" multiple class="form-control">
    <option *ngFor="let student of students" [ngValue]="student">{{ student.Name }}</option>
</select>

In your TypeScript file, include the selectedStudents() function:

temporaryArray = [];
selectedStudents(students){
   this.temporaryArray = students;
}

//called on button click
assigne(){
   this.assignedStudents = this.temporaryArray;
}

After that, you can display the selected students in another select element:

<select multiple class="form-control">
    <option *ngFor="let student of assignedStudents" [ngValue]="student">{{ student.Name }}</option>
</select>

For a complete example based on the question and comments, you can refer to this link: https://stackblitz.com/edit/multiselect-from-one-array-to-other

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

Leveraging the power of Bootstrap in combination with Angular 2, integrate both ng-bootstrap and the traditional Bootstrap to enhance

Beginning a fresh Angular 2 Project, I am inclined to incorporate Bootstrap 3 for the user interface. What would be the most advisable approach in this scenario? Is it feasible to blend ng-bootstrap and the original Bootstrap components? I noticed that th ...

Struggling to overcome the TS2322 error when assigning generic values

I am currently working on developing higher-order React components that will include default values for components labeled as "named". Here is a basic implementation example: type SomeProps = { a: string } type Variants = 'variantA' | 'var ...

Am I on track with this observation?

I am currently using the following service: getPosition(): Observable<Object> { return Observable.create(observer => { navigator.geolocation.watchPosition((pos: Position) => { observer.next(pos); observer.c ...

Angular2 API call error: The function .map defaultOptions.merge is not recognized

When attempting to call the API using the post method, I encountered the following error message: this._defaultOptions.merge is not a function ... I looked into some solutions and tried importing the following without success: import 'rxjs/add/ope ...

A guide on extracting a JSON data with a BigInt type using TypeScript

I am facing an issue with parsing a bigint from a JSON stream. The value I need to parse is 990000000069396215. In my TypeScript code, I have declared this value as id_address: bigint. However, the value gets truncated and returns something like 9900000000 ...

I'm struggling with finding an answer to this question: "What is the most effective way to conduct a

I'm experimenting with a file upload. I decided to encapsulate the FileReader() inside an observable based on information I found in this discussion thread: onFileSelected(event: any) { this.importJsonFileToString(event.target.files[0]) .p ...

Tribal Code Typescript Compiler

Typescript is a great alternative to Javascript in my opinion, but it bothers me that it requires node.js as a dependency. Additionally, I find it frustrating that there seems to be only one compiler available for this language, and it's self-hosted. ...

Steps for resetting the ng-multiselect-dropdown

Is it necessary to wrap the ng-multiselect-dropdown in a form in order to reset it? code - app.component.html <div> <p id = "node">Select a Root API Object - </p> <p style="width:50%"> <ng-multiselect-dropdown [placeho ...

Initialization of an empty array in Typescript

My promises array is structured as follows: export type PromisesArray = [ Promise<IApplicant> | null, Promise<ICampaign | ICampaignLight> | null, Promise<IApplication[]> | null, Promise<IComment[]> | null, Promise<{ st ...

"Encountered an error in Angular: ContentChild not found

Working with Angular 5, I am attempting to develop a dynamic component. One of the components is a simple directive named MyColumnDef (with the selector [myColumnDef]). It is used in the following: parent.compontent.html: <div> <my-table> ...

Is it possible to execute TypeScript class methods in asynchronous mode without causing the main thread to be blocked?

Creating an app that retrieves attachments from specific messages in my Outlook mail and stores the data in MongoDB. The challenge lies in the time-consuming process of receiving these attachments. To address this, I aim to execute the task in a separate t ...

In Angular, what is the process of dynamically updating an input label?

Within my form, I have two input fields - one is a "select" and the other is an "input". Utilizing Angular, my goal is to dynamically change the text label of the second input based on the selection made in the first input. ...

Error: Missing provider for InjectionToken DEFAULT_LOCALE

I have been exploring the setup of an Angular 2 project with i18n. I followed a tutorial here that uses Transloco, and everything seemed to work perfectly. However, when running the unit tests, I encountered an error that I couldn't find any informati ...

How to utilize the async pipe on an observable<Object> and connect it to a local variable in the HTML using Angular

Hey there! So, I have this observable called user$ which has a bunch of properties such as name, title, and address. component{ user$:Observable<User>; constructor(private userService:UserService){ this.user$ = this.userService.someMethodRet ...

Rearranging the export order in a barrel file for Angular 2 services can have a significant impact on dependency

After spending some time puzzling over this issue, I'm reaching out for some assistance. In my development workflow, I store all of my core service files in a shared folder that I then combine into a single file named common.ts. These services are i ...

Function generation using TypeScript

I'm in the process of creating a tool to automatically generate boilerplate code for me. The concept involves parsing all .json files within a folder called config, and then creating interfaces and auxiliary functions based on that data. Thanks to t ...

Optimal method for consistently displaying the contents of a node queue with the help of Bottleneck technology

I am currently utilizing the Bottleneck node package and my goal is to maintain a consistent record of everything in the queue. I am looking for a way to immediately display in a console window whenever a job enters the queue, with the following informati ...

"Utilizing an if-else statement within a forEach loop

I have an array of ages and I want to determine the age range for each age along with the count, then push it into my object. The issue I am facing is that if there are multiple ages in the same range, it gets pushed twice. I only want to push it once and ...

Executing multiple instances of the cascading dropdown fill method in Angular 7

I am currently working on an Angular app that includes cascading comboboxes for country and state selection. However, I have noticed that the get states() method in my state.component.ts file is taking a long time to run. What could be causing this issue? ...

steps to activate button once the form is validated

Can you provide guidance on disabling and enabling a button when the form is valid? I have several conditions in my form: Name on Card: Should only contain English alphabetic letters. Card Number: Must consist of exactly 16 numbers. Expiration Month: A ...