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/10 11:52:30

AXI-A7.4.6 Atomic transaction signaling

AWATOP(Atomic Operation Type)是一个6位的AXI信号,专为支持原子操作而引入。它出现在写地址通道(AW)上,用于指示当前事务的原子操作类型和字节序信息。其编码规则如下: 默认值 0x00(0b000000):表示非原子操作,即普通写事务。 AtomicStore(0b01exxx):原子存储类…

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

交通信号仿真软件:Vistro_(5).交通流仿真设置

交通流仿真设置 在交通信号仿真软件中&#xff0c;交通流仿真是一个核心模块&#xff0c;它涉及到如何模拟车辆在交通网络中的行驶行为。本节将详细介绍如何在仿真软件中设置交通流仿真&#xff0c;包括仿真参数的配置、车辆生成、路径规划、交通流量控制等方面的内容。 1. 仿真…

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

全面解析OpenFace:5大核心技术实现实时面部行为分析

全面解析OpenFace&#xff1a;5大核心技术实现实时面部行为分析 【免费下载链接】OpenFace OpenFace – a state-of-the art tool intended for facial landmark detection, head pose estimation, facial action unit recognition, and eye-gaze estimation. 项目地址: http…

作者头像 李华
网站建设 2026/5/8 2:28:09

Java计算机毕设之基于java+springboot博客管理系统设计和实现基于springboot个人博客系统的设计与实现(完整前后端代码+说明文档+LW,调试定制等)

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华
网站建设 2026/5/11 2:30:57

Java计算机毕设之基于springboot大学生心理健康分析及干预平台基于springboot高校心理健康评测与服务系统(完整前后端代码+说明文档+LW,调试定制等)

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华