优化供应商全称搜索与下拉展示
This commit is contained in:
parent
c2a0756d2f
commit
fd26b766a3
@ -5,6 +5,10 @@ export interface SupplierOption {
|
|||||||
name: string;
|
name: string;
|
||||||
anomalyCount: number;
|
anomalyCount: number;
|
||||||
tier: string;
|
tier: string;
|
||||||
|
city?: string | null;
|
||||||
|
province?: string | null;
|
||||||
|
shortName?: string | null;
|
||||||
|
reviewStatus?: string | null;
|
||||||
}
|
}
|
||||||
export interface SupplierListData {
|
export interface SupplierListData {
|
||||||
items: SupplierOption[];
|
items: SupplierOption[];
|
||||||
|
|||||||
@ -11,7 +11,7 @@ const route = useRoute();
|
|||||||
const supplierNames = ref<string[]>([]);
|
const supplierNames = ref<string[]>([]);
|
||||||
const selected = ref("");
|
const selected = ref("");
|
||||||
const supplierQuery = ref("");
|
const supplierQuery = ref("");
|
||||||
const supplierResults = ref<string[]>([]);
|
const supplierResults = ref<Array<{ name: string; city?: string | null; anomalyCount: number; reviewStatus?: string | null }>>([]);
|
||||||
const supplierDropdownOpen = ref(false);
|
const supplierDropdownOpen = ref(false);
|
||||||
const supplierSearching = ref(false);
|
const supplierSearching = ref(false);
|
||||||
let supplierSearchTimer: ReturnType<typeof setTimeout> | undefined;
|
let supplierSearchTimer: ReturnType<typeof setTimeout> | undefined;
|
||||||
@ -95,7 +95,7 @@ async function loadOptions() {
|
|||||||
? route.query.supplier
|
? route.query.supplier
|
||||||
: (response.data.data?.defaultSupplier ?? supplierNames.value[0] ?? "");
|
: (response.data.data?.defaultSupplier ?? supplierNames.value[0] ?? "");
|
||||||
supplierQuery.value = selected.value;
|
supplierQuery.value = selected.value;
|
||||||
supplierResults.value = supplierNames.value;
|
supplierResults.value = (response.data.data?.items ?? []).map((item) => ({ name: item.name, city: item.city, anomalyCount: item.anomalyCount, reviewStatus: item.reviewStatus }));
|
||||||
if (selected.value) await loadProfile();
|
if (selected.value) await loadProfile();
|
||||||
} catch {
|
} catch {
|
||||||
error.value = "供应商列表加载失败,请重试。";
|
error.value = "供应商列表加载失败,请重试。";
|
||||||
@ -105,7 +105,7 @@ async function loadOptions() {
|
|||||||
function openSupplierDropdown() {
|
function openSupplierDropdown() {
|
||||||
supplierDropdownOpen.value = true;
|
supplierDropdownOpen.value = true;
|
||||||
if (!supplierQuery.value || supplierQuery.value === selected.value) {
|
if (!supplierQuery.value || supplierQuery.value === selected.value) {
|
||||||
supplierResults.value = supplierNames.value;
|
supplierResults.value = supplierNames.value.map((name) => ({ name, anomalyCount: 0 }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,20 +113,19 @@ function searchSuppliers(value: string) {
|
|||||||
if (supplierSearchTimer) clearTimeout(supplierSearchTimer);
|
if (supplierSearchTimer) clearTimeout(supplierSearchTimer);
|
||||||
const keyword = value.trim();
|
const keyword = value.trim();
|
||||||
if (!keyword) {
|
if (!keyword) {
|
||||||
supplierResults.value = supplierNames.value;
|
supplierResults.value = supplierNames.value.map((name) => ({ name, anomalyCount: 0 }));
|
||||||
supplierSearching.value = false;
|
supplierSearching.value = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const local = supplierNames.value.filter((name) => matchesSupplier(name, keyword));
|
const local = supplierNames.value.filter((name) => matchesSupplier(name, keyword));
|
||||||
supplierResults.value = local;
|
supplierResults.value = local.map((name) => ({ name, anomalyCount: 0 }));
|
||||||
supplierSearching.value = true;
|
supplierSearching.value = true;
|
||||||
supplierSearchTimer = setTimeout(async () => {
|
supplierSearchTimer = setTimeout(async () => {
|
||||||
try {
|
try {
|
||||||
const response = await fetchSupplierOptions(keyword);
|
const response = await fetchSupplierOptions(keyword);
|
||||||
supplierResults.value =
|
supplierResults.value = (response.data.data?.items ?? []).map((item) => ({ name: item.name, city: item.city, anomalyCount: item.anomalyCount, reviewStatus: item.reviewStatus }));
|
||||||
response.data.data?.items.map((item) => item.name) ?? [];
|
|
||||||
} catch {
|
} catch {
|
||||||
supplierResults.value = local;
|
supplierResults.value = local.map((name) => ({ name, anomalyCount: 0 }));
|
||||||
} finally {
|
} finally {
|
||||||
supplierSearching.value = false;
|
supplierSearching.value = false;
|
||||||
}
|
}
|
||||||
@ -235,16 +234,17 @@ watch(
|
|||||||
role="listbox"
|
role="listbox"
|
||||||
>
|
>
|
||||||
<button
|
<button
|
||||||
v-for="name in supplierResults"
|
v-for="item in supplierResults"
|
||||||
:key="name"
|
:key="item.name"
|
||||||
type="button"
|
type="button"
|
||||||
class="supplier-option"
|
class="supplier-option"
|
||||||
:class="{ active: name === selected }"
|
:class="{ active: item.name === selected }"
|
||||||
role="option"
|
role="option"
|
||||||
:aria-selected="name === selected"
|
:aria-selected="item.name === selected"
|
||||||
@mousedown.prevent="selectSupplier(name)"
|
@mousedown.prevent="selectSupplier(item.name)"
|
||||||
>
|
>
|
||||||
{{ name }}
|
{{ item.name }}<small v-if="item.city"> · {{ item.city }}</small>
|
||||||
|
<small v-if="item.anomalyCount"> · {{ item.anomalyCount }} 条异常</small>
|
||||||
</button>
|
</button>
|
||||||
<span v-if="supplierSearching" class="supplier-option-state"
|
<span v-if="supplierSearching" class="supplier-option-state"
|
||||||
>搜索中…</span
|
>搜索中…</span
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user