增加搜索框
This commit is contained in:
@@ -1658,7 +1658,7 @@
|
|||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "按名称模糊查询",
|
"description": "按营养名称模糊查询",
|
||||||
"name": "name",
|
"name": "name",
|
||||||
"in": "query"
|
"in": "query"
|
||||||
},
|
},
|
||||||
@@ -1679,6 +1679,12 @@
|
|||||||
"description": "每页数量",
|
"description": "每页数量",
|
||||||
"name": "page_size",
|
"name": "page_size",
|
||||||
"in": "query"
|
"in": "query"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "string",
|
||||||
|
"description": "按原料名称模糊查询",
|
||||||
|
"name": "raw_material_name",
|
||||||
|
"in": "query"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"responses": {
|
"responses": {
|
||||||
@@ -2660,10 +2666,16 @@
|
|||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "按名称模糊查询",
|
"description": "按原料名称模糊查询",
|
||||||
"name": "name",
|
"name": "name",
|
||||||
"in": "query"
|
"in": "query"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "string",
|
||||||
|
"description": "按营养名称模糊查询",
|
||||||
|
"name": "nutrient_name",
|
||||||
|
"in": "query"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "排序字段,例如 \"id DESC\"",
|
"description": "排序字段,例如 \"id DESC\"",
|
||||||
@@ -3069,7 +3081,6 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"enum": [
|
"enum": [
|
||||||
7,
|
|
||||||
-1,
|
-1,
|
||||||
0,
|
0,
|
||||||
1,
|
1,
|
||||||
@@ -3079,12 +3090,12 @@
|
|||||||
5,
|
5,
|
||||||
-1,
|
-1,
|
||||||
5,
|
5,
|
||||||
6
|
6,
|
||||||
|
7
|
||||||
],
|
],
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
"format": "int32",
|
"format": "int32",
|
||||||
"x-enum-varnames": [
|
"x-enum-varnames": [
|
||||||
"_numLevels",
|
|
||||||
"DebugLevel",
|
"DebugLevel",
|
||||||
"InfoLevel",
|
"InfoLevel",
|
||||||
"WarnLevel",
|
"WarnLevel",
|
||||||
@@ -3094,7 +3105,8 @@
|
|||||||
"FatalLevel",
|
"FatalLevel",
|
||||||
"_minLevel",
|
"_minLevel",
|
||||||
"_maxLevel",
|
"_maxLevel",
|
||||||
"InvalidLevel"
|
"InvalidLevel",
|
||||||
|
"_numLevels"
|
||||||
],
|
],
|
||||||
"name": "level",
|
"name": "level",
|
||||||
"in": "query"
|
"in": "query"
|
||||||
@@ -9641,7 +9653,6 @@
|
|||||||
"type": "integer",
|
"type": "integer",
|
||||||
"format": "int32",
|
"format": "int32",
|
||||||
"enum": [
|
"enum": [
|
||||||
7,
|
|
||||||
-1,
|
-1,
|
||||||
0,
|
0,
|
||||||
1,
|
1,
|
||||||
@@ -9651,10 +9662,10 @@
|
|||||||
5,
|
5,
|
||||||
-1,
|
-1,
|
||||||
5,
|
5,
|
||||||
6
|
6,
|
||||||
|
7
|
||||||
],
|
],
|
||||||
"x-enum-varnames": [
|
"x-enum-varnames": [
|
||||||
"_numLevels",
|
|
||||||
"DebugLevel",
|
"DebugLevel",
|
||||||
"InfoLevel",
|
"InfoLevel",
|
||||||
"WarnLevel",
|
"WarnLevel",
|
||||||
@@ -9664,7 +9675,8 @@
|
|||||||
"FatalLevel",
|
"FatalLevel",
|
||||||
"_minLevel",
|
"_minLevel",
|
||||||
"_maxLevel",
|
"_maxLevel",
|
||||||
"InvalidLevel"
|
"InvalidLevel",
|
||||||
|
"_numLevels"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -30,7 +30,8 @@ import {PaginationDTO, Response} from '../enums';
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {object} NutrientsParams
|
* @typedef {object} NutrientsParams
|
||||||
* @property {string} [name] - 按名称模糊查询
|
* @property {string} [name] - 按营养名称模糊查询
|
||||||
|
* @property {string} [raw_material_name] - 按原料名称模糊查询
|
||||||
* @property {string} [order_by] - 排序字段,例如 "id DESC"
|
* @property {string} [order_by] - 排序字段,例如 "id DESC"
|
||||||
* @property {number} [page]
|
* @property {number} [page]
|
||||||
* @property {number} [page_size]
|
* @property {number} [page_size]
|
||||||
@@ -239,7 +240,8 @@ import {PaginationDTO, Response} from '../enums';
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {object} RawMaterialsParams
|
* @typedef {object} RawMaterialsParams
|
||||||
* @property {string} [name] - 按名称模糊查询
|
* @property {string} [name] - 按原料名称模糊查询
|
||||||
|
* @property {string} [nutrient_name] - 按营养名称模糊查询
|
||||||
* @property {string} [order_by] - 排序字段,例如 "id DESC"
|
* @property {string} [order_by] - 排序字段,例如 "id DESC"
|
||||||
* @property {number} [page]
|
* @property {number} [page]
|
||||||
* @property {number} [page_size]
|
* @property {number} [page_size]
|
||||||
|
|||||||
@@ -1,5 +1,21 @@
|
|||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
|
<!-- 搜索区域 -->
|
||||||
|
<div style="margin-bottom: 20px; display: flex; align-items: center; gap: 10px;">
|
||||||
|
<el-select v-model="searchType" placeholder="请选择搜索类型" style="width: 150px;">
|
||||||
|
<el-option label="按原料名称" value="name"></el-option>
|
||||||
|
<el-option label="按营养名称" value="nutrient_name"></el-option>
|
||||||
|
</el-select>
|
||||||
|
<el-input
|
||||||
|
v-model="searchKeyword"
|
||||||
|
placeholder="请输入关键词"
|
||||||
|
clearable
|
||||||
|
style="width: 300px;"
|
||||||
|
@keyup.enter="handleSearch"
|
||||||
|
></el-input>
|
||||||
|
<el-button type="primary" @click="handleSearch">搜索</el-button>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- 原料列表 -->
|
<!-- 原料列表 -->
|
||||||
<el-table :data="tableData" style="width: 100%" v-loading="loading" row-key="id"
|
<el-table :data="tableData" style="width: 100%" v-loading="loading" row-key="id"
|
||||||
:expand-row-keys="expandRowKeys" @expand-change="handleExpandChange">
|
:expand-row-keys="expandRowKeys" @expand-change="handleExpandChange">
|
||||||
@@ -48,7 +64,7 @@ export default {
|
|||||||
const tableData = ref([]);
|
const tableData = ref([]);
|
||||||
const loading = ref(false);
|
const loading = ref(false);
|
||||||
const searchKeyword = ref('');
|
const searchKeyword = ref('');
|
||||||
const searchType = ref('rawMaterial');
|
const searchType = ref('name'); // 默认按原料名称搜索
|
||||||
const expandRowKeys = ref([]);
|
const expandRowKeys = ref([]);
|
||||||
|
|
||||||
const pagination = ref({
|
const pagination = ref({
|
||||||
@@ -60,48 +76,24 @@ export default {
|
|||||||
const fetchRawMaterials = async () => {
|
const fetchRawMaterials = async () => {
|
||||||
loading.value = true;
|
loading.value = true;
|
||||||
try {
|
try {
|
||||||
let response;
|
const params = {
|
||||||
if (searchType.value === 'rawMaterial') {
|
|
||||||
response = await FeedApi.getRawMaterials({
|
|
||||||
name: searchKeyword.value,
|
|
||||||
page: pagination.value.page,
|
page: pagination.value.page,
|
||||||
page_size: pagination.value.page_size,
|
page_size: pagination.value.page_size,
|
||||||
});
|
};
|
||||||
} else {
|
|
||||||
const nutrientResponse = await FeedApi.getNutrients({
|
if (searchKeyword.value) {
|
||||||
name: searchKeyword.value,
|
params[searchType.value] = searchKeyword.value;
|
||||||
page: pagination.value.page,
|
|
||||||
page_size: pagination.value.page_size,
|
|
||||||
});
|
|
||||||
// 从营养数据中提取原料
|
|
||||||
const rawMaterials = [];
|
|
||||||
if (nutrientResponse.data && nutrientResponse.data.list) {
|
|
||||||
nutrientResponse.data.list.forEach(nutrient => {
|
|
||||||
if (nutrient.raw_materials) {
|
|
||||||
nutrient.raw_materials.forEach(rm => {
|
|
||||||
// 避免重复添加
|
|
||||||
if (!rawMaterials.some(item => item.id === rm.id)) {
|
|
||||||
rawMaterials.push({
|
|
||||||
id: rm.id,
|
|
||||||
name: rm.name,
|
|
||||||
// 模拟其他字段
|
|
||||||
description: '通过营养搜索',
|
|
||||||
raw_material_nutrients: []
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
response = { data: { list: rawMaterials, pagination: { total: rawMaterials.length } } };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const response = await FeedApi.getRawMaterials(params);
|
||||||
|
|
||||||
if (response.data) {
|
if (response.data) {
|
||||||
tableData.value = response.data.list;
|
tableData.value = response.data.list;
|
||||||
pagination.value.total = response.data.pagination.total;
|
pagination.value.total = response.data.pagination.total;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('获取原料列表失败:', error);
|
console.error('获取原料列表失败:', error);
|
||||||
|
ElMessage.error('获取原料列表失败');
|
||||||
} finally {
|
} finally {
|
||||||
loading.value = false;
|
loading.value = false;
|
||||||
}
|
}
|
||||||
@@ -124,7 +116,8 @@ export default {
|
|||||||
|
|
||||||
const handleExpandChange = async (row, expandedRows) => {
|
const handleExpandChange = async (row, expandedRows) => {
|
||||||
const isExpanded = expandedRows.some(r => r.id === row.id);
|
const isExpanded = expandedRows.some(r => r.id === row.id);
|
||||||
if (isExpanded && row.raw_material_nutrients.length === 0) {
|
// 优化:仅在展开时且数据未加载时才请求
|
||||||
|
if (isExpanded && (!row.raw_material_nutrients || row.raw_material_nutrients.length === 0)) {
|
||||||
try {
|
try {
|
||||||
const response = await FeedApi.getRawMaterialById(row.id);
|
const response = await FeedApi.getRawMaterialById(row.id);
|
||||||
if (response.data) {
|
if (response.data) {
|
||||||
@@ -135,6 +128,7 @@ export default {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('获取原料详情失败:', error);
|
console.error('获取原料详情失败:', error);
|
||||||
|
ElMessage.error('获取原料营养成分失败');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user