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

相关文章

前端bug调试的方法技巧及常见错误

《前端bug调试的方法技巧及常见错误》:本文主要介绍编程中常见的报错和Bug,以及调试的重要性,调试的基本流程是通过缩小范围来定位问题,并给出了推测法、删除代码法、console调试和debugg... 目录调试基本流程调试方法排查bug的两大技巧如何看控制台报错前端常见错误取值调用报错资源引入错误解析错误

Vue中动态权限到按钮的完整实现方案详解

《Vue中动态权限到按钮的完整实现方案详解》这篇文章主要为大家详细介绍了Vue如何在现有方案的基础上加入对路由的增、删、改、查权限控制,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、数据库设计扩展1.1 修改路由表(routes)1.2 修改角色与路由权限表(role_routes)二、后端接口设计

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

Vue ElementUI中Upload组件批量上传的实现代码

《VueElementUI中Upload组件批量上传的实现代码》ElementUI中Upload组件批量上传通过获取upload组件的DOM、文件、上传地址和数据,封装uploadFiles方法,使... ElementUI中Upload组件如何批量上传首先就是upload组件 <el-upl

前端知识点之Javascript选择输入框confirm用法

《前端知识点之Javascript选择输入框confirm用法》:本文主要介绍JavaScript中的confirm方法的基本用法、功能特点、注意事项及常见用途,文中通过代码介绍的非常详细,对大家... 目录1. 基本用法2. 功能特点①阻塞行为:confirm 对话框会阻塞脚本的执行,直到用户作出选择。②

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

CSS3 最强二维布局系统之Grid 网格布局

《CSS3最强二维布局系统之Grid网格布局》CS3的Grid网格布局是目前最强的二维布局系统,可以同时对列和行进行处理,将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局,本文介... 深入学习 css3 目前最强大的布局系统 Grid 网格布局Grid 网格布局的基本认识Grid 网

HTML5中下拉框<select>标签的属性和样式详解

《HTML5中下拉框<select>标签的属性和样式详解》在HTML5中,下拉框(select标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中选择值的方式,本文将深入探讨select标签的... 在html5中,下拉框(<select>标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中

前端 CSS 动态设置样式::class、:style 等技巧(推荐)

《前端CSS动态设置样式::class、:style等技巧(推荐)》:本文主要介绍了Vue.js中动态绑定类名和内联样式的两种方法:对象语法和数组语法,通过对象语法,可以根据条件动态切换类名或样式;通过数组语法,可以同时绑定多个类名或样式,此外,还可以结合计算属性来生成复杂的类名或样式对象,详细内容请阅读本文,希望能对你有所帮助...

禁止HTML页面滚动的操作方法

《禁止HTML页面滚动的操作方法》:本文主要介绍了三种禁止HTML页面滚动的方法:通过CSS的overflow属性、使用JavaScript的滚动事件监听器以及使用CSS的position:fixed属性,每种方法都有其适用场景和优缺点,详细内容请阅读本文,希望能对你有所帮助... 在前端开发中,禁止htm