javascript - 使用文本框自定义多选 - 使用 contenteditable
问题描述
我正在使用文本框进行自定义多选。如果用户输入 1、2、3、4,那么 1 2 3 4 应该被视为单独的值。我可以使用<input type="text">
它而不是我想要 div 的文本框因此用户可以单击 div 中的任何位置并能够添加项目。下图是我当前的实现。
custom-autocomplete.component.html
<div name="test">
<ul class="autocomplete-text base-auto-select-cointainer">
<li class="d-inline-flex selected-item" *ngFor="let item of itemList;let i=index" title="{{item}}">{{item}}
<span class="remove-item pointer" role="presentation" (click)="handelOnItemRemove(i)">×</span></li>
<li class="d-inline-flex">
<input class="item-input" type="text" (keyup)="handelOnItemKeyUp($event)"
tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"
role="textbox">
</li>
</ul>
</div>
自定义自动完成组件.ts
import { Component, OnInit, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-custom-autocomplete',
templateUrl: './custom-autocomplete.component.html',
styleUrls: ['./custom-autocomplete.component.css']
})
export class CustomAutocompleteComponent implements OnInit {
itemList: any[] = []; // for collection selcted items
userInputs: any;
model = 'some text';
constructor() { }
// event emiters for passing data to parent componet.
@Output() handelItemSelection: EventEmitter<any> = new EventEmitter<any>();
ngOnInit() {
}
handelOnItemKeyUp(event) {
if (event.target.value) {
let userInput: string = event.target.value;
let splitChar = userInput.charAt(userInput.length - 1);
let tempArray: any[] = userInput.split(',');
if (tempArray.length > 1) {
if (tempArray[0] != "" && tempArray[0] != undefined) {
this.itemList.push(tempArray[0])
this.handelItemSelection.emit(this.itemList); //passing updated items to parent
}
}
console.log('')
splitChar == ',' ?event.target.value = null : '' // setting empty value to text box when user enter ,
}
}
// code block for handel item removal from list.
handelOnItemRemove(index: number) {
this.itemList.splice(index, 1);
this.handelItemSelection.emit(this.itemList); //passing updated items to parent
}
}
有人可以建议我如何使用<div contenteditable="true"></div>
解决方案
推荐阅读
- java - 在java中计算仅包含字母的字符串中的单词
- c# - 如何将多个 MigraDoc 文档连接成一个
- go - golang、ebpf 和函数持续时间
- sql - 在 Snowflake SQL 中制定相关子查询(按行检查条件)
- c# - 使用 C# DataContract 将 JSON 的一部分反序列化为字典
- javascript - 未捕获的语法错误:尝试解析 JSON 时 JSON 中位置 0 处出现意外标记 l
- opencv - 如何通过环境变量将 OpenCL 设备设置为 Intel
- multidimensional-array - 从两个不同数组的值计数创建 Array2D
- sql - 如何从具有相同列路径的另一个表中填充一个表
- apache-spark - 如何在 Spark SQL 查询中转义特殊字符,“`”似乎不起作用