可以不选择的el-radio单选框
# 可以不选择的el-radio单选框
这个组件比较宽泛,主要是异常和正常两个选项,所以写成了这样
写的不是很好,可以借鉴
有待优化
<template>
<span>
<el-radio v-model="isNormal" :label="true" @click.native.prevent="toggleNormal"
class="is-focus">正常</el-radio>
<el-radio v-model="isAbnormal" :label="true" @click.native.prevent="toggleAbnormal"
class="is-focus">异常</el-radio>
</span>
</template>
<script>
export default {
name: 'contentRadioGroup',
props: {
state: {
type: Number,
default: -1
}
},
data() {
return {
flag: false,
isNormal: false,
isAbnormal: false
};
},
methods: {
changed() {
let state;
if (!this.isNormal && !this.isAbnormal) {
state = -1;
} else if (this.isNormal && !this.isAbnormal) {
state = 0;
} else if (!this.isNormal && this.isAbnormal) {
state = 1;
}
this.$emit('update:state', state);
this.$emit('change', state);
},
toggleNormal() {
this.isNormal = !this.isNormal;
if (this.isNormal && this.isAbnormal) {
this.isAbnormal = false;
}
this.changed();
},
toggleAbnormal() {
this.isAbnormal = !this.isAbnormal;
if (this.isAbnormal && this.isNormal) {
this.isNormal = false;
}
this.changed();
}
},
watch: {
state: {
handler: function (val) {
if (val === -1) {
this.isNormal = false;
this.isAbnormal = false;
} else if (val === 0) {
this.isNormal = true;
this.isAbnormal = false;
} else if (val === 1) {
this.isNormal = false;
this.isAbnormal = true;
}
},
immediate: true
}
}
};
</script>
<style scoped>
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
上次更新: 2023/06/01, 12:40:50