Mar*_*ark 5 javascript typescript vue.js vuetify.js
因此,我有一个用于自动搜索零件表的自动完成输入。零件表具有标识列,零件号列,修订版列,描述列和其他一些信息列。
我可以使用它,因此在文本字段中键入时,自动完成选项会显示零件号,转速和描述,并按零件号过滤零件。
但是我的目标是能够同时过滤零件号和描述。
我曾尝试修改item-text =“ fpartno”,但无法弄清楚如何使其与两个或多个属性一起使用。而且,我在网上找到的大多数解决方案只会影响选项的显示方式,而不会影响其过滤方式。
同样,删除项目文本也不起作用。
我将自动完成功能放入“单页组件”中。我目前正在使用Typescript,但我也会接受使用javascript的答案。如果我只能使用理想的模板语法。
注意:属性fcomment包含描述,以防万一。
<template>
<v-container fluid grid-list-xl>
<v-layout wrap align-center>
<v-flex xs12 sm6 d-flex>
<v-autocomplete :items="inmastxs"
label="Part #"
item-value="identity_column"
item-text="fpartno"
cache-items
:search-input.sync="search"
solo>
<template slot="selection" slot-scope="data">
{{ data.item.fpartno }} - {{ data.item.frev }} ({{ data.item.fcomment }})
</template>
<template slot="item" slot-scope="data">
{{ data.item.fpartno }} - {{ data.item.frev }} ({{ data.item.fcomment }})
</template>
</v-autocomplete>
</v-flex>
</v-layout>
</v-container>
</template>
<script lang="ts">
import Vue from 'vue'
import { Component, Prop, Watch } from 'vue-property-decorator';
import { inmastx } from '../../models/M2M/inmastx';
import axios from 'axios';
@Component({})
export default class InMastxSearch extends Vue {
private search: string = "";
private PartNumber: string = "";
private loading: boolean = false;
private inmastxs: inmastx[] = [];
@Watch('search')
onPropertyChanged(value: string, oldValue: string) {
this.fetchParts(value);
}
private mounted() {
this.fetchParts("");
}
private fetchParts(value: string) {
if (value == null) {
value = "";
}
console.log(value);
this.loading = true
axios.get("../odata/inmastx?$orderby=fpartno,frev&$top=10&$filter=contains(fpartno,'" + value + "') or contains(fcomment,'" + value + "')")
.then((response) => {
this.inmastxs = response.data.value;
console.log(this.inmastxs);
})
.catch(function (error) {
console.log(error);
}).then(() => {
this.loading = false;
});
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
如果您需要更多详细信息,请告诉我,并提前致谢。
eve*_*ram 13
在这里玩游戏晚了,但解决方案是绑定自定义过滤器
<v-autocomplete
:filter="filterObject"
>
Run Code Online (Sandbox Code Playgroud)
然后定义行为
methods: {
filterObject(item, queryText, itemText) {
return (
item.prop1.toLocaleLowerCase().indexOf(queryText.toLocaleLowerCase()) >
-1 ||
item.prop2.toLocaleLowerCase().indexOf(queryText.toLocaleLowerCase()) > -1
);
}
}
Run Code Online (Sandbox Code Playgroud)
在V型过滤器自动完成文件道具提供的链接默认TS实现。
小智 8
带走:
item-text="fpartno"
Run Code Online (Sandbox Code Playgroud)
并使用:
no-filter="true"
Run Code Online (Sandbox Code Playgroud)
所以我意识到我使用了错误的工具来完成这项工作。在阅读文档时,我一定错过了有关 Combobox 的部分,其中包含我需要的一切。
无论如何,将v-autocomplete更改为v-combobox删除item-text="fpartno"并添加自定义过滤器为我的组件提供了我需要的功能。
在这里它工作正常:
下面是我更新的代码:
<template>
<v-container fluid grid-list-xl>
<v-layout wrap align-center>
<v-flex xs12 sm6 d-flex>
<v-combobox :items="inmastxs"
label="Part #"
item-value="identity_column"
:filter="PartRevDescFilter"
cache-items
:search-input.sync="search"
solo>
<template slot="selection" slot-scope="data">
{{ data.item.fpartno }} - {{ data.item.frev }} ({{ data.item.fcomment }})
</template>
<template slot="item" slot-scope="data">
{{ data.item.fpartno }} - {{ data.item.frev }} ({{ data.item.fcomment }})
</template>
</v-combobox>
</v-flex>
</v-layout>
</v-container>
</template>
<script lang="ts">
import Vue from 'vue'
import { Component, Prop, Watch } from 'vue-property-decorator';
import { inmastx } from '../../models/M2M/inmastx';
import axios from 'axios';
@Component({})
export default class InMastxSearch extends Vue {
private search: string = "";
private PartNumber: string = "";
private loading: boolean = false;
private inmastxs: inmastx[] = [];
@Watch('search')
onPropertyChanged(value: string, oldValue: string) {
this.fetchParts(value);
}
private mounted() {
this.fetchParts("");
}
private fetchParts(value: string) {
if (value == null) {
value = "";
}
this.loading = true
axios.get("../odata/inmastx?$orderby=fpartno,frev&$top=10&$filter=contains(fpartno,'" + value + "') or contains(fcomment,'" + value + "')")
.then((response) => {
this.inmastxs = response.data.value;
})
.catch(function (error) {
console.log(error);
}).then(() => {
this.loading = false;
});
}
private PartRevDescFilter(item: inmastx, queryText, itemText) {
return (item.fpartno.toLowerCase().includes(queryText.toLowerCase()) || item.frev.toLowerCase().includes(queryText.toLowerCase()) || item.fcomment.toLowerCase().includes(queryText.toLowerCase()));
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
因为问题的标题提到了自动完成而不是组合框,所以我认为这个答案不足以被视为最佳答案。除非自动完成一直是错误的工具。
| 归档时间: |
|
| 查看次数: |
1521 次 |
| 最近记录: |