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

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>

http://www.lryc.cn/news/216962.html

相关文章:

  • 『亚马逊云科技产品测评』活动征文|搭建基础运维环境
  • 双指针扫描
  • uniapp小程序九宫格抽奖
  • mysql树状结构查询及注意事项
  • TimeGPT-1——第一个时间序列数据领域的大模型他来了
  • 通过Google搜索广告传送的携带木马的PyCharm软件版本
  • 网站文章收录因素,别人复制文章排名比你原创的好?
  • C#开源的一个能利用Windows通知栏背单词的软件 - ToastFish
  • 速拿offer,超全自动化测试面试题+答案汇总,背完还怕拿不到offer?
  • LeetCode----1415. 长度为 n 的开心字符串中字典序第 k 小的字符串
  • 2310C++协程超传服务器
  • 【排序算法】 计数排序(非比较排序)详解!了解哈希思想!
  • 20231103配置cv180zb的编译环境【填坑篇】
  • 足底筋膜炎如何治疗
  • rabbitMq路由键介绍
  • 【python基础】python切片—如何理解[-1:],[:-1],[::-1]的用法
  • 剑指JUC原理-9.Java无锁模型
  • 汽车托运使用的场景
  • 机器学习 - 加油站数据分析
  • 基于CMFB余弦调制滤波器组的频谱响应matlab仿真
  • helm一键部署grafana
  • pytorch复现_NMS
  • 备份doris数据到minio
  • Linux中正则表达式等
  • 记一次并发问题 Synchronized 失效
  • 手机平板摄像头如何给电脑用来开视频会议
  • windows docker desktop 更换镜像 加速
  • linux下多机器ssh免密码登录配置
  • 【IDEA使用maven package时,出现依赖不存在以及无法从仓库获取本地依赖的问题】
  • Flink 统计接入的数据量-滚动窗口和状态的使用