Unexpected behavior observed in ng-select when pasting search values

When attempting to update an array of strings acting as the model for an ng-select, the values do not appear correctly in the search box.

The values that are displaying correctly are the ones selected from the dropdown menu. However, the numbers I manually add to the list do not display properly.

Select box in template:

<ng-select
 id="oNumberSelect"
 [items]="ownerNumberResults | async"
 [typeahead]="typeAhead$"
 bindLabel="desc"
 bindValue="code"
 dropdownPosition="bottom"
 [(ngModel)]="selectedOwnerNumbers"
 (ngModelChange)="handleSelectionChange()"
 [multiple]="true"
 [searchable]="true"
 multiple="true"
 style="overflow-y: auto; width: 100%"
 appendTo="body"
 (paste)="handlePaste($event)"
 minTermLength="3"
 [addTag]="true"
   >
</ng-select>

Referenced methods:

handlePaste(pasteEvent: ClipboardEvent): void {
 pasteEvent.stopPropagation();
 pasteEvent.preventDefault();
 const clipStrings:string[] = [...pasteEvent.clipboardData.getData('Text').trim().split(/[\s,)]+/)]

 this.selectedOwnerNumbers = [...this.selectedOwnerNumbers, ...clipStrings];
}

searchOwnerNumbers(): void {
 this.ownerNumberResults = this.typeAhead$.pipe(
  distinctUntilChanged(),
  debounceTime(500),
  switchMap(term => {
    return this.ownerHttpService.searchOwnerProperty('ownerNumber', term);
   }
  )
 );
}

handleSelectionChange(): void {
 console.log(this.selectedOwnerNumbers)
}

Select variables:

selectedOwnerNumbers: string[];
typeAhead$ = new Subject<string>();
ownerNumberResults: Observable<Option[]>;

I have attempted various techniques such as using sets and different array arrangements but I am unable to get the pasted values to display correctly in the UI.

Answer №1

It is common for this issue to occur when the bindValue and bindLabel do not match.

Consider adjusting the handle paste logic as shown below:

  handlePaste(pasteEvent: ClipboardEvent) {
    pasteEvent.stopPropagation();
    pasteEvent.preventDefault();
    const clipStrings: string[] = [
      ...pasteEvent.clipboardData
        .getData('Text')
        .trim()
        .split(/[\s,)]+/),
    ];
    this.selectedOwnerNumbers.push(this.ownerNumberResults.find(result => result.desc === clipStrings[0]).code);
    this.selectedOwnerNumbers = [...this.selectedOwnerNumbers]
  }

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

Utilizing JQuery to extract the image title and render it as HTML code

I am currently facing an issue with displaying an image in my project. I want to hide the image using CSS (display: none) and instead, retrieve the value of its title attribute and display it within the parent div by utilizing JQuery. Despite knowing tha ...

Code Wizard

I am currently working on a project to develop an HTML editor. How it Needs to Function Elements Inside: Text Area - Used for typing HTML as text Iframe - Displays the typed HTML as real [renders string HTML to UI] Various Buttons Functionality: When ...

What kind of registration does React Hook Form use?

