DialogForm.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <script setup lang="ts">
  2. import { ref, watch } from "vue";
  3. import { ElMessage, FormInstance } from "element-plus";
  4. const SELECT_OPTIONS = [
  5. { label: "网关", value: 1 },
  6. { label: "人工智能", value: 2 },
  7. { label: "CVM", value: 3 },
  8. { label: "防火墙", value: 4 },
  9. { label: "未知", value: 5 }
  10. ];
  11. const props = defineProps({
  12. visible: {
  13. type: Boolean,
  14. default: false
  15. },
  16. data: {
  17. type: Object,
  18. default: () => {
  19. return {};
  20. }
  21. }
  22. });
  23. const ruleFormRef = ref<FormInstance>();
  24. const formVisible = ref(false);
  25. const formData = ref(props.data);
  26. const textareaValue = ref("");
  27. const submitForm = async (formEl: FormInstance | undefined) => {
  28. if (!formEl) return;
  29. await formEl.validate(valid => {
  30. if (valid) {
  31. ElMessage.success("提交成功");
  32. formVisible.value = false;
  33. resetForm(formEl);
  34. }
  35. });
  36. };
  37. const resetForm = (formEl: FormInstance | undefined) => {
  38. if (!formEl) return;
  39. formEl.resetFields();
  40. };
  41. const closeDialog = () => {
  42. formVisible.value = false;
  43. resetForm(ruleFormRef.value);
  44. };
  45. const emit = defineEmits(["update:visible"]);
  46. watch(
  47. () => formVisible.value,
  48. val => {
  49. emit("update:visible", val);
  50. }
  51. );
  52. watch(
  53. () => props.visible,
  54. val => {
  55. formVisible.value = val;
  56. }
  57. );
  58. watch(
  59. () => props.data,
  60. val => {
  61. formData.value = val;
  62. }
  63. );
  64. const rules = {
  65. name: [{ required: true, message: "请输入产品名称", trigger: "blur" }]
  66. };
  67. </script>
  68. <template>
  69. <el-dialog
  70. v-model="formVisible"
  71. title="新建产品"
  72. :width="680"
  73. draggable
  74. :before-close="closeDialog"
  75. >
  76. <!-- 表单内容 -->
  77. <el-form
  78. ref="ruleFormRef"
  79. :model="formData"
  80. :rules="rules"
  81. label-width="100px"
  82. >
  83. <el-form-item label="产品名称" prop="name">
  84. <el-input
  85. v-model="formData.name"
  86. :style="{ width: '480px' }"
  87. placeholder="请输入产品名称"
  88. />
  89. </el-form-item>
  90. <el-form-item label="产品状态" prop="status">
  91. <el-radio-group v-model="formData.status">
  92. <el-radio label="0">已停用</el-radio>
  93. <el-radio label="1">已启用</el-radio>
  94. </el-radio-group>
  95. </el-form-item>
  96. <el-form-item label="产品描述" prop="description">
  97. <el-input
  98. v-model="formData.description"
  99. :style="{ width: '480px' }"
  100. placeholder="请输入产品描述"
  101. />
  102. </el-form-item>
  103. <el-form-item label="产品类型" prop="type">
  104. <el-select
  105. v-model="formData.type"
  106. clearable
  107. :teleported="false"
  108. :style="{ width: '480px' }"
  109. >
  110. <el-option
  111. v-for="(item, index) in SELECT_OPTIONS"
  112. :key="index"
  113. :value="item.value"
  114. :label="item.label"
  115. >
  116. {{ item.label }}
  117. </el-option>
  118. </el-select>
  119. </el-form-item>
  120. <el-form-item label="备注" prop="mark">
  121. <el-input
  122. v-model="textareaValue"
  123. type="textarea"
  124. :style="{ width: '480px' }"
  125. placeholder="请输入内容"
  126. />
  127. </el-form-item>
  128. </el-form>
  129. <template #footer>
  130. <el-button @click="closeDialog">取消</el-button>
  131. <el-button type="primary" @click="submitForm(ruleFormRef)"
  132. >确定</el-button
  133. >
  134. </template>
  135. </el-dialog>
  136. </template>