如何使用Angular通过Selectbox过滤表

发布于 2025-01-22 14:11:02 字数 773 浏览 0 评论 0原文

给定一个自定义表组件以及自定义选择框组件,我希望能够通过从选择框中选择一个选项来过滤表行。

我的想法是首先将点击列表添加到我的选择框的选项中,因此,在单击选项时,将执行onoptionClicked()方法。在此onoptionClicked()方法中,我将发射options删除事件:

  @Output() optionSelected = new EventEmitter<string>();

  onOptionClicked(option: string) {
    this.optionSelected.emit(option);
  }

我的表组件将在事件options opockelected

<div class="tbl" (optionSelected)="filterTableRows($event)">
  <table>
    <thead>
...

:但是,从根本上讲,这个想法是错误的,但是:

  1. 我的IDE显示出一个错误,说事件选项未由任何适用指令或Div Element

    发出。

  2. 该表在选项置换事件上没有反应。

似乎问题是我的表组件和我的Select-box组件是不同的组件,显然它们无法通过事件发射器进行通信。有人可以帮我吗?

Given a custom table component as well as a custom select box component, I want to be able to filter the table rows by selecting an option from the select box.

My idea was to first add a click-listener to the options of my select box, so when clicking on an option, the onOptionClicked() method will be executed. Inside this onOptionClicked() method, I will emit an optionSelected event:

  @Output() optionSelected = new EventEmitter<string>();

  onOptionClicked(option: string) {
    this.optionSelected.emit(option);
  }

My table component will react upon the event optionSelected:

<div class="tbl" (optionSelected)="filterTableRows($event)">
  <table>
    <thead>
...

There seems to be something fundamentally wrong with this idea, however:

  1. My IDE shows an error saying Event optionSelected is not emitted by any applicable directives nor by div element.

  2. The table does not react on the optionSelected event.

It seems the issue is that my table component and my select-box component are different components, apparently they cannot communicate via event emitters. Can someone help me out, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别在捏我脸啦 2025-01-29 14:11:02

我认为使用@output() decorator < /a>。这是使用此组件来向其他组件发出信号的东西。您似乎在同一组件中使用它。

而是使用a replasesUbject 保留您的过滤器:

optionSelected$ = new ReplaySubject<string>('');

onOptionClicked(option: string) {
  this.optionSelected$.next(option);
}

要进行数据过滤,您可以订阅您到重播主题:

this.optionSelected$.subscribe(filter => {
  this.yourFilteredData = this.yourData.filter(item => { 
   // <-- you filter code
  });
})

注意:请记住,当组件被销毁时,请记住完成任何订阅,以避免常见内存泄漏

I think there is a misunderstanding on the use of the @Output() decorator. This is something to be used to emit signals to other components using this one. You seem to be using it in the same component.

Instead, use a ReplaySubject to retain your filter:

optionSelected$ = new ReplaySubject<string>('');

onOptionClicked(option: string) {
  this.optionSelected$.next(option);
}

To do the data filtering you could subscribe to the replay subject:

this.optionSelected$.subscribe(filter => {
  this.yourFilteredData = this.yourData.filter(item => { 
   // <-- you filter code
  });
})

Note: please remember to complete any subscriptions when the component is destroyed to avoid the common memory leak.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文