如何使用 Angular 2/4 开发搜索建议文本框

问题描述 投票:0回答:4

我是 Angular 新手。我如何开发 Angular 2/4 中的下拉建议搜索框。单击搜索按钮时,将使用下面的代码显示详细信息。但是我在尝试在文本框中输入时需要显示建议详细信息。与自动完成相同(在搜索期间,将显示“键入时”建议。)

组件.HTML

        <form role="form">
            <div class="row">
                <div class="form-group">
                    <div class="input-group">
                        <input name="search" 
                            class="form-control" 
                            type="text" 
                            placeholder="Search" 
                            required="" [(ngModel)]='searchcontent'>
                        <span class="input-group-btn">
                            <button 
                                class="btn btn-success ProductSearchBtn" 
                                    type="button" 
                                (click)='FetchItemDetailsSearch(searchcontent)'>
                                    <i class="glyphicon glyphicon-search" 
                                        aria-hidden="true"></i>
                                    <span style="margin-left:10px;">Search</span>
                            </button>
                        </span>
                    </div>
                </div>
            </div>
        </form>
    </div>
</section>
<section class="CommonsubWhiteBg" 
    style='height:850px;overflow-y:scroll; overflow-x:hidden' 
    (scroll)="onScroll($event)">
        <ng-container *ngFor="let item of itemdetails;">
            <article class="row" style="margin:0px;">
                <div class="col-md-12 col-sm-12 col-xs-12 EnquiryMore">
                    <a [routerLink]="['/productdetails',item.ItemID]">
                        <h5>{{item.ItemCode + ' - ' + item.ItemDescription}}</h5>
                    </a>
                </div>
            </article>
        </ng-container>
        <ng-container *ngIf="!itemdetails" style="margin:0px;">
            <article class="col-md-12">
                <h3>Loading data...</h3>
            </article>
        </ng-container>
        <ng-container *ngIf="itemdetails&&itemdetails.length==0" 
            class="ItemNotfoundArea row" style="margin:0px;">
            <article class="col-md-12">
                <h1>Sorry</h1>
                <p>Item Not Found</p>
            </article>
        </ng-container>
    </section>

component.ts 文件

FetchItemDetailsSearch(itemcodeordesc: string): void {
    this.pageIndex = 1;
    this.searchflag = 1;
    if (itemcodeordesc.length > 0)
        this.searchcontent = itemcodeordesc;
    else {
        itemcodeordesc = undefined
        this.searchcontent = itemcodeordesc;
    }
    this.prevScrollPosition = 0;        
    this._enqService
        .FetchItemDetailsSearch(this.searchcontent, this.pageIndex)
            .subscribe(itemsData => this.itemdetails = itemsData,
                error => {
                    console.error(error);
                    this.statusMessage = 
                        "Problem with the service.Please try again after sometime";
                }
            );
        }

**服务.ts **

FetchItemDetailsSearch(itemcodeordesc: string, pageIndex: number): 
  Observable<IEnquiryDetails[]> {
    return this._http.get('http://localhost:1234/api/enquirydetails/', 
        { params: 
            { itemcodeordesc: itemcodeordesc, 
              pageIndex: pageIndex } 
        })
    .map((response: Response) => <IEnquiryDetails[]>response.json())
    .catch(this.handleError)
}

条件:仅尝试使用 Angular 4/2 而不是 jQuery

asp.net angular angular2-template angular2-forms angular4-forms
4个回答
1
投票

看看角材料自动完成组件。

https://material.angular.io/components/autocomplete/examples

您还可以拥有自己的自定义过滤功能。下面的示例代码将为您提供实现相同目标的方法。 https://stackblitz.com/angular/krlrmgepmrv?file=app%2Fautocomplete-filter-example.ts

或 试试这个

http://www.angulartutorial.net/2017/03/simple-search-using-pipe-in-angular-2.html


1
投票

请检查此答案

   <form role="form">
            <div class="row">
                <div class="form-group">
                    <div class="input-group">
                        <input name="search" class="form-control" type="text" placeholder="Search" (keyup)="FetchItemDetailsSearch(searchcontent)" [(ngModel)]="searchcontent">
                        <span class="input-group-btn">
                            <button class="btn btn-success ProductSearchBtn" type="button" (click)='FetchItemDetailsSearch(searchcontent)'><i class="glyphicon glyphicon-search" aria-hidden="true"></i><span style="margin-left:10px;">Search</span></button>
                        </span>
                    </div>
                </div>
            </div>
        </form>

0
投票

您可以使用自动完成插件,例如this


0
投票

"## 标题 ## 受让人 ngFor="让filteredOptions 的选项| async" [value]="option"> {{option.name}} 从 '@angular/core' 导入 {Component, OnInit}; 从 '@angular/forms' 导入 {FormControl, FormsModule, ReactiveFormsModule}; 导入 {Observable } from 'rxjs'; 从 'rxjs/operators' 导入 {map, startWith}; 从 '@angular/common' 导入 {NgFor, AsyncPipe}; 从 '@angular/material/autocomplete' 导入 {MatAutocompleteModule}; 导入 {MatInputModule } from '@angular/material/input'; import {MatFormFieldModule} from '@angular/material/form-field'; 导出接口 User { name: string; } /* * @title 显示值自动完成 */ @Component( { 选择器:'autocomplete-display-example',templateUrl:'autocomplete-display-example.html',styleUrls:['autocomplete-display-example.css'],独立:true,导入:[ FormsModule,MatFormFieldModule,MatInputModule, MatAutocompleteModule、ReactiveFormsModule、NgFor、AsyncPipe、]、}) 导出类 AutocompleteDisplayExample 实现 OnInit { myControl = new FormControl(''); 选项: User[] = [{name: 'Mary'}, {name: 'Shelley'} , {名称: '伊戈尔'}];过滤选项:Observable; ngOnInit() { this.filteredOptions = this.myControl.valueChanges.pipe( startWith(''), map(value => { const name = typeof value === 'string' ? value : value?.name; return name ? this._filter(name as string) : this.options.slice(); }), ); } displayFn(user: User): string { return user && user.name ?用户名 : ''; } private _filter(name: string): User[] { const filterValue = name.toLowerCase(); return this.options.filter(option => option.name.toLowerCase().includes(filterValue)); } } .example-form { 最小宽度:150px;最大宽度:500px;宽度:100%; } .example-full-width { 宽度:100%; } “

© www.soinside.com 2019 - 2024. All rights reserved.