When utilizing react-hook-form alongside Typescript, there is a component that passes along various props, including register. The confusion arises when defining the type of register within an interface: export interface MyProps { title: string; ... ...

Discovering the most efficient route on a looped set of items

My question may not be fully comprehensible, but essentially it involves the following scenario: I have created an interactive presentation that displays static images in a sequence to generate an animation. By clicking the Play button, the animation prog ...

"Effortless Auto-complete with Linked Content and Visuals

I've been searching everywhere and have not been able to find a solution for this issue. I can successfully use JQuery Easyautocomplete () with links, and also with images separately, but I can't figure out how to make it work with both. I am new ...

Null value returned by getDOM() function during transition from Angular 2 to Angular 4

I've encountered a unique challenge that has me stumped. Despite finding similar issues, the proposed solutions don't seem to work for my case. My issue arose when I decided to migrate a project from Angular 2 to Angular 4. I began by creating a ...

Having trouble getting my JavaScript code to function properly on Firefox browser

I have created a script where the cursor automatically moves to the next form field once it reaches its maximum length, in this case 1. Here is the JavaScript code: window.onload=function(){ var container = document.getElementsByClassName("container")[0] ...

Retrieve data from two databases and display the information from two separate arrays on a single ejs page after making two separate database calls

Currently, I am faced with a challenge where I need to render a page by passing two arrays that are populated by two separate database calls. It seems that when I only pass one array to the ejs page, everything works as expected. For a single array, my a ...

Implementing Dynamic Columns and Rows in GridLayout using NativeScript

My HTML code consists of two GridDatas and two buttons. When Button1 is pressed, gridData1 should be displayed, and when Button2 is pressed, gridData2 should be displayed. The number of rows and columns remains static. Here are the TypeScript snippets: g ...

Modify the information and return it to me

I am attempting to modify and return the content inside a custom directive (I have found some resources on SO but they only cover replacement). Here is an example: HTML <glossary categoryID="199">Hello and welcome to my site</glossary> JS . ...

Having trouble with the downloadUrl function when trying to generate a Google map locator in Opencart?

Currently, I am working on the development of a website using opencart at Within the dealers.php file, there is a function named query() which searches postcodes in the database to retrieve store location results and outputs them in XML format. For exampl ...

Tips for incorporating PHP $_SESSION data into a JavaScript file

What I've been doing is using $_SESSION['siteRoot'] to store the root address of my website (since it's part of a framework and can vary depending on how the site is accessed). The challenge now is incorporating this value into some of ...

Using AJAX autocomplete with Sys.Serialization.JavaScriptSerializer

I implemented an ajax autocomplete feature in ASP.NET where a method from a web service is called to fetch postal codes. public string[] GetNames(string prefixText, int count, String contextKey) { prefixText = prefixText.Trim(); XmlNodeList list; ...

NodeJS - issues with nodemon auto-reload feature causing my server to not

After successfully installing NodeJS version 4.4.5, I proceeded to install nodemon version 1.9.2 by following all the installation instructions using npm (npm install -g nodemon). In a newly created folder, I have my server.js file with some basic code: ...

Vue 3 + Vite: The router path was not found

I'm currently working on a project using Vue 3 and Vite, where I need to verify if the user is logged in with AWS Cognito before accessing the main page. Here's an example of my router.js: import { createRouter, createWebHistory } from &apo ...

Printing content from an Angular dashboard can be achieved by following a few

Currently, I am working on developing a legal document automation program for my company. However, I have encountered an issue during the final stages of completing this web application. For the layout and setup, I am using the default angular Dashboard l ...

I'm having trouble retrieving my variable within the socketcluster's socket.on function

How can I store the value of msg in the variable sample when sample is not accessible inside the callback function? import { Injectable } from '@angular/core'; import * as socketCluster from 'socketcluster-client'; @Injectable({ pro ...

Script fails to load upon accessing page through index hyperlink

I am facing an issue with my JavaScript elements not loading when I navigate to a form page from a link in the index. However, if I enter the URL manually or redirect from the login page, the JavaScript loads perfectly fine. The JavaScript code is consolid ...

Unable to loop through the Array

let Users = [ { name: 'John', id: '1', jp: 'USA' }, { name: 'Jane', id: '2', jp: 'Japan' }, ]; export function DisplayUsers(usersList) { return ( <div> {usersList?.map((user ...

A method for integrating a Child Component into a UI5 parent application without specifying them in the manifest.json

Seeking advice on loading a Child Component in a UI5 parent app. Currently working on creating a Fiori Launchpad that can dynamically load other apps as Child Components. I have been exploring methods mentioned in an insightful post at However, the imple ...