Angular筛选表单
创始人
2024-10-29 09:30:41
0

下面是一个示例,展示了如何使用Angular进行筛选表单:


  • {{ product.name }} - {{ product.category }}
// 在组件的类中
import { Component } from '@angular/core';

@Component({
  selector: 'app-products',
  template: `
    
  `,
  styleUrls: ['./products.component.css']
})
export class ProductsComponent {
  products: any[] = [
    { name: 'iPhone', category: '电子产品' },
    { name: 'Sofa', category: '家具' },
    { name: 'T-shirt', category: '服装' },
    // 其他产品
  ];
  filteredProducts: any[] = [];
  filter: any = {};

  filterProducts() {
    this.filteredProducts = this.products.filter((product) => {
      let nameMatch = true;
      let categoryMatch = true;

      if (this.filter.name) {
        nameMatch = product.name.toLowerCase().includes(this.filter.name.toLowerCase());
      }

      if (this.filter.category) {
        categoryMatch = product.category === this.filter.category;
      }

      return nameMatch && categoryMatch;
    });
  }
}

在上面的示例中,我们首先在模板中创建了一个表单,包含一个输入框和一个下拉菜单,用于输入筛选条件。然后,我们使用ngModel指令将输入框和下拉菜单的值与组件中的filter对象进行绑定。当用户点击"筛选"按钮时,会调用filterProducts方法。

filterProducts方法使用Array.filter函数对产品数组进行筛选。根据filter对象中的值进行匹配,如果产品名称中包含输入的名称,并且产品类别与选择的类别匹配,就会被保留在filteredProducts数组中。最后,我们使用*ngFor指令在页面上循环渲染筛选后的产品列表。

注意:以上示例仅供参考,实际应用中,您可能需要根据自己的需求进行相应的修改。

相关内容

热门资讯

透视代打!wepoker代打辅... 透视代打!wepoker代打辅助,wepoker有什么规律(详细插件下载)1、让任何用户在无需wep...
透视工具!aapoker ai... 透视工具!aapoker ai插件(透视)aa poker透视软件,必胜教程(有挂解密)该软件可以轻...
透视真的!wepoker透视破... 透视真的!wepoker透视破解版,wepoker到底有没有透视(详细开辅助能查到);1)wepok...
透视讲解!aapoker ai... 透视讲解!aapoker ai插件(透视)aapoker怎么设置提高好牌几率,wpk教程(有挂方法)...
透视好友!wepokerplu... 透视好友!wepokerplus外挂,wepoker辅助器是真的吗(详细辅助插件功能)1、该软件可以...
透视透视!aapoker怎么控... 透视透视!aapoker怎么控制牌(透视)aapoker辅助插件工具,爆料教程(有挂详情);1、完成...
透视好牌!wepoker提高好... 透视好牌!wepoker提高好牌率,wepoker是不是有人用挂(详细破解辅助插件);1、wepok...
透视了解!aa poker透视... 透视了解!aa poker透视软件(透视)aapoker免费透视脚本,总结教程(有挂解密)aapok...
透视挂透视!wepoker怎么... 透视挂透视!wepoker怎么挂底牌,wepoker轻量版透视系统(详细辅助器有哪些功能)1、起透看...
透视软件!aa poker透视... 透视软件!aa poker透视软件(透视)aapoker辅助怎么用,规律教程(有挂教程)1、超多福利...