news 2026/5/10 17:04:12

“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/5 17:38:34

大数据预测分析:提升供应链管理效率

大数据预测分析在供应链管理中的应用&#xff1a;从理论到实践的效率提升框架 元数据框架 标题 大数据预测分析在供应链管理中的应用&#xff1a;从理论到实践的效率提升框架 关键词 大数据预测、供应链管理、需求预测、库存优化、机器学习、因果推断、智能决策 摘要 在全球化与…

作者头像 李华
网站建设 2026/5/9 3:14:50

32、化学网络中的精确矩动力学计算示例解析

化学网络中的精确矩动力学计算示例解析 1. 简单非线性示例 考虑一个包含三种物质的前馈系统,其中物质 (S_1) 催化 (S_2) 的生成,并且 (S_1) 和 (S_2) 共同作用生成 (S_3)。具体反应如下: - (0 \xrightarrow{\kappa_1} S_1 \xrightarrow{\delta_1} 0) - (S_1 \xrightarrow…

作者头像 李华
网站建设 2026/5/5 17:38:35

34、分布式控制器设计与机器学习图像分析方法

分布式控制器设计与机器学习图像分析方法 1. 分布式控制器设计理论 1.1 分布式梯度与目标函数 在多智能体系统中,连续可微函数 (V : R^{nd} \to R^+) 在图 (G) 上具有分布式梯度的充要条件是 (V(x)) 在 (G) 上是团分解的。这表明所有具有分布式梯度的目标函数都具有特定形式…

作者头像 李华
网站建设 2026/5/5 11:56:23

18、工业人机物理系统集成的数字化与控制评估

工业人机物理系统集成的数字化与控制评估 1. 自下而上评估阶段概述 在自上而下设计阶段结束后,自下而上阶段开始对设计好的人机工业物理系统(HICPS)进行评估。“工程”方法常被错误地等同于设计阶段的“实施”部分,即自下而上的评估阶段,此阶段大多是“技术性”的,当工…

作者头像 李华
网站建设 2026/5/10 6:10:35

45、反垃圾邮件措施全解析

反垃圾邮件措施全解析 1. 垃圾邮件问题概述 在计算机领域,垃圾邮件指的是那些无用的电子邮件,比如可疑的防脱发疗法广告、非法的金字塔骗局,以及用你不懂的语言编写的神秘信息等。对于电子邮件管理员来说,垃圾邮件是一个严重的问题,它主要涉及两个方面:一是防止系统被用…

作者头像 李华
网站建设 2026/5/8 0:01:39

泉盛UV-K5固件升级终极指南:LOSEHU固件5分钟快速上手

想让你的泉盛UV-K5/K6对讲机从"能用"升级到"好用"吗&#xff1f;LOSEHU固件正是你需要的魔法钥匙&#xff01;这款开源固件为原厂设备注入了全新活力&#xff0c;让业余无线电爱好者也能享受专业级功能。今天&#xff0c;我将带你快速解锁这款固件的全部潜…

作者头像 李华