Angular material Chips Autocomplete

2023-11-04 05:30

本文主要是介绍Angular material Chips Autocomplete,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Chips Autocomplete 官网的例子我没法正常使用,无法实现搜索

我的select是个通用组件,现在贴代码:
component.ts
 

import {Component,ElementRef,forwardRef,Input,OnChanges,OnDestroy,OnInit,SimpleChanges,ViewChild,
} from '@angular/core';
import { Tag } from '../../models/tag/tag';
import { map, startWith, takeUntil } from 'rxjs/operators';
import { ControlValueAccessor, UntypedFormControl, NG_VALUE_ACCESSOR } from '@angular/forms';
import { COMMA, ENTER } from '@angular/cdk/keycodes';
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
// import { MatChipInputEvent } from '@angular/material/chips';
import { TagService } from '../../services/tag/tag.service';
import { TagType } from '../../enums/TagType';
import { ISearchOptions } from '../../interfaces/SearchOptions';
import { SubscriberWrapperComponent } from '../subscriber-wrapper/subscriber-wrapper.component';@Component({selector: 'app-tags-select',templateUrl: './tags-select.component.html',styleUrls: ['./tags-select.component.scss'],providers: [{provide: NG_VALUE_ACCESSOR,useExisting: forwardRef(() => TagsSelectComponent),multi: true,},],
})
export class TagsSelectComponentextends SubscriberWrapperComponentimplements ControlValueAccessor, OnChanges, OnInit, OnDestroy
{@Input() title: string;@Input() disabled: boolean;@Input() color: 'primary' | 'accent' | 'warn';@Input() type: TagType;@Input() tags: Tag[];filteredTags: Tag[];selectedTags: Tag[] = [];separatorKeysCodes: number[] = [ENTER, COMMA];tagCtrl = new UntypedFormControl('');hideComponent: boolean;@ViewChild('tagInput') tagInput: ElementRef<HTMLInputElement>;@ViewChild('chipList') chipList: ElementRef;constructor(private tagService: TagService) {super();}onChange = (_: any) => {};onTouched = () => {};async ngOnInit() {this.initTags();const conditions = [];if (this.type) {conditions.push({key: 'type',value: this.type,operator: '=',});}this.tags = await this.listTags('', {conditions,});await this.updateFilterTags();}async listTags(query: string = '', options: ISearchOptions = {}) {if (!this.tags) {const response = await this.tagService.list(query, options);return response.entities;} else {return this.tags;}}// add(event: MatChipInputEvent): void {//   event.chipInput!.clear();//   this.tagCtrl.setValue(null);// }remove(tag: Tag): void {const index = this.selectedTags.indexOf(tag);if (index >= 0) {this.selectedTags.splice(index, 1);}this.filteredTags.push(tag);// this.onChange(this.selectedTags);  }selected(event: MatAutocompleteSelectedEvent): void {this.selectedTags.push(event.option.value);this.tagInput.nativeElement.value = '';this.tagCtrl.setValue(null);// this.onChange(this.selectedTags);}registerOnChange(fn: any): void {this.onChange = fn;}registerOnTouched(fn: any): void {this.onTouched = fn;}writeValue(selectedTags: Tag[] = []): void {this.selectedTags = selectedTags ?? [];this.updateFilterTags();}ngOnChanges(changes: SimpleChanges): void {if (changes.disabled?.currentValue) {this.tagCtrl.disable();}if (changes.tags?.currentValue) {this.updateFilterTags();}}initTags() {this.tagCtrl.valueChanges.pipe(takeUntil(this.unsubscribeAll),startWith(null),map(async (value?: string) => {await this.updateFilterTags(value);})).subscribe();}async updateFilterTags(value?: string) {const conditions = [];if (this.type) {conditions.push({key: 'type',value: this.type,operator: '=',});}const response = value? this.tags?.filter((tag: Tag) => {const regex = new RegExp(`^${value}`, 'i'); return regex.test(tag.title?tag.title:'');}) || []: this.tags?.slice() || [];const selectedTagIds = this.selectedTags.map((tag: Tag) => tag.id);this.filteredTags = response.filter((tag: Tag) => !selectedTagIds.includes(tag.id));}
}

页面.html
 

