form.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <xn-form-container
  3. :title="formData.id ? '编辑监控对象' : '新增监控对象'"
  4. :width="'500px'"
  5. :visible="visible"
  6. :destroy-on-close="true"
  7. @close="onClose"
  8. >
  9. <a-form
  10. ref="formRef"
  11. :model="formData"
  12. :rules="formRules"
  13. layout="inline"
  14. :label-col="{ style: { width: '100px', justifyContent: 'end' } }"
  15. >
  16. <a-form-item label="对象名称" name="name">
  17. <a-input v-model:value="formData.name" placeholder="请输入对象名称" allow-clear />
  18. </a-form-item>
  19. <a-form-item label="对象状态" name="status">
  20. <a-select ref="select" v-model:value="formData.status" placeholder="请选择对象状态">
  21. <a-select-option value="1">正常</a-select-option>
  22. <a-select-option value="2">停用</a-select-option>
  23. </a-select>
  24. </a-form-item>
  25. <a-form-item label="监控位置区域">
  26. <a-input v-model:value="formData.monitorPoint" placeholder="请输入监控位置区域" allow-clear />
  27. </a-form-item>
  28. </a-form>
  29. <template #footer>
  30. <a-button class="xn-mr8" @click="onClose">退出</a-button>
  31. <a-button type="primary" @click="onSubmit">保存</a-button>
  32. </template>
  33. </xn-form-container>
  34. </template>
  35. <script setup>
  36. import { required } from '@/utils/formRules'
  37. import setupApi from '@/api/basicset/setupApi'
  38. // 默认是关闭状态
  39. const visible = ref(false)
  40. const emit = defineEmits({ successful: null })
  41. const formRef = ref()
  42. // 默认要校验的
  43. const formRules = {
  44. name: [required('请输入对象名称')],
  45. status: [required('请选择对象状态')]
  46. }
  47. // 表单数据
  48. const formData = ref({})
  49. // 打开抽屉
  50. const onOpen = (record) => {
  51. visible.value = true
  52. if (record) {
  53. formData.value = Object.assign({}, record)
  54. } else {
  55. formData.value = {}
  56. }
  57. }
  58. // 关闭抽屉
  59. const onClose = () => {
  60. formRef.value.resetFields()
  61. visible.value = false
  62. }
  63. // 验证并提交数据
  64. const onSubmit = () => {
  65. formRef.value
  66. .validate()
  67. .then(() => {
  68. setupApi.submitForm(formData.value, formData.value.id).then(() => {
  69. onClose()
  70. emit('successful')
  71. })
  72. })
  73. .catch((error) => {
  74. console.log(error)
  75. })
  76. }
  77. // 调用这个函数将子组件的一些数据和方法暴露出去
  78. defineExpose({
  79. onOpen
  80. })
  81. </script>
  82. <style lang="less" scoped>
  83. :deep(.ant-form-item) {
  84. width: 100%;
  85. margin-bottom: 10px;
  86. }
  87. </style>