日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Angular 下拉搜索框实现

發布時間:2023/12/10 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Angular 下拉搜索框实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天有個需求要寫一個下拉搜索框,本來是下拉框的,由于內容太多,所以添加一個查找功能。根據博客進行改寫的。參考了他的基本框架進行實現。

效果圖:




輸入框右邊的白色箭頭圖片下載地址

<td>xxxxxxx</td> <td style="text-align:left"><div style="background:#2c4b76;padding:0px"><div class="btn-group bootstrap-select fit-width" #menu1><input name="optionsToDisplay" id="optionsToDisplay" type="text" class="form-cotnrol" style="height: 23px;width: 237px" #optionsToDisplayClass="ngModel" (input)="onFilter($event,menu)" data-toggle="dropdown" [ngModel]="selectValue?.name" autocomplete="off"/><img src="assets/images/bottomangle.png" style="z-index: 1;position: fixed;margin-left:0px;border:1px;border-color:#767676" data-toggle="dropdown"><div class="dropdown-menu open" style="width: 256px"><ul class="dropdown-menu inner"><li *ngIf="measureTypesToShow==null || measureTypesToShow.length==0"><a>沒有內容</a></li><li *ngFor="let item1 of measureTypesToShow,let i=index"><a #a1 id="{{item1.id}}" (click)="activeValueToType(menu1,a1,$event)">{{item1.name}}</a></li></ul></div></div></div></td> //過濾字符,進行菜單的搜索onFilterToType(event,e: HTMLElement): void {if (!e.classList.contains("open")) {e.classList.add("open");}if(this.measureTypesToShow && this.measureTypesToShow.length){this.measureTypesToShow.length = 0;}//對輸入的字符進行非空限制let inputValue = event.target.value;this.selectValueToType.name = inputValue;// this.setMersureTypeID();if (inputValue && inputValue.length) {this.filterValueToType = inputValue;this.measureTypesToShow = this.activateFilterToType();} else {this.filterValueToType = null;this.measureTypesToShow = this.measureTypes.concat();}this.doSelectValueToType();}

模糊查找

activateFilterToType() {if (this.measureTypes && this.measureTypes.length) {let temp:data[] = [];for (let measureTypes1 of this.measureTypes) {if (measureTypes1.name.toUpperCase().includes(this.filterValueToType.toUpperCase())){temp.push(measureTypes1);}}return temp;}}

用來做校驗的,判斷如果直接輸入后并沒有點擊下拉菜單的內容,會導致賦值失敗,所以需要判斷一下是否沒點擊

public doSelectValueToType(){let flag = true;if (this.measureTypes.map(item=>{return item.name;}).includes(this.selectValueToType.name)){flag = false;}if (flag) {this.errorMessage3 = "請選擇正確的xxxxxxxxx!";} else {this.errorMessage3 = "";}} //選中菜單時activeValueToType(e: HTMLElement, a: HTMLElement, event: Event) {// console.log(a.innerText);if (a.id == null || a.id == "") {this.selectValueToType.name = a.innerText;this.selectValueToType.id = a.id;this.select.emit(this.selectValueToType);return;}this.selectValueToType.name = a.innerText;this.selectValueToType.id = a.id;this.setMersureTypeID();if (e.classList.contains("open")) {e.classList.remove("open");} else {e.classList.add("open");}event.stopPropagation();this.select.emit(this.measureType);this.doSelectValueToType();} setMersureTypeID() {this.info.measureTypeID = this.selectValueToType.id;this.info.type = this.selectValueToType.name;}

同時需要加上css