<mat-label>{{title}}</mat-label>
<mat-form-field class="tag-chip-list" *ngIf="!hideComponent"><mat-chip-list #chipList aria-label="Tag selection" [disabled]="disabled"><mat-chip [color]="color" [class]="color"*ngFor="let tag of selectedTags; let i=index"(removed)="remove(tag)"><button matChipRemove><mat-icon svgIcon="CloseBlue" class="close-icon"></mat-icon></button>{{tag.name}}</mat-chip><inputplaceholder="Add another tag..."#tagInput[formControl]="tagCtrl"[matAutocomplete]="auto"[matChipInputFor]="chipList"[matChipInputSeparatorKeyCodes]="separatorKeysCodes"></mat-chip-list><mat-autocomplete #auto="matAutocomplete"(optionSelected)="selected($event)"><mat-option *ngFor="let tag of filteredTags" [value]="tag"[innerHTML]="tag.name"></mat-option></mat-autocomplete>
</mat-form-field>

这篇关于Angular material Chips Autocomplete的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/343558

相关文章

使用vscode搭建pywebview集成vue项目实践

《使用vscode搭建pywebview集成vue项目实践》:本文主要介绍使用vscode搭建pywebview集成vue项目实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录环境准备项目源码下载项目说明调试与生成可执行文件核心代码说明总结本节我们使用pythonpywebv

使用Python和Tkinter实现html标签去除工具

《使用Python和Tkinter实现html标签去除工具》本文介绍用Python和Tkinter开发的HTML标签去除工具,支持去除HTML标签、转义实体并输出纯文本,提供图形界面操作及复制功能,需... 目录html 标签去除工具功能介绍创作过程1. 技术选型2. 核心实现逻辑3. 用户体验增强如何运行

CSS 样式表的四种应用方式及css注释的应用小结

《CSS样式表的四种应用方式及css注释的应用小结》:本文主要介绍了CSS样式表的四种应用方式及css注释的应用小结,本文通过实例代码给大家介绍的非常详细,详细内容请阅读本文,希望能对你有所帮助... 一、外部 css(推荐方式)定义:将 CSS 代码保存为独立的 .css 文件,通过 <link> 标签

使用Vue-ECharts实现数据可视化图表功能

《使用Vue-ECharts实现数据可视化图表功能》在前端开发中,经常会遇到需要展示数据可视化的需求,比如柱状图、折线图、饼图等,这类需求不仅要求我们准确地将数据呈现出来,还需要兼顾美观与交互体验,所... 目录前言为什么选择 vue-ECharts?1. 基于 ECharts,功能强大2. 更符合 Vue

Vue中插槽slot的使用示例详解

《Vue中插槽slot的使用示例详解》:本文主要介绍Vue中插槽slot的使用示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、插槽是什么二、插槽分类2.1 匿名插槽2.2 具名插槽2.3 作用域插槽三、插槽的基本使用3.1 匿名插槽

springboot+vue项目怎么解决跨域问题详解

《springboot+vue项目怎么解决跨域问题详解》:本文主要介绍springboot+vue项目怎么解决跨域问题的相关资料,包括前端代理、后端全局配置CORS、注解配置和Nginx反向代理,... 目录1. 前端代理(开发环境推荐)2. 后端全局配置 CORS(生产环境推荐)3. 后端注解配置(按接口

Vue 2 项目中配置 Tailwind CSS 和 Font Awesome 的最佳实践举例

《Vue2项目中配置TailwindCSS和FontAwesome的最佳实践举例》:本文主要介绍Vue2项目中配置TailwindCSS和FontAwesome的最... 目录vue 2 项目中配置 Tailwind css 和 Font Awesome 的最佳实践一、Tailwind CSS 配置1. 安

CSS3 布局样式及其应用举例

《CSS3布局样式及其应用举例》CSS3的布局特性为前端开发者提供了无限可能,无论是Flexbox的一维布局还是Grid的二维布局,它们都能够帮助开发者以更清晰、简洁的方式实现复杂的网页布局,本文给... 目录深入探讨 css3 布局样式及其应用引言一、CSS布局的历史与发展1.1 早期布局的局限性1.2

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

CSS引入方式和选择符的讲解和运用小结

《CSS引入方式和选择符的讲解和运用小结》CSS即层叠样式表,是一种用于描述网页文档(如HTML或XML)外观和格式的样式表语言,它主要用于将网页内容的呈现(外观)和结构(内容)分离,从而实现... 目录一、前言二、css 是什么三、CSS 引入方式1、行内样式2、内部样式表3、链入外部样式表四、CSS 选