houzhongjian
2024-07-11 759b1c71011abd6b58c37d2566f3f3c208c2f1b2
提交 | 用户 | 时间
759b1c 1 <template>
H 2   <div>
3     <el-dialog
4       v-bind="$attrs"
5       :close-on-click-modal="false"
6       :modal-append-to-body="false"
7       v-on="$listeners"
8       @open="onOpen"
9       @close="onClose"
10     >
11       <el-row :gutter="0">
12         <el-form
13           ref="elForm"
14           :model="formData"
15           :rules="rules"
16           size="small"
17           label-width="100px"
18         >
19           <el-col :span="24">
20             <el-form-item
21               label="选项名"
22               prop="label"
23             >
24               <el-input
25                 v-model="formData.label"
26                 placeholder="请输入选项名"
27                 clearable
28               />
29             </el-form-item>
30           </el-col>
31           <el-col :span="24">
32             <el-form-item
33               label="选项值"
34               prop="value"
35             >
36               <el-input
37                 v-model="formData.value"
38                 placeholder="请输入选项值"
39                 clearable
40               >
41                 <el-select
42                   slot="append"
43                   v-model="dataType"
44                   :style="{width: '100px'}"
45                 >
46                   <el-option
47                     v-for="(item, index) in dataTypeOptions"
48                     :key="index"
49                     :label="item.label"
50                     :value="item.value"
51                     :disabled="item.disabled"
52                   />
53                 </el-select>
54               </el-input>
55             </el-form-item>
56           </el-col>
57         </el-form>
58       </el-row>
59       <div slot="footer">
60         <el-button
61           type="primary"
62           @click="handelConfirm"
63         >
64           确定
65         </el-button>
66         <el-button @click="close">
67           取消
68         </el-button>
69       </div>
70     </el-dialog>
71   </div>
72 </template>
73 <script>
74 import { isNumberStr } from '@/utils'
75 import { getTreeNodeId, saveTreeNodeId } from '@/utils/db'
76
77 const id = getTreeNodeId()
78
79 export default {
80   components: {},
81   inheritAttrs: false,
82   props: [],
83   data() {
84     return {
85       id,
86       formData: {
87         label: undefined,
88         value: undefined
89       },
90       rules: {
91         label: [
92           {
93             required: true,
94             message: '请输入选项名',
95             trigger: 'blur'
96           }
97         ],
98         value: [
99           {
100             required: true,
101             message: '请输入选项值',
102             trigger: 'blur'
103           }
104         ]
105       },
106       dataType: 'string',
107       dataTypeOptions: [
108         {
109           label: '字符串',
110           value: 'string'
111         },
112         {
113           label: '数字',
114           value: 'number'
115         }
116       ]
117     }
118   },
119   computed: {},
120   watch: {
121     // eslint-disable-next-line func-names
122     'formData.value': function (val) {
123       this.dataType = isNumberStr(val) ? 'number' : 'string'
124     },
125     id(val) {
126       saveTreeNodeId(val)
127     }
128   },
129   created() {},
130   mounted() {},
131   methods: {
132     onOpen() {
133       this.formData = {
134         label: undefined,
135         value: undefined
136       }
137     },
138     onClose() {},
139     close() {
140       this.$emit('update:visible', false)
141     },
142     handelConfirm() {
143       this.$refs.elForm.validate(valid => {
144         if (!valid) return
145         if (this.dataType === 'number') {
146           this.formData.value = parseFloat(this.formData.value)
147         }
148         this.formData.id = this.id++
149         this.$emit('commit', this.formData)
150         this.close()
151       })
152     }
153   }
154 }
155 </script>
156
157 <style lang="scss" scoped>
158 </style>