首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从自定义的自动完成单元格编辑器中获取输入值

从自定义的自动完成单元格编辑器中获取输入值
EN

Stack Overflow用户
提问于 2022-01-13 05:50:56
回答 1查看 418关注 0票数 1

我正在尝试使用ag网格来定制一个自动完成组件。我无法用下拉列表中选定的值更新返回的值

换言之:

我输入'A‘=>,我得到一个条目列表,我选择一个=>,输入被更新为项目值=>,保存=>,我得到'A’

cellEditorSelector:

代码语言:javascript
复制
    cellEditorSelector: (params) => {
      const values = this.articles.map((item) => {
        return {
          text: item.code,
          value: item.id,
        };
      });
      return {
        component: 'autoComplete',
        params: {
          values,
        },
      };
    },

自定义组件:

代码语言:javascript
复制
  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;
  }
}

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-13 11:00:48

你可以这样做:

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70692005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档