.select input.key {width: 100%;height: 30px;border: 1px solid #ddd;margin: 0;background-position: 95% center;}.select .content {display: block;width: 80%;/*max-height: 200px;*/border-left: 1px solid #ddd;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd;}.select .content p {border-top: 1px solid #ddd;margin: 0px;padding: 5px;background-color: #D2D2D2;}.select .content .search {background-color: #fff;}.select .search > input {width: 98%;height: 25px;border-radius: 5px;border: 1px solid #ddd;background-position: 95% center;}.select .content .option {overflow-y: auto;max-height: 100px;}/*!* Bootstrap-select v1.12.2 (http://silviomoreto.github.io/bootstrap-select)** Copyright 2013-2017 bootstrap-select* Licensed under MIT (https://github.com/silviomoreto/bootstrap-select/blob/master/LICENSE)*/.bootstrap-select {width: 220px \0;/*IE9 and below*/}.bootstrap-select > .dropdown-toggle {width: 100%;padding-right: 25px;z-index: 1;}.bootstrap-select > .dropdown-toggle.bs-placeholder,.bootstrap-select > .dropdown-toggle.bs-placeholder:hover,.bootstrap-select > .dropdown-toggle.bs-placeholder:focus,.bootstrap-select > .dropdown-toggle.bs-placeholder:active {color: #999;}.bootstrap-select > select {position: absolute !important;bottom: 0;left: 50%;display: block !important;width: 0.5px !important;height: 100% !important;padding: 0 !important;opacity: 0 !important;border: none;}.has-error .bootstrap-select .dropdown-toggle,.error .bootstrap-select .dropdown-toggle {border-color: #b94a48;}.bootstrap-select.fit-width {width: auto !important;}.bootstrap-select.form-control {margin-bottom: 0;padding: 0;border: none;}.bootstrap-select.form-control.input-group-btn {z-index: auto;}.bootstrap-select.form-control.input-group-btn:not(:first-child):not(:last-child) > .btn {border-radius: 0;}.form-inline .bootstrap-select.btn-group,.form-horizontal .bootstrap-select.btn-group,.form-group .bootstrap-select.btn-group {margin-bottom: 0;}.form-group-lg .bootstrap-select.btn-group.form-control,.form-group-sm .bootstrap-select.btn-group.form-control {padding: 0;}.form-group-lg .bootstrap-select.btn-group.form-control .dropdown-toggle,.form-group-sm .bootstrap-select.btn-group.form-control .dropdown-toggle {height: 100%;font-size: inherit;line-height: inherit;border-radius: inherit;}.form-inline .bootstrap-select.btn-group .form-control {width: 100%;}.bootstrap-select.btn-group.bs-container .dropdown-menu {z-index: 1060;}.bootstrap-select.btn-group .dropdown-toggle .caret {position: absolute;top: 50%;right: 12px;margin-top: -2px;vertical-align: middle;}.bootstrap-select.btn-group[class*="col-"] .dropdown-toggle {width: 100%;}.bootstrap-select.btn-group .dropdown-menu {min-width: 100%;-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;}.bootstrap-select.btn-group .dropdown-menu.inner {position: static;float: none;border: 0;padding: 0;margin: 0;border-radius: 0;-webkit-box-shadow: none;box-shadow: none;overflow-y: auto;max-height: 150px;max-width: 300px;}.bootstrap-select.btn-group .dropdown-menu li {position: relative;}.bootstrap-select.btn-group .dropdown-menu li a {cursor: pointer;-webkit-user-select: none;-moz-user-select: none;-ms-user-select: none;user-select: none;}.bootstrap-select.btn-group .dropdown-menu li a span.text {display: inline-block;}.bootstrap-select.btn-group .dropdown-menu li small {padding-left: 0.5em;}.bootstrap-select.btn-group.fit-width .dropdown-toggle .caret {position: static;top: auto;margin-top: -1px;}.bootstrap-select.btn-group.show-tick .dropdown-menu li a span.text {margin-right: 34px;}.bs-actionsbox .btn-group button {width: 50%;}.bs-donebutton .btn-group button {width: 100%;}.bs-searchbox + .bs-actionsbox {padding: 0 8px 4px;}.bs-searchbox .form-control {margin-bottom: 0;height: 80%;padding-top: 3px;padding-bottom: 3px;width: 100%;float: none;}.font-cousm {font-size: 15px;}

總結

以上是生活随笔為你收集整理的Angular 下拉搜索框实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。