Is the ng-selector in Angular2 not sorting items alphabetically as expected?

This code snippet demonstrates the use of ng-selector in an .html file

<ng-selector 
                name="company"
               [(ngModel)]="company_selected"

                [formControl]="loanApplyForm.controls['company']"
                (loadValues)="fetchHLCompany($event)"
                id-field="id" label-field="name"
                 [options]="company_values"
                 allow-creation="true"
                 placeholder="Search your company">
</ng-selector>

I am facing a couple of issues with this implementation. When I enter a value, it prompts to add even if the typed value is already in the list. I want it to only ask to add if the input value is not in the existing list. I tried removing 'allow-creation' but then it doesn't prompt at all. Secondly, the search functionality is not sorting results alphabetically. Any assistance on these matters would be greatly appreciated. Thank you in advance.

Answer №1

If I understand correctly, you can achieve the following:

<ng-selector 
                name="company"
                [(ngModel)]="company_selected"

                [formControl]="loanApplyForm.controls['company']"
                (loadValues)="fetchHLCompany($event)"
                id-field="id" label-field="name"
                [options]="company_values"
                allow-creation="checkAddNewItem()"
                placeholder="Search your company">
</ng-selector>

Here is the function being used:

checkAddNewItem() {
   return !company_values.filter(item => item.includes(company_selected)).length;
}

This function checks if any values in the array contain the inputted values. If any do, it returns true and allows for the addition of that value.

In response to a follow-up question about sorting the options list, you need to sort it before setting it in the component upon retrieval.

company_values.sort((a, b) => a.localeCompare(b));

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

Leverage the power of InfiniteSWR in your project by integrating it seamlessly with

If you're looking for a comprehensive example of how to integrate useSWR hooks with axios and typescript, check out the official repository here. import useSWR, { SWRConfiguration, SWRResponse } from 'swr' import axios, { AxiosRequestConfig, ...

`A bug in my Angular 4 application causes a TypeError when hosted on IIS`

After hosting an Angular 4 app on IIS, it seems there is an issue when accessing the app from machines other than the ones used for development. An error message " 'Uncaught TypeError: undefined is not a function' common.es5.js:3084" appears on t ...

Dynamic React Gallery with Interactive Image Picker

Looking to develop a new photo management application as an alternative to Google Photos, with a focus on displaying and selecting images in a user-friendly way. Currently using the react-grid-gallery library for this purpose. Here is my current implement ...

Submitting forms using Ajax in Angular 2

As a novice in Angular 2, with some experience in AngularJS, I am looking for guidance on how to utilize $.ajaxSubmit() or a similar method to submit a form using AJAX instead of the default form submission. Specifically, I am interested in uploading files ...

Destructuring an array of strings for use as parameters

Hey guys, I'm working with an array of keys here Example 1: let keyArray = ['x', 'y', 'z'] I'm trying to find a way to use these keys as parameters without repeating them multiple times. Do you have any suggestions ...

Angular package that includes horizontal scrolling functionality with arrow buttons

Currently seeking an Angular 2-6 package featuring a horizontal image scroll with arrows, similar to the functionality on Airbnb: Any suggestions or recommendations are greatly appreciated – thank you! ...

``In Angular 12, what are the best ways to tackle the CORS problem?

I am currently working on a project using Angular and I have encountered an issue with CORS error while trying to submit the login API. I have also included a screenshot of the error for reference. Any advice or suggestions would be greatly appreciated. AP ...

User interface designed for objects containing multiple keys of the same data type along with a distinct key

I have a question that relates to this topic: TypeScript: How to create an interface for an object with many keys of the same type and values of the same type?. My goal is to define an interface for an object that can have multiple optional keys, all of t ...

What could be causing the errors I'm encountering in my TypeScript component within AngularJS?

I am working with an AngularJS component written in TypeScript called news.component.ts. This component makes a call to a service named google.service.ts in order to fetch news RSS using a service that converts XML to JSON. Within the NewsComponent, I hav ...

What is the method for adding local images to FormData in Expo version 48 and above?

When working with Expo v47 and its corresponding React Native and TypeScript versions, FormData.append had the following typing: FormData.append(name: string, value: any): void An example of appending images using this code could be: const image = { uri ...

How to troubleshoot a click event not being stopped in Angular 2

Despite using HostListener on a click event to prevent event propagation, the click handler on the actual element still activates. Below is the pertinent code and a stackblitz demonstration for reference. // Custom Directive @Directive({ selector: &apo ...

Spread an all-encompassing category across a collection

What is the method in TypeScript to "spread" a generic type across a union? type Box<T> = { content: T }; type Boxes<string | number> = Box<string> | Box<number>; (Given that we are aware of when to use Boxes versus Box) ...

Async function is improperly updating the array state by overwriting it completely instead of just updating one item as

I am working on a file upload feature where each uploaded file should have a progress bar that updates as the file gets uploaded. I'm using a state to keep track of selected files and their respective progress: interface IFiles { file: File; c ...

Code error TS2345 occurs when assigning the argument of type '{ headers: HttpHeaders; }' to a parameter of type 'RequestOptionsArgs'. This indicates a mismatch in the type of data being passed, causing an

Upon running ionic serve, these are the results that I am encountering. My setup consists of Ionic4 version with Angular 8. While executing the command, this error appears: src/app/home/home.page.ts:60:77 - error TS2345: Argument of type '{ headers ...

What is the best way to convert a tuple containing key/value pairs into an object?

How can the function keyValueArrayToObject be rewritten in order to ensure that the type of keyValueObject is specifically {a: number; b: string}, instead of the current type which is {[k: string]: any}? const arrayOfKeyValue = [ {key: 'a', val ...

Is it advisable to include auto-generated files in an npm package upon publication?

I have a TypeScript NPM package where my build process compiles all *.ts files into myLib.d.ts, myLib.js, and myLib.js.map. In order for my NPM package to function properly, it requires the src/*.ts files as well as the auto-generated myLib.* files. Howe ...

Jasmine-ts is encountering a problem related to a subpath within a package, causing

Embarking on a journey of jasmine unit testing, I am encountering challenges when trying to run jasmine and locate my TypeScript written specs. Using jasmine-ts, I typically execute the following command: jasmine-ts --config=spec/support/jasmine.json The ...

The module X is experiencing a metadata version mismatch error. Version 4 was found instead of the expected version 3 while resolving symbol Y

I am in the process of creating an Angular 4 application using angular-cli (ng build) and incorporating ngx-clipboard. Recently, I have encountered a sudden error that has persisted for a few days despite there being no changes to my source code: ERROR in ...

Receiving NULL data from client side to server side in Angular 2 + Spring application

I'm currently working on a project that involves using Angular 2 on the client side and Spring on the server side. I need to send user input data from the client to the server and receive a response back. However, I'm encountering an issue where ...

I am looking to customize the mask input in my angular 7 application so that it allows either a "-" or a digit as the first character, with all subsequent characters being digits. How can I make this modification?

Within my Angular 7 application, I am working on a method that masks user input. Currently, the method restricts users from inputting anything other than digits. However, I need to modify it so that users can input either a "-" or a digit as the first char ...