编辑按钮实现
This commit is contained in:
@@ -60,7 +60,8 @@ import {ElMessageBox, ElMessage} from 'element-plus';
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'RawMaterialTable',
|
name: 'RawMaterialTable',
|
||||||
setup() {
|
emits: ['edit'], // 声明触发的事件
|
||||||
|
setup(props, { emit }) {
|
||||||
const tableData = ref([]);
|
const tableData = ref([]);
|
||||||
const loading = ref(false);
|
const loading = ref(false);
|
||||||
const searchKeyword = ref('');
|
const searchKeyword = ref('');
|
||||||
@@ -155,6 +156,10 @@ export default {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleEdit = (row) => {
|
||||||
|
emit('edit', row); // 触发 edit 事件,并传递当前行数据
|
||||||
|
};
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
fetchRawMaterials();
|
fetchRawMaterials();
|
||||||
});
|
});
|
||||||
@@ -170,7 +175,7 @@ export default {
|
|||||||
handleSizeChange,
|
handleSizeChange,
|
||||||
handleCurrentChange,
|
handleCurrentChange,
|
||||||
handleExpandChange,
|
handleExpandChange,
|
||||||
handleEdit: (row) => console.log('edit', row),
|
handleEdit,
|
||||||
handleDelete,
|
handleDelete,
|
||||||
fetchRawMaterials, // 将方法暴露出去
|
fetchRawMaterials, // 将方法暴露出去
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,10 +9,11 @@
|
|||||||
<el-icon :size="20"><Refresh /></el-icon>
|
<el-icon :size="20"><Refresh /></el-icon>
|
||||||
</el-button>
|
</el-button>
|
||||||
</div>
|
</div>
|
||||||
<el-button type="primary" @click="addRawMaterial">添加原料</el-button>
|
<el-button type="primary" @click="handleAddRawMaterial">添加原料</el-button>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<raw-material-table ref="rawMaterialTableRef"></raw-material-table>
|
<!-- 确保只有一个 raw-material-table 实例,并正确绑定事件 -->
|
||||||
|
<raw-material-table ref="rawMaterialTableRef" @edit="handleEdit"></raw-material-table>
|
||||||
</el-card>
|
</el-card>
|
||||||
|
|
||||||
<!-- 添加/编辑原料弹窗 -->
|
<!-- 添加/编辑原料弹窗 -->
|
||||||
@@ -23,8 +24,12 @@
|
|||||||
:close-on-click-modal="false"
|
:close-on-click-modal="false"
|
||||||
@close="handleCancel"
|
@close="handleCancel"
|
||||||
>
|
>
|
||||||
|
<!-- 只有在弹窗显示时才渲染表单组件,并传递编辑状态和初始数据 -->
|
||||||
<raw-material-form
|
<raw-material-form
|
||||||
|
v-if="showDialog"
|
||||||
ref="rawMaterialFormRef"
|
ref="rawMaterialFormRef"
|
||||||
|
:isEdit="isEditMode"
|
||||||
|
:initialData="currentEditData"
|
||||||
@submit="handleSubmit"
|
@submit="handleSubmit"
|
||||||
@cancel="handleCancel"
|
@cancel="handleCancel"
|
||||||
></raw-material-form>
|
></raw-material-form>
|
||||||
@@ -33,7 +38,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { ref } from 'vue';
|
import { ref, nextTick } from 'vue'; // 导入 nextTick
|
||||||
import { Refresh } from '@element-plus/icons-vue';
|
import { Refresh } from '@element-plus/icons-vue';
|
||||||
import { ElMessage } from 'element-plus';
|
import { ElMessage } from 'element-plus';
|
||||||
import RawMaterialTable from '../../components/feed/RawMaterialTable.vue';
|
import RawMaterialTable from '../../components/feed/RawMaterialTable.vue';
|
||||||
@@ -51,6 +56,8 @@ export default {
|
|||||||
const rawMaterialTableRef = ref(null);
|
const rawMaterialTableRef = ref(null);
|
||||||
const rawMaterialFormRef = ref(null); // 用于引用 RawMaterialForm 组件
|
const rawMaterialFormRef = ref(null); // 用于引用 RawMaterialForm 组件
|
||||||
const showDialog = ref(false);
|
const showDialog = ref(false);
|
||||||
|
const isEditMode = ref(false); // 标识当前是新增模式还是编辑模式
|
||||||
|
const currentEditData = ref(null); // 存储当前正在编辑的原料数据
|
||||||
const formTitle = ref('添加原料');
|
const formTitle = ref('添加原料');
|
||||||
|
|
||||||
const refreshList = () => {
|
const refreshList = () => {
|
||||||
@@ -59,31 +66,56 @@ export default {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const addRawMaterial = () => {
|
// 处理添加原料按钮点击事件
|
||||||
|
const handleAddRawMaterial = () => {
|
||||||
formTitle.value = '添加原料';
|
formTitle.value = '添加原料';
|
||||||
|
isEditMode.value = false; // 新增模式
|
||||||
|
currentEditData.value = null; // 清空编辑数据
|
||||||
showDialog.value = true;
|
showDialog.value = true;
|
||||||
// 确保在 dialog 显示后,表单组件被渲染,然后调用 resetForm
|
// 在 nextTick 中执行,确保 DOM 更新完成,表单组件被渲染后才调用 resetForm
|
||||||
if (rawMaterialFormRef.value) {
|
nextTick(() => {
|
||||||
rawMaterialFormRef.value.resetForm();
|
rawMaterialFormRef.value?.resetForm();
|
||||||
}
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 处理编辑按钮点击事件
|
||||||
|
const handleEdit = (row) => {
|
||||||
|
formTitle.value = '编辑原料';
|
||||||
|
isEditMode.value = true; // 编辑模式
|
||||||
|
currentEditData.value = { ...row }; // 存储当前编辑数据,进行深拷贝
|
||||||
|
showDialog.value = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 处理表单提交
|
||||||
const handleSubmit = async (formData) => {
|
const handleSubmit = async (formData) => {
|
||||||
try {
|
try {
|
||||||
|
let message = '';
|
||||||
|
if (isEditMode.value) {
|
||||||
|
// 编辑模式
|
||||||
|
await FeedApi.updateRawMaterial(currentEditData.value.id, formData);
|
||||||
|
message = '原料更新成功';
|
||||||
|
} else {
|
||||||
|
// 新增模式
|
||||||
await FeedApi.createRawMaterial(formData);
|
await FeedApi.createRawMaterial(formData);
|
||||||
ElMessage.success('原料添加成功');
|
message = '原料添加成功';
|
||||||
|
}
|
||||||
|
ElMessage.success(message);
|
||||||
refreshList(); // 刷新列表
|
refreshList(); // 刷新列表
|
||||||
showDialog.value = false; // 关闭弹窗
|
showDialog.value = false; // 关闭弹窗
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ElMessage.error('原料添加失败: ' + (error.message || '未知错误'));
|
ElMessage.error('操作失败: ' + (error.message || '未知错误'));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 处理弹窗取消或关闭
|
||||||
const handleCancel = () => {
|
const handleCancel = () => {
|
||||||
showDialog.value = false;
|
showDialog.value = false;
|
||||||
|
// 关闭弹窗时重置表单和编辑状态
|
||||||
if (rawMaterialFormRef.value) {
|
if (rawMaterialFormRef.value) {
|
||||||
rawMaterialFormRef.value.resetForm(); // 关闭时重置表单
|
rawMaterialFormRef.value.resetForm();
|
||||||
}
|
}
|
||||||
|
isEditMode.value = false;
|
||||||
|
currentEditData.value = null;
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -91,8 +123,11 @@ export default {
|
|||||||
rawMaterialFormRef,
|
rawMaterialFormRef,
|
||||||
showDialog,
|
showDialog,
|
||||||
formTitle,
|
formTitle,
|
||||||
|
isEditMode,
|
||||||
|
currentEditData,
|
||||||
refreshList,
|
refreshList,
|
||||||
addRawMaterial,
|
handleAddRawMaterial,
|
||||||
|
handleEdit,
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
handleCancel,
|
handleCancel,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user