js
/**
* 输入框输入事件:
* event: 输入框事件
* text: 还没输入时,默认赋值的字段
*/
inputChange(event, text?: string) {
this.searchKeyword = '';
// 保证searchKeyword不为空
// this.matchKey = '';
if (this.searchKeyword.length === 0) {
this.searchKeyword = 'key';
}
if (text) {
event.target.value = text;
this.fuzzy = text;
}
// 判断自动联系下拉框出现情况
if (event.target.value) {
event.target.value = event.target.value.replace(/(^\s*)/g, '');
// 通过 空格符 分隔输入框的值为N个关键字
const splitsArr = event.target.value.split(' ');
// 需要匹配的字段:取数组最后一个关键字
this.matchKey = splitsArr.slice(-1).join();
this.notMatch = splitsArr.slice(0, splitsArr.length - 1).join(' ') + ' ';
// 输入前面是空格,或者正在输入第一个条件时,给提示
if (this.matchKey.substring(0, 1) === '(') {
this.matchKey = this.matchKey.substring(1);
this.notMatch += '(';
}
} else {
this.matchKey = '';
}
this.fuzzyWidth = this.calTextWidth(this.fuzzy, '14px');
this.data.forEach((item) => {
for (const key in item) {
if (item[key].indexOf(this.matchKey) > -1) {
this.searchKeyword = key;
}
}
});
this.propagateChange(event.target.value);
}