EditCallingsForm.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <script setup lang="ts">
  2. import { ElDialog, ElForm, ElFormItem, ElInput, ElDatePicker, ElButton, ElMessage, ElRadio } from 'element-plus'
  3. import type { FormInstance, FormRules } from 'element-plus'
  4. import { updateCallings } from '@/api/modules/anycallService'
  5. // 定义表单数据类型
  6. type IFormData = {
  7. callings: string
  8. }
  9. // 定义组件的属性
  10. interface Props {
  11. cloneId: string
  12. visible: boolean
  13. modelValue?: IFormData | null
  14. mode?: 'create' | 'edit'
  15. }
  16. // 定义组件的事件
  17. interface Emits {
  18. (e: 'update:visible', value: boolean): void
  19. (e: 'cancel'): void
  20. (e: 'refresh'): void
  21. }
  22. // 接收属性和事件
  23. const props = withDefaults(defineProps<Props>(), {
  24. visible: false,
  25. modelValue: null,
  26. mode: 'create'
  27. })
  28. const emit = defineEmits<Emits>()
  29. // 创建本地响应式变量用于 v-model 绑定
  30. const dialogVisible = ref(props.visible)
  31. // 监听 props.visible 变化,更新本地变量
  32. watch(() => props.visible, (newVisible) => {
  33. dialogVisible.value = newVisible
  34. })
  35. // 监听本地变量变化,发出 update 事件
  36. watch(dialogVisible, (newVisible) => {
  37. emit('update:visible', newVisible)
  38. })
  39. // 表单引用
  40. const editFormRef = ref<FormInstance>();
  41. // 响应式数据 - 直接定义所有必需字段
  42. const formData = ref<Partial<IFormData>>({
  43. callings: '',
  44. });
  45. // 监听 props.modelValue 变化
  46. watch(() => props.modelValue, (newModelValue) => {
  47. if (newModelValue) {
  48. formData.value = {
  49. ...formData.value,
  50. ...newModelValue
  51. };
  52. console.log(newModelValue, formData.value,33344)
  53. }
  54. });
  55. // 表单验证规则
  56. const formRules = ref<FormRules>({
  57. callings: [
  58. { required: true, message: '输入数量', trigger: 'blur' },
  59. ],
  60. })
  61. // 监听可见性变化
  62. watch(() => props.visible, (newVisible) => {
  63. resetForm()
  64. })
  65. // 重置表单
  66. function resetForm() {
  67. console.log('reset')
  68. if (editFormRef.value) {
  69. editFormRef.value.resetFields()
  70. }
  71. formData.value = {
  72. callings: '',
  73. }
  74. }
  75. // 处理确认
  76. async function handleConfirm() {
  77. if (!editFormRef.value) return
  78. try {
  79. await editFormRef.value.validate()
  80. const { code } = await updateCallings({
  81. cloneId: props.cloneId,
  82. callings: formData.value.callings || ''
  83. })
  84. if (code === 0) {
  85. ElMessage.success('操作成功')
  86. emit('refresh')
  87. }
  88. // 关闭对话框
  89. emit('update:visible', false)
  90. } catch (error) {
  91. // 验证失败,不做处理
  92. }
  93. }
  94. function handleCancel() {
  95. emit('cancel')
  96. emit('update:visible', false)
  97. }
  98. function handleClose() {
  99. emit('update:visible', false)
  100. }
  101. </script>
  102. <template>
  103. <div>
  104. <ElDialog title="编辑Callings" v-model="dialogVisible" align-center @close="handleClose"
  105. width="400" :z-index="2000">
  106. <ElForm ref="editFormRef" :model="formData" :rules="formRules" label-width="120px" class="mt-4 space-y-4 w-full">
  107. <ElFormItem label="callings" prop="callings" label-width="120">
  108. <ElInput type="number" v-model.number="formData.callings" />
  109. </ElFormItem>
  110. </ElForm>
  111. <template #footer>
  112. <div class="flex justify-end space-x-2">
  113. <ElButton @click="handleCancel">取消</ElButton>
  114. <ElButton type="primary" @click="handleConfirm">确定</ElButton>
  115. </div>
  116. </template>
  117. </ElDialog>
  118. </div>
  119. </template>