edit.hbs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <template>
  2. <vab-dialog v-model="dialogFormVisible" append-to-body :title="title" width="500px" @close="close">
  3. <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
  4. <el-form-item label="标题" prop="title">
  5. <el-input v-model.trim="form.title" clearable />
  6. </el-form-item>
  7. <el-form-item label="作者" prop="author">
  8. <el-input v-model.trim="form.author" clearable />
  9. </el-form-item>
  10. </el-form>
  11. <template #footer>
  12. <el-button type="primary" @click="save">保存</el-button>
  13. </template>
  14. </vab-dialog>
  15. </template>
  16. <script lang="ts" setup>
  17. import type { FormInstance } from 'element-plus'
  18. import { doEdit } from '/@/api/{{ camelCase name }}'
  19. defineOptions({
  20. name: '{{ properCase name }}Edit',
  21. })
  22. const emit = defineEmits(['fetch-data'])
  23. const formRef = ref<FormInstance>()
  24. const title = ref<string>('')
  25. const dialogFormVisible = ref<boolean>(false)
  26. const form = reactive<any>({
  27. title: '',
  28. author: '',
  29. })
  30. const rules = reactive<any>({
  31. title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
  32. author: [{ required: true, trigger: 'blur', message: '请输入作者' }],
  33. })
  34. const showEdit = (row: any) => {
  35. dialogFormVisible.value = true
  36. nextTick(() => {
  37. if (!row) title.value = '添加'
  38. else {
  39. title.value = '编辑'
  40. Object.assign(form, row)
  41. }
  42. })
  43. }
  44. defineExpose({
  45. showEdit,
  46. })
  47. const close = () => {
  48. formRef.value?.clearValidate()
  49. formRef.value?.resetFields()
  50. emit('fetch-data')
  51. }
  52. const save = () => {
  53. formRef.value?.validate(async (valid: any) => {
  54. if (valid) {
  55. const { msg }: any = await doEdit(form)
  56. await $baseMessage(msg, 'success', 'hey')
  57. await close()
  58. dialogFormVisible.value = false
  59. }
  60. })
  61. }
  62. </script>