问题描述
我正在使用buefy的b-taginput和lodash的debounce方法在@typing事件期间从api源获取数据。问题是,当我在输入字段中按空格键时,在反跳方法内,输入字符无法识别为实际字符。
<b-field label="Roles">
<b-taginput
:value="this.objectData.roles"
:data="filteredTags"
autocomplete
field="role"
icon="label"
placeholder="add role..."
@focus="getAsyncRole"
@typing="getAsyncRole"
@input="(newValue) => {updateValue(newValue,'roles')}"
>
<template slot-scope="props">
<p>{{props.option.role}}</p>
</template>
<template slot="empty">There are no items</template>
</b-taginput>
</b-field>
getAsyncRole: debounce(function(name) {
console.log('inside getAsyncRole and name.length is '+name.length) // the length is 0 when i hit
spacebar but why?
if (!name.length) {
this.filteredTags = [];
return; //exits the function if length of input is zero
}
this.isFetching = true;
api
.getSearchData(this.sessionData.key,`/role/?filter={role} LIKE '%25${name}%25'`)
.then(response => {
console.log('response for getasync role is'+JSON.stringify(response))
this.filteredTags = [];
response.forEach(item => {
this.filteredTags.push(item);
});
})
.catch(error => {
this.filteredTags = [];
throw error;
})
.finally(() => {
this.isFetching = false;
});
},500),
如果我键入任何字母字符,上述代码都可以工作(即,根据输入字符,它可能会给我带来自动完成结果)。但是我也希望它在将空格键打到b-taginput中时列出所有自动完成结果(总结果)。由于无法将空格字符识别为实际字符,因此name.length
变为零,然后退出该函数而不进行api调用。
注意:我注意到此问题仅发生在b-taginput中。在<b-autocomplete>
的情况下不会发生此问题。使用<b-autocomplete>
时,如果我按空格键,则会得到所需的所有结果。因此,我认为这个问题仅针对b-taginput。请为此提供建议的解决方法。
解决方法
源代码指示@typing在发出输入之前对其进行修剪。这留下了几个选择,最好的选择(到目前为止)是预取未过滤的列表。有了列表,您可以完全像示例代码一样进行过滤,在列表中搜索输入字符串。
(示例代码之所以有效,是因为在每个字符串中都“找到”了通过键入空格发出的空字符串''
)
请考虑一下:您正在取消API的使用,因为您担心打的太重。放下去弹跳,然后击打一次。担心获取所有标签太长而无法等待?只需等待一次,再也不必等待(考虑到您愿意在每个空白输入上进行此等待)。