news 2026/4/15 13:37:21

“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/4/15 12:17:18

基于STM32与机智云平台的智能台灯系统设计与实现

基于STM32与机智云平台的智能台灯系统设计与实现 摘要 本文设计并实现了一种基于STM32F103C8T6单片机与机智云平台的智能台灯系统。该系统整合了多维环境感知与智能照明控制功能,通过DHT11温湿度传感器(精度:温度2℃、湿度5%)、HC-SR04超声波传感器(测距范围2-400cm,精…

作者头像 李华
网站建设 2026/4/8 16:48:15

阻塞队列:三组核心方法全对比

深入解析阻塞队列&#xff1a;三组核心方法全对比与实战指南引言&#xff1a;为什么需要阻塞队列&#xff1f;在多线程编程中&#xff0c;线程间的数据共享和通信是一个常见而复杂的问题。传统的共享变量方式需要开发者手动处理线程同步、等待/通知机制&#xff0c;这既容易出错…

作者头像 李华
网站建设 2026/4/15 11:34:04

性能测试实战:混合场景与稳定性测试详解

性能测试实战&#xff1a;混合场景与稳定性测试详解一、性能测试场景实战回顾1. 单接口基准测试&#xff08;已完成&#xff09;目的&#xff1a;寻找单个接口的性能拐点&#xff08;最佳并发 & 最大TPS&#xff09;。策略&#xff1a;从小并发&#xff08;如10&#xff09…

作者头像 李华
网站建设 2026/4/13 20:18:06

37、高阶多智能体系统具有对抗交互和切换拓扑的二分共识研究

高阶多智能体系统具有对抗交互和切换拓扑的二分共识研究 1. 引言 在过去的10 - 15年里,多智能体系统和共识问题受到了广泛关注。多数研究假设智能体通过协作来达成共识,即它们为了共同目标交换信息。然而,在许多实际场景中,两个智能体可能将彼此视为对手,即便能获取对方…

作者头像 李华
网站建设 2026/4/13 15:28:05

10、函数与流编辑器的使用指南

函数与流编辑器的使用指南 在脚本编写的世界里,函数和流编辑器是两个强大的工具,它们能显著提升脚本的效率和可维护性。下面将详细介绍函数的使用以及流编辑器的相关内容。 函数的使用 1. 数组传递 在函数调用时,并非所有传递的值都是单个值,有时需要传递数组。以下是传…

作者头像 李华
网站建设 2026/3/20 14:46:02

交通信号仿真软件:Vistro_(2).交通信号控制基础理论

交通信号控制基础理论 1. 交通信号控制的基本概念 交通信号控制是指通过信号灯的红、黄、绿三种颜色的变化&#xff0c;来指导和管理交通流的运行。这种控制方式可以有效地减少交通拥堵&#xff0c;提高道路的通行能力&#xff0c;确保交通安全。在交通信号仿真软件中&#xff…

作者头像 李华