2025-11-21 16:57:50 +08:00
|
|
|
<template>
|
|
|
|
|
<div>
|
2025-11-21 17:27:40 +08:00
|
|
|
<!-- 搜索区域 -->
|
|
|
|
|
<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>
|
|
|
|
|
|
2025-11-21 16:57:50 +08:00
|
|
|
<!-- 原料列表 -->
|
|
|
|
|
<el-table :data="tableData" style="width: 100%" v-loading="loading" row-key="id"
|
|
|
|
|
:expand-row-keys="expandRowKeys" @expand-change="handleExpandChange">
|
|
|
|
|
<el-table-column type="expand">
|
|
|
|
|
<template #default="props">
|
|
|
|
|
<div style="padding: 10px 20px;">
|
|
|
|
|
<h4>营养成分</h4>
|
|
|
|
|
<el-table :data="props.row.raw_material_nutrients" border>
|
|
|
|
|
<el-table-column prop="nutrient_name" label="营养名称"></el-table-column>
|
|
|
|
|
<el-table-column prop="value" label="含量"></el-table-column>
|
|
|
|
|
</el-table>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
</el-table-column>
|
|
|
|
|
<el-table-column prop="name" label="原料名称"></el-table-column>
|
|
|
|
|
<el-table-column prop="description" label="描述"></el-table-column>
|
|
|
|
|
<el-table-column label="操作">
|
|
|
|
|
<template #default="scope">
|
|
|
|
|
<el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
|
2025-11-22 17:30:07 +08:00
|
|
|
<el-button size="small" type="warning" @click="handleEditNutrients(scope.row)">修改营养成分</el-button>
|
2025-11-21 16:57:50 +08:00
|
|
|
<el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
|
|
|
|
|
</template>
|
|
|
|
|
</el-table-column>
|
|
|
|
|
</el-table>
|
|
|
|
|
|
|
|
|
|
<!-- 分页 -->
|
|
|
|
|
<el-pagination
|
|
|
|
|
style="margin-top: 20px;"
|
|
|
|
|
:current-page="pagination.page"
|
|
|
|
|
:page-size="pagination.page_size"
|
|
|
|
|
:total="pagination.total"
|
|
|
|
|
layout="total, sizes, prev, pager, next, jumper"
|
|
|
|
|
@size-change="handleSizeChange"
|
|
|
|
|
@current-change="handleCurrentChange"
|
|
|
|
|
></el-pagination>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
2025-11-21 16:59:33 +08:00
|
|
|
import {ref, onMounted} from 'vue';
|
2025-11-21 16:57:50 +08:00
|
|
|
import {FeedApi} from '../../api/feed';
|
|
|
|
|
import {ElMessageBox, ElMessage} from 'element-plus';
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
name: 'RawMaterialTable',
|
2025-11-22 17:30:07 +08:00
|
|
|
emits: ['edit', 'edit-nutrients'], // 声明触发的事件
|
2025-11-21 17:48:11 +08:00
|
|
|
setup(props, { emit }) {
|
2025-11-21 16:57:50 +08:00
|
|
|
const tableData = ref([]);
|
|
|
|
|
const loading = ref(false);
|
|
|
|
|
const searchKeyword = ref('');
|
2025-11-21 17:27:40 +08:00
|
|
|
const searchType = ref('name'); // 默认按原料名称搜索
|
2025-11-21 16:57:50 +08:00
|
|
|
const expandRowKeys = ref([]);
|
|
|
|
|
|
|
|
|
|
const pagination = ref({
|
|
|
|
|
page: 1,
|
|
|
|
|
page_size: 10,
|
|
|
|
|
total: 0,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const fetchRawMaterials = async () => {
|
|
|
|
|
loading.value = true;
|
|
|
|
|
try {
|
2025-11-21 17:27:40 +08:00
|
|
|
const params = {
|
|
|
|
|
page: pagination.value.page,
|
|
|
|
|
page_size: pagination.value.page_size,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (searchKeyword.value) {
|
|
|
|
|
params[searchType.value] = searchKeyword.value;
|
2025-11-21 16:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-21 17:27:40 +08:00
|
|
|
const response = await FeedApi.getRawMaterials(params);
|
|
|
|
|
|
2025-11-21 16:57:50 +08:00
|
|
|
if (response.data) {
|
|
|
|
|
tableData.value = response.data.list;
|
|
|
|
|
pagination.value.total = response.data.pagination.total;
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('获取原料列表失败:', error);
|
2025-11-21 17:27:40 +08:00
|
|
|
ElMessage.error('获取原料列表失败');
|
2025-11-21 16:57:50 +08:00
|
|
|
} finally {
|
|
|
|
|
loading.value = false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleSearch = () => {
|
|
|
|
|
pagination.value.page = 1;
|
|
|
|
|
fetchRawMaterials();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleSizeChange = (size) => {
|
|
|
|
|
pagination.value.page_size = size;
|
|
|
|
|
fetchRawMaterials();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleCurrentChange = (page) => {
|
|
|
|
|
pagination.value.page = page;
|
|
|
|
|
fetchRawMaterials();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleExpandChange = async (row, expandedRows) => {
|
|
|
|
|
const isExpanded = expandedRows.some(r => r.id === row.id);
|
2025-11-21 17:27:40 +08:00
|
|
|
// 优化:仅在展开时且数据未加载时才请求
|
|
|
|
|
if (isExpanded && (!row.raw_material_nutrients || row.raw_material_nutrients.length === 0)) {
|
2025-11-21 16:57:50 +08:00
|
|
|
try {
|
|
|
|
|
const response = await FeedApi.getRawMaterialById(row.id);
|
|
|
|
|
if (response.data) {
|
|
|
|
|
const index = tableData.value.findIndex(item => item.id === row.id);
|
|
|
|
|
if (index !== -1) {
|
|
|
|
|
tableData.value[index].raw_material_nutrients = response.data.raw_material_nutrients;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('获取原料详情失败:', error);
|
2025-11-21 17:27:40 +08:00
|
|
|
ElMessage.error('获取原料营养成分失败');
|
2025-11-21 16:57:50 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleDelete = (row) => {
|
|
|
|
|
ElMessageBox.confirm(
|
|
|
|
|
`确定要删除原料 "${row.name}" 吗?`,
|
|
|
|
|
'提示',
|
|
|
|
|
{
|
|
|
|
|
confirmButtonText: '确定',
|
|
|
|
|
cancelButtonText: '取消',
|
|
|
|
|
type: 'warning',
|
|
|
|
|
}
|
|
|
|
|
).then(async () => {
|
|
|
|
|
try {
|
|
|
|
|
await FeedApi.deleteRawMaterial(row.id);
|
|
|
|
|
ElMessage.success('删除成功');
|
|
|
|
|
await fetchRawMaterials();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
ElMessage.error('删除失败: ' + (error.message || '未知错误'));
|
|
|
|
|
}
|
|
|
|
|
}).catch(() => {
|
|
|
|
|
// 用户取消操作
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-21 17:48:11 +08:00
|
|
|
const handleEdit = (row) => {
|
|
|
|
|
emit('edit', row); // 触发 edit 事件,并传递当前行数据
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-22 17:30:07 +08:00
|
|
|
const handleEditNutrients = (row) => {
|
|
|
|
|
emit('edit-nutrients', row); // 触发 edit-nutrients 事件
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-21 16:57:50 +08:00
|
|
|
onMounted(() => {
|
|
|
|
|
fetchRawMaterials();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
tableData,
|
|
|
|
|
loading,
|
|
|
|
|
searchKeyword,
|
|
|
|
|
searchType,
|
|
|
|
|
pagination,
|
|
|
|
|
expandRowKeys,
|
|
|
|
|
handleSearch,
|
|
|
|
|
handleSizeChange,
|
|
|
|
|
handleCurrentChange,
|
|
|
|
|
handleExpandChange,
|
2025-11-21 17:48:11 +08:00
|
|
|
handleEdit,
|
2025-11-21 16:57:50 +08:00
|
|
|
handleDelete,
|
2025-11-22 17:30:07 +08:00
|
|
|
handleEditNutrients,
|
2025-11-21 16:59:33 +08:00
|
|
|
fetchRawMaterials, // 将方法暴露出去
|
2025-11-21 16:57:50 +08:00
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
};
|
2025-11-21 17:48:11 +08:00
|
|
|
</script>
|