简体   繁体   English

使用 primeNg 和 Angular 10 将自动完成 select 值传递给父组件

[英]Passing an autocomplete select value to parent component with primeNg and Angular 10

I have a component name update-data and a child component named show-select.我有一个名为 update-data 的组件和一个名为 show-select 的子组件。 In the parent HTML file I do add the selector and try to bind event from child component.在父 HTML 文件中,我添加了选择器并尝试从子组件绑定事件。

<app-show-select (categorySelected)="onCategorySelected($event)"></app-show-select>

In the UpdateDataComponent file I just make a basic console.log:在 UpdateDataComponent 文件中,我只创建了一个基本的 console.log:

onCategorySelected(categorySelected: CategoriesModel): void {
  console.log('categorySelected: ', categorySelected);
}

My issue is coming from my child component: I do return a value from it but not the selected one.我的问题来自我的子组件:我确实从中返回了一个值,但没有返回选定的值。

export class ShowSelectComponent implements OnInit {
  categories: CategoriesModel[];
  filteredCategories: CategoriesModel[];
  category: CategoriesModel;
  @Output() categorySelected = new EventEmitter<CategoriesModel>();
  categoryId: number;

  constructor(private apiService: ApiService) { }

  ngOnInit(): void {
    this.getCategories();
  }

  private getCategories(): void {
    const response = this.apiService.getCategories().subscribe(
      (data) => {
        this.categories = data as CategoriesModel[];
      }
    );
    console.log('Get categories');
    console.log('response ', response);
  }

  filterCategories(event): void {
    this.filteredCategories = [];
    // tslint:disable-next-line:prefer-for-of
    for (let i = 0; i < this.categories.length; i++) {
      this.category = this.categories[i];
      if (this.category.categoryName.toLowerCase().indexOf(event.query.toLowerCase()) === 0) {
        console.log(this.category.categoryName);
        this.filteredCategories.push(this.category);
      }
    }
    this.categoryId = this.category?.id;
    this.categorySelected.emit(this.category);
  }
}

The sub component HTML is inspired of the official documentation:子组件 HTML 的灵感来自官方文档:

<p-autoComplete
  [(ngModel)]="category"
  [suggestions]="filteredCategories"
  field = "categoryName"
  (completeMethod)="filterCategories($event)"
  [size]="30"
  [minLength]="1" placeholder="Hint: type a letter"
  [dropdown]="true">
  <ng-template let-category pTemplate="category">
    <div class="ui-helper-clearfix" style="border-bottom:1px solid #D5D5D5">
      {{category.id}} {{category.categoryName}}
    </div>
  </ng-template>
</p-autoComplete>

My issue is that i pass the last entry of the categories list and not the one I selected.我的问题是我通过了类别列表的最后一个条目,而不是我选择的那个。 On the other hand, i do display the right content in this module when I add另一方面,当我添加时,我确实在这个模块中显示了正确的内容

<p> Category: {{category?.id||''}} {{category?.categoryName||'none'}} </p>

In the select component I display in the HTML page:在 select 组件中,我在 HTML 页面中显示:

Category: 2 Some value

In the console I display something totally different which match to the last entry of my json response and a choice that appears in the input when using Autocomplete with primeNg:在控制台中,我显示了完全不同的内容,这些内容与我的 json 响应的最后一个条目以及使用 PrimeNg 自动完成时出现在输入中的选项相匹配:

CategorySelected:  {id: "56", categoryName: "OTHER CATEGORY", docDescription: "No description"}

I finally solved partially the most important part concerning my issue:我终于部分解决了关于我的问题的最重要部分:

I modified the p-autocomplete with a click event:我用点击事件修改了 p-autocomplete:

<p-autoComplete
  [(ngModel)]="category"
  ...
  (click)="getCategoryDetails($event)"
  ...>
  <ng-template let-category pTemplate="category">
    <div class="ui-helper-clearfix" style="border-bottom:1px solid #D5D5D5">
      {{category.id}} {{category.categoryName}}
    </div>
  </ng-template>
</p-autoComplete>

I created this function:我创建了这个 function:

getCategoryDetails($event: MouseEvent): void {
  this.categoryId = this.category?.id;
  this.categorySelected.emit(this.category);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Primeng 自动完成组件的 Angular2 数据绑定 - Angular2 Data binding for Primeng autocomplete component Angular 10/Ionic 5 - 将输入数据从模态传递到父组件 - Angular 10/Ionic 5 - Passing input data from a modal to a parent component 在angular2中将父组件的值传递给ngForm的子组件? - Passing value from Parent component to child component for ngForm in angular2? Angular 从子组件传递一个值以禁用父组件中的按钮 - Angular passing a value from child component to disable button in parent component PrimeNG Angular 10 - 如何在 p-autoComplete 中添加图标 - PrimeNG Angular 10 - How to add an icon in p-autoComplete 如何从 Angular 的 PrimeNG 库中为 AutoComplete 组件设置焦点 - how to set focus for AutoComplete component from PrimeNG library in Angular Angular 10 值在自动完成中为空 - Angular 10 value is empty in autocomplete Angular 6:将 select 框值的选定值传递给 ngSubmit 上的组件 - Angular 6 : Passing selected value of select box value to the component on ngSubmit Primeng Autocomplete - 不使用 Angular 中的反应形式修补值 - Primeng Autocomplete - Not patching the value using reactive form in Angular Angular 10/Ionic 5 - 将输入数据从模态传递到父组件,然后提交到 firebase 数据库 - Angular 10/Ionic 5 - Passing input data from a modal to a parent component, then submitting to a firebase database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM