Retrieving user input from a personalized autocomplete cell editor

Struggling to create a custom autocomplete feature with ag grid. Unable to update the returned value with the selected item from the dropdown list.

In simpler terms:

I input 'A' => Receive a list of options => Select one => The input should display the chosen value => Save it => But I still see 'A'

Here is the cellEditorSelector code snippet :

    cellEditorSelector: (params) => {
      const values = this.articles.map((item) => {
        return {
          text: item.code,
          value: item.id,
        };
      });
      return {
        component: 'autoComplete',
        params: {
          values,
        },
      };
    },

The custom component looks like this:

  selector: 's1-ag-grid-auto-complete',
  template: `<div class="flex flex-col gap-1">
    <div class="relative">
      <input
        #input
        class="w-full text-xs rounded-md border-gray-300 shadow-sm pr-6"
        matInput
        (input)="onInputChange($event)"
        (change)="onChange($event)"
        [matAutocomplete]="auto"
        [value]="value"
        s1AutocompleteForceSelection
        type="text"
      />
    </div>
    <mat-autocomplete autoActiveFirstOption="true" #auto="matAutocomplete">
      <mat-divider></mat-divider>
      <mat-option *ngFor="let option of filteredList" [value]="option.value">
        <ng-container>
          <ng-container></ng-container>
        </ng-container>
        <span [title]="option.text" class="text-xs break-words">{{ option.text }} </span>
      </mat-option>
    </mat-autocomplete>
  </div>`,
})
export class AgGridAutoCompleteComponent implements ICellEditorAngularComp {
  public value: any;
  public filteredList;
  public options;
  private params: any;

  public agInit(params: any): void {
    this.params = params;
    this.value = params.value;
    this.options = params.values;
  }

  public onInputChange(event: Event): void {
    const inputValue = (event.target as HTMLInputElement).value;
    if (inputValue != null && inputValue != '') {
      const inputValueLowerCase = inputValue.toLowerCase();
      this.filteredList = (this.options || []).filter((element) => {
        return element.text.toLowerCase().includes(inputValueLowerCase);
      });
    } else {
      this.filteredList = [];
    }
  }

  public onChange($event) {
    this.value = $event.target.value;
  }

  public getValue(): any {
    return this.value;
  }
}

Seeking suggestions or solutions. Any ideas?

Answer №1

If you want to achieve this, here's how:

<mat-autocomplete #auto="matAutocomplete" (optionSelected)="input.value = $event.option.value">

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

Trouble arises when applying CSS to ng-x accordion styling

While working with ng-x accordion in Angular 2, I successfully rendered my accordion component. However, I encountered an issue when trying to add styles to the template provided by ng-x accordion. Despite using CSS in my rendered component for classes l ...

Exploring Vue 3: Crafting a custom plugin using the composition API and enhancing it with Typescript type augmentation

Encountering an issue with displaying plugins properly within <script> and <template> tags on WebStorm. Firstly, let's take a look at my files and configuration: tsconfig.config.json { "extends": "@vue/tsconfig/tsconfig. ...

Avoiding Any Errors When Using TypeScript Array Map with Objects

Currently in the process of upgrading my Angular project to version 13 within a new workspace. While transferring code, I encountered a typescript-eslint error that has me stumped. The previous working code looked like this: interface IConfigurationSettin ...

Tips for efficiently sorting a string array in JavaScript

Looking to sort an array in a specific numerical order? Check out this example: const files = ['30.png', '10.png', '1.jpeg', '2.jpeg', '12.gif', '4.png'] If you're running into issues when ...

Manipulating HTTP responses and variables in Angular 6

I created a custom application using the MEAN stack that allows users to sign up and log in. To enhance security, I want users to reconfirm their identity by entering their password or a PIN if they return to the application after some time. To achieve th ...

Passing a template to a component in Angular Elements: A step-by-step guide

After creating an Angular component that showcases an array of markers on a map and generates a popup when one of the markers is clicked, I encountered the need for users to define the content of this popup themselves. To tackle this challenge, I decided ...

Typescript - utilizing forward and useref functionality

I am working with two components: const ComponentOne = () => { const a = React.useRef<WhatTypeGoesHere>(null); // ^^^^^^^^^^ encountering error - ComponentTwo is treated as value instead of type here const fn = () => { ...

Encountering syntax errors with CommonJS Rollup plugin when attempting to import third-party libraries, particularly those involving the 'process' module

I have been developing a personalized rollup configuration that involves React projects and inlines the JS and CSS in index.html. When attempting to import third-party React libraries (such as material-ui-color), I encountered an issue with CommonJS repo ...

What is the best way to combine async/await with a custom Promise class implementation?

I've created a unique Promise class. How can I incorporate it with async/await? type Resolve<T> = (x: T | PromiseLike<T>) => void type Reject = (reason?: any) => void class CustomizedPromise<T> extends Promise<T> { ...

When trying to pass MaterialUI theme props to styled components, an error of 'Undefined' is encountered

I'm encountering difficulty accessing my Material-UI theme props within a styled component, resulting in errors like... TypeError: Cannot read property 'primary' of undefined or similar issues in the browser. Here is the custom theme I&apo ...

Creating a TypeScript type that supports a flexible number of generic parameters

I am currently working on creating an emit function that has the capability to accept multiple arguments. In addition, TypeScript will validate the 2nd argument and beyond based on the 1st argument (the event). The code provided below is a starting point, ...

Imitate a targeted ngxs store selection using ngxs

Within this component, there are two ngxs selectors being utilized: @Component({ selector: 'some-component', templateUrl: './some-component.html', styleUrls: ['./some-component.scss'], changeDetection: ChangeDetectionS ...

Ways to determine the current active tab in React are:

Currently, I am facing an issue with my code involving two tabs. Upon clicking on the second tab, I want to display a specific message. However, I am struggling to determine when the second tab is selected. The main problem lies in the fact that the selec ...

Guide to capturing upload requests with ng6-file-man

Currently, I am in the process of implementing file upload functionality for my application. To achieve this, I am utilizing the example provided by ng6-file-man-test, which leverages the ng6-file-man module to facilitate various file and folder manipulati ...

Is it possible to configure the JSONP module in Angular 2 to specifically interpret the MIME type as 'application/json'?

I am currently utilizing the JSONP module to call a SonarQube API. this.jsonp.get('https://sonarqube.com/api/projects/index') .subscribe(res => console.log(res)); In the past, I used the Http module of Angular2, however, this led to the ...

Offer Angular module functionality

I am currently working on an Angular 4 module that I need to import into a larger Angular 4 single-page application. The module consists of a single widget-like component that provides access to various functionalities within the module. The page and modu ...

Conceal pagination when printing - utilizing primeng table

In my application, I have a main component called Bill which includes a button for printing: <button class="btn btn-primary" type="button" (click)="printBill()"> Print </button> I also have a child component named BillProducts that contains a ...

Tips for creating a custom hook that is type safe:

When I use the custom function createUser, I've noticed that I can pass numbers instead of strings without receiving an error. Surprisingly, even if I forget to include an argument, no red squiggles appear. const [userState, createUser] = useCre ...

Exploring the capabilities of Angular2 and Jasmine through testing

I have been working on a basic spec for a component and I'm curious about the test's behavior. The test is designed to verify if a component is successfully created. It seems that when the test is executed, it first compiles and runs the Compone ...

What is the best method for invoking strophejs-plugin-chatstates through a service?

I recently incorporated the strophejs-plugin-chatstates into my one-to-one chat application to send the composing state of the user. The code includes a connection with the local Strophe. this.connection = new Strophe.Connection(`${EJABBERD_URL}/bosh`, ...