sensorCat.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <el-dialog
  3. :title="title"
  4. :visible.sync="dialogVisible"
  5. width="30%"
  6. >
  7. <div>
  8. <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="100px" label-position="right">
  9. <el-form-item label="类别名称" prop="sensorTypeName">
  10. <el-input v-model="formData.sensorTypeName" placeholder="输入设备类别名称" />
  11. </el-form-item>
  12. <el-form-item label="说明">
  13. <el-input v-model="formData.sensorCatDesc" type="textarea" rows="5" placeholder="输入说明" />
  14. </el-form-item>
  15. </el-form>
  16. </div>
  17. <span slot="footer" class="dialog-footer">
  18. <el-button @click="dialogVisible = false">取消</el-button>
  19. <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
  20. </span>
  21. </el-dialog>
  22. </template>
  23. <script>
  24. import { ACTION_ADD, ACTION_UPDATE } from '@/utils/actionType'
  25. import { createSensorCat, updateSensorCat } from '@/api/goaf/sensorCatApi'
  26. export default {
  27. name: 'SensorCat',
  28. props: {
  29. title: {
  30. type: String,
  31. default: ''
  32. }
  33. },
  34. data() {
  35. return {
  36. dialogVisible: false,
  37. actionType: '',
  38. formData: {
  39. sensorTypeId: undefined,
  40. sensorTypeName: '',
  41. sensorCatDesc: ''
  42. },
  43. rules: {
  44. sensorTypeName: [
  45. { required: true, message: '请填写名称', trigger: 'blur' }
  46. ]
  47. }
  48. }
  49. },
  50. methods: {
  51. formSuccess() {
  52. this.$emit('formSuccess')
  53. },
  54. // Show Add Model
  55. showAddModel() {
  56. this.actionType = ACTION_ADD
  57. this.resetFormData()
  58. this.dialogVisible = true
  59. },
  60. // Show Edit Model
  61. showEditModel(data) {
  62. this.actionType = ACTION_UPDATE
  63. this.resetFormData()
  64. this.dialogVisible = true
  65. this.formData = data
  66. },
  67. // Reset Form Data
  68. resetFormData() {
  69. this.formData = {
  70. sensorTypeId: undefined,
  71. sensorTypeName: '',
  72. sensorCatDesc: ''
  73. }
  74. },
  75. // Submit
  76. submitForm(formName) {
  77. this.$refs[formName].validate((valid) => {
  78. if (valid) {
  79. this.handleActionCommand()
  80. } else {
  81. console.log('error submit!!')
  82. return false
  83. }
  84. })
  85. },
  86. // Handle ACTION Command
  87. handleActionCommand() {
  88. const actionType = this.actionType
  89. switch (actionType) {
  90. case ACTION_ADD:
  91. createSensorCat(this.formData).then((resp) => {
  92. const { code, msg } = resp
  93. if (code === 0) {
  94. this.dialogVisible = false
  95. this.$message.success(msg)
  96. this.formSuccess()
  97. } else {
  98. this.$message.error(msg)
  99. }
  100. })
  101. break
  102. case ACTION_UPDATE:
  103. updateSensorCat(this.formData).then((resp) => {
  104. console.log('this.formData', this.formData)
  105. const { code, msg } = resp
  106. if (code === 0) {
  107. this.dialogVisible = false
  108. this.$message.success(msg)
  109. this.formSuccess()
  110. } else {
  111. this.$message.error(msg)
  112. }
  113. })
  114. break
  115. }
  116. }
  117. }
  118. }
  119. </script>