简体   繁体   English

使用Angular与 <input> 标签

[英]Multiple select Dropdown using Angular with <input> tag

I am building up angular 6 application, in which i am in the need to make a multi select dropdown using <input> text box without using <select> . 我正在构建angular 6应用程序,其中需要使用<input>文本框而不使用<select>进行多选下拉列表。

Html : HTML

<form>
  Select User: <input type="text" name="user" [value]="users"><br>
  <button type="submit"> Submit </button>
</form>

Ts : Ts

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
    users = ['First User', 'Second User', 'Third User', 'Fourth User'];
}

I also need to do it using Pure Javascript, Tpescript , and also Without third party or jquery . 我还需要使用Pure Javascript,Tpescript以及不使用第三方或jquery来完成此操作

Also recommended not to use html datalist . 还建议不要使用html 数据列表

I have a lot of search for it but couldn't find a proper solution for it. 我对此进行了大量搜索,但找不到合适的解决方案。 Kindly help me to achieve the result. 请帮助我达到目标。

Stackblitz : https://stackblitz.com/edit/angular-v7kmbq Stackblitzhttps : //stackblitz.com/edit/angular-v7kmbq

Check this StackBlitz: Dropdown Example 检查此StackBlitz: 下拉示例

HTML file: HTML档案:

<button type="button" (click)="clear()">Clear</button>

<div class="autocomplete">
    <input name="suggestion" type="text" placeholder="User" (click)="suggest()" [formControl]="typeahead">

    <div class="autocomplete-items" *ngIf="show">
      <div *ngFor="let s of suggestions" (click)="selectSuggestion(s)">{{ s }}</div>
    </div>
</div>

TS file: TS文件:

import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';

  suggestions: string [] = [];
  suggestion: string;
  show: boolean = false;
  typeahead: FormControl = new FormControl();

  fieldHistory: string [] = [];

  suggest() {
    this.suggestions = this.users;
    this.show = true;
  }

  selectSuggestion(s) {
      this.suggestion = "";

      var index = this.fieldHistory.findIndex(function(element) {
          return element === s;
      });

      if (index === -1) {
          this.fieldHistory.push(s);
      } else {
          var firstPart = this.fieldHistory.slice(0, index);
          var secondPart = this.fieldHistory.slice(++index);

          this.fieldHistory = firstPart.concat(secondPart);
      }

      for (let i = 0; i < this.fieldHistory.length; i++) 
           this.suggestion = this.suggestion + " " + this.fieldHistory[i];

      this.typeahead.patchValue(this.suggestion);
      this.show = false;
  }

  clear() {
      this.suggestion = "";
      this.fieldHistory.pop();

      for (let item of this.fieldHistory) 
          this.suggestion = this.suggestion + " " + item;

      this.typeahead.patchValue(this.suggestion);
  }

  users = ['First User', 'Second User', 'Third User', 'Fourth User'];
}

CSS file: CSS文件:

.autocomplete {
    position: relative;
    width: 100%;
}

.autocomplete-items {
    position: absolute;
    border: 1px solid #d4d4d4;
    border-radius: 4px;
    margin-top: 15px;
    top: 100%;
    left: 0;
    right: 0;
}

.autocomplete-items div {
    padding: 10px;
    cursor: pointer;
    background-color: #fff;
    border-bottom: 1px solid #d4d4d4;
}

.autocomplete-items div:hover {
    background-color: #e9e9e9; 
}

Also import the module: import { ReactiveFormsModule } from '@angular/forms'; 还要导入模块: import { ReactiveFormsModule } from '@angular/forms';

Here is the working code, I used [(ngModel)] instead formcontrols: 这是工作代码,我使用了[[ngModel)]而不是formcontrols:

https://stackblitz.com/edit/angular-qjrhs5?file=src%2Fapp%2Fapp.component.css https://stackblitz.com/edit/angular-qjrhs5?file=src%2Fapp%2Fapp.component.css

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM