news 2026/5/13 9:57:49

“Test Type 组件选中 → 取消 → Apply Filter → 父组件接收”逻辑代码

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
“Test Type 组件选中 → 取消 → Apply Filter → 父组件接收”逻辑代码
开始 │ ▼ 用户打开下拉框 │ ▼ 用户输入搜索(可选)│ ▼ 过滤 Test Type 列表 │ ▼ 用户勾选某个 Test Type │ ├── 如果该项未被选中 → 添加到 selectedTestType │ └── 如果该项已被选中 → 从 selectedTestType 移除 │ ▼ 显示当前 selectedTestType(UI更新) │ ▼ 用户点击 Apply Filter 按钮 │ ▼ 组件执行 applyFilter()│ ▼ 通过 @Output()将 selectedTestType 发送给父组件 │ ▼ 父组件接收到数据,更新自身状态或调用 API │ ▼ 流程结束
+---------------------+|开始|+---------------------+|v+---------------------+|用户打开下拉框|+---------------------+|v+---------------------+|用户输入搜索(可选)|+---------------------+|v+---------------------+|过滤 Test Type 列表|+---------------------+|v+-----------------------------+|用户勾选或取消 Test Type|+-----------------------------+|v+-----------------------------+|更新 selectedTestType|+-----------------------------+|v+-----------------------------+|用户点击 Apply Filter|+-----------------------------+|v+-----------------------------+|applyFilter()→ @Output()|+-----------------------------+|v+-----------------------------+|父组件接收 selectedTestType|+-----------------------------+|v+---------------------+|结束|+---------------------+

ng g c location-test-type
src/app/location-test-type/ ├── location-test-type.component.ts ├── location-test-type.component.html ├── location-test-type.component.css └── location-test-type.component.spec.ts




1. 编写组件逻辑

location-test-type.component.ts

import{Component, OnInit, Output, EventEmitter}from'@angular/core';interface TestTypeItem{name: string;value: string;checked?: boolean;}@Component({selector:'app-location-test-type', templateUrl:'./location-test-type.component.html', styleUrls:['./location-test-type.component.css']})exportclass LocationTestTypeComponent implements OnInit{// 搜索输入 searchText: string='';// 全部 Test Type 列表 testTypeList: TestTypeItem[]=[{name:'test1', value:'test1'},{name:'test2', value:'test2'},{name:'test3', value:'test3'}];// 用户选中的 Test Type selectedTestType: TestTypeItem[]=[];// 搜索后的列表 filteredTestTypeList: TestTypeItem[]=[];// 把选中数据 emit 给父组件 @Output()selected=new EventEmitter<TestTypeItem[]>();ngOnInit(): void{this.filteredTestTypeList=[...this.testTypeList];}// 选中 / 取消 toggleTestType(item: TestTypeItem){item.checked=!item.checked;const index=this.selectedTestType.findIndex(x=>x.name===item.name);if(item.checked&&index===-1){this.selectedTestType.push(item);}elseif(!item.checked&&index>-1){this.selectedTestType.splice(index,1);}}// 搜索search(){const text=this.searchText.toLowerCase();this.filteredTestTypeList=this.testTypeList.filter(item=>item.name.toLowerCase().includes(text));}// 应用 FilterapplyFilter(){// Emit 给父组件 this.selected.emit(this.selectedTestType);}}

location-test-type.component.html

<divclass="dropdown"><inputtype="text"placeholder="Search.."[(ngModel)]="searchText"(input)="search()"/><div *ngFor="let item of filteredTestTypeList"class="option"><inputtype="checkbox"[checked]="item.checked"(change)="toggleTestType(item)"/><label>{{item.name}}</label></div><button(click)="applyFilter()">Apply Filter</button><divclass="selected-items"><span *ngFor="let item of selectedTestType">{{item.name}}</span></div></div>

location-test-type.component.css

.dropdown{border: 1px solid#ccc;padding: 10px;width: 200px;}.option{display: flex;align-items: center;}.selected-items{margin-top: 10px;font-weight: bold;}

父组件使用

假设父组件是 app.component.html:

<app-location-test-type(selected)="onLocationTestTypeSelected($event)"></app-location-test-type>

父组件 app.component.ts:

onLocationTestTypeSelected(selected: any[]){console.log('父组件收到选中数据:', selected);}

优点:

  • 逻辑完全独立,便于维护

  • 支持搜索、选中、取消和 Apply Filter

  • 数据通过 @Output() 回传父组件

  • 可直接扩展到多选下拉或 Location Test Type 场景

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/7 2:47:39

Graphcast:如何完成任务

原文&#xff1a;towardsdatascience.com/graphcast-how-to-get-things-done-f2fd5630c5fb?sourcecollection_archive---------0-----------------------#2024-01-29 本文介绍了如何使用谷歌最新的工具进行预测&#xff0c;从获取数据到格式化等等。 https://abhinavyesss.me…

作者头像 李华
网站建设 2026/5/3 10:12:58

39、超越传统界限:高频跟踪与金融套期保值技术揭秘

超越传统界限:高频跟踪与金融套期保值技术揭秘 1. 高频跟踪问题提出 在信号处理和控制领域,传统跟踪方法存在物理限制,难以有效应对一些干扰。然而,通过采用合适的模拟模型和多速率技术,有望在低采样率下实现(次)最优跟踪与干扰抑制,这为数字控制系统带来了新的范例。…

作者头像 李华
网站建设 2026/4/28 14:36:10

十字路口的抉择:B端与C端C++开发者的职业路径全解析

在C开发者的职业道路上&#xff0c;一个经典的选择题横亘在前&#xff1a;是深入服务企业与系统的B端&#xff08;Business&#xff09; 领域&#xff0c;还是投身于创造直接用户价值的C端&#xff08;Consumer&#xff09; 世界&#xff1f;这不仅是一个技术栈的选择&#xff…

作者头像 李华
网站建设 2026/5/12 23:04:39

8、脚本编程中的替代语法与循环结构

脚本编程中的替代语法与循环结构 1. 正则表达式脚本 在脚本编写中,使用正则表达式进行条件测试是一项很实用的技能。例如,我们可以处理美式英语和英式英语中“color”的不同拼写,即“color”和“colour”。以下是实现该功能的脚本代码: if [[ $REPLY =~ colou?r ]] ; …

作者头像 李华