当前位置: 首页 > news >正文

“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 场景

http://www.cnnetsun.cn/news/74820.html

相关文章:

  • ComfyUI与Mosquitto MQTT代理集成:物联网场景适配
  • 7、脚本编程中的代码片段与替代语法技巧
  • 15、使用 AWK 总结日志
  • ComfyUI插件生态盘点:提升效率的必备扩展推荐
  • 程序员爆哭!我们让 COCO AI 接管 GitLab 审查后,团队直接起飞:连 CTO 都说“这玩意儿比人靠谱多了
  • 交通信号仿真软件:Synchro_(14).Synchro与其他软件的集成
  • 交通信号仿真软件:Vistro_(1).Vistro软件介绍
  • 交通信号仿真软件:Vistro_(4).交通网络建模
  • 微软将影响在线服务的第三方漏洞纳入奖励计划
  • 42、Linux 图形界面与邮件服务器配置全解析
  • 47、Linux系统安全防护全解析
  • 48、Linux系统安全:PAM、文件权限与网络防护
  • Blender贝塞尔曲线终极指南:用Bezier Utilities插件快速掌握曲线编辑技巧
  • 3步轻松制作Windows 11精简版:让老旧电脑焕发新生
  • SCS 59.单细胞空间转录组空间度量(SPATA2)
  • 【毕业设计】基于springboot高校体育运动会比赛系统运动项目、运动论坛(源码+文档+远程调试,全bao定制等)
  • 干货收藏:AI大模型进化史,从ChatGPT到智能体的三次关键跃迁
  • Docker Compose编排LLama-Factory多节点训练集群详细配置示例
  • Wan2.2-T2V-A14B模型部署指南:从VSCode配置C/C++环境说起
  • 计算机Java毕设实战-基于springboot公寓管理系统基于Springboot的公寓报修管理系统【完整源码+LW+部署说明+演示视频,全bao一条龙等】
  • 如何制作支持离线地图的GPS自行车码表:从硬件选型到功能实现的完整指南
  • 计算机Java毕设实战-基于springboot国风彩妆网站springboot国风彩妆化妆品网站电商销售商城系统【完整源码+LW+部署说明+演示视频,全bao一条龙等】
  • Honey Select 2 HF Patch技术架构深度解析与模块化部署指南
  • 【课程设计/毕业设计】基于springboot的自习室预订系统设计与实现基于springboot高校自习室预约系统的设计与实现【附源码、数据库、万字文档】
  • 大学计算机基础系列(合集)
  • 【课程设计/毕业设计】基于Web的高校大学生奖学金评定系统设计与实现基于springboot高校学生奖学金评定系统的设计与实现【附源码、数据库、万字文档】
  • 【爬虫框架-8】其他
  • Vue-next-admin终极指南:打造现代化后台管理系统的完整解决方案
  • vgmstream终极指南:游戏音频转换与播放全攻略
  • vgmstream音频解码神器:游戏音频格式转换终极指南