Browse Source

switch组件修改-change事件没有同步修改绑定值问题

pull/97/head
zq 1 month ago
parent
commit
702e178d9d
  1. 115
      src/components/GuipSwitch.vue
  2. 35
      src/views/elementGroups.vue

115
src/components/GuipSwitch.vue

@ -1,13 +1,14 @@
<template> <template>
<el-form-item> <el-form-item :prop="prop" :label="label" :rules="rules">
<div class="switchWrap"> <div class="switchWrap">
<span :class="['switchDesc',{'fl':float =='left'},{'fr':float =='right'}]" v-if="activeText || inactiveText"> <span :class="['switchDesc', { 'fl': float == 'left' }, { 'fr': float == 'right' }]"
v-if="activeText || inactiveText">
{{ internalValue === activeValue ? activeText : inactiveText {{ internalValue === activeValue ? activeText : inactiveText
}}</span> }}</span>
<el-switch v-model="internalValue" :active-color="activeColor" :inactive-color="inactiveColor" v-bind="$attrs" <el-switch v-model="internalValue" :active-color="activeColor" :inactive-color="inactiveColor"
:disabled="disabled || loading" :active-value="activeValue" v-bind="$attrs" :disabled="disabled" :active-value="activeValue" :inactive-value="inactiveValue"
:inactive-value="inactiveValue" @change="handleChange"> @change="handleChange">
<!-- 自定义开启时的图标 --> <!-- 自定义开启时的图标 -->
<template #active-icon> <template #active-icon>
</template> </template>
@ -17,18 +18,20 @@
</el-switch> </el-switch>
</div> </div>
</el-form-item> </el-form-item>
</template> </template>
<!-- 他这个描述文案不太符合当前使用暂且不用 -->
<!-- :active-text="activeText"
:inactive-text="inactiveText" -->
<!-- 设置active-value和inactive-value属性接受Boolean, String或Number类型的值 -->
<script> <script>
export default { export default {
name: 'CustomSwitch', name: 'GuipSwitchå',
inheritAttrs: false,
props: { props: {
// modelValue: { type: [Boolean, String, Number], default: undefined },
value: { type: [Boolean, String, Number], default: undefined },
prop: String,
label: String,
rules: Array,
labelWidth: String,
activeText: { activeText: {
type: String, type: String,
default: '', default: '',
@ -37,17 +40,13 @@ export default {
type: String, type: String,
default: '', default: '',
}, },
modelValue: {
type: [Boolean,String,Number],
default: false,
},
activeValue: { activeValue: {
type: [Boolean,String,Number], type: [Boolean, String, Number],
default: '', default: true,
}, },
inactiveValue: { inactiveValue: {
type: [Boolean,String,Number], type: [Boolean, String, Number],
default: '', default: false,
}, },
activeColor: { activeColor: {
type: String, type: String,
@ -66,58 +65,64 @@ export default {
default: false, default: false,
}, },
}, },
emits: ['update:modelValue', 'change'],
data() { data() {
return { return {
internalValue: this.modelValue, internalValue: this.value,
loading: false, // errorMsg: ''
}; }
},
computed: {
// internalValue: {
// get() {
// return this.modelValue !== undefined ? this.modelValue : this.value
// },
// set(val) {
// this.$emit('update:modelValue', val)
// this.$emit('input', val)
// }
// }
}, },
watch: { watch: {
value(newVal) { value(newVal) {
this.internalValue = newVal; this.internalValue = newVal
},
modelValue(newVal) {
this.internalValue = newVal;
},
internalValue(newVal) {
this.internalValue = newVal;
this.$emit('input', newVal);
}, },
// modelValue(newVal) {
// this.internalValue = newVal
// },
}, },
methods: { methods: {
// switch handleChange(val) {
async handleChange(value) { // this.$emit('update:modelValue', val)
this.loading = true; this.$emit('input', val)
try { this.$emit('change', val)
// this.validateField()
await new Promise((resolve) => setTimeout(resolve, 100));
this.internalValue = value;
this.$emit('update:modelValue', value);
this.$emit('change', value);
} catch (error) {
console.error('操作失败:', error);
} finally {
this.loading = false;
}
}, },
}, validateField() {
components: { if (this.prop && this.$parent.validateField) {
}, this.$parent.validateField(this.prop, (error) => {
}; this.errorMsg = error || ''
})
}
}
}
}
</script> </script>
<style scoped> <style scoped>
.switchWrap { .switchWrap {
/* align-items: center; */ /* align-items: center; */
} }
.fl{
.fl {
float: left; float: left;
margin-right: 12px; margin-right: 12px;
} }
.fr{
.fr {
float: right; float: right;
margin-left: 12px; margin-left: 12px;
} }
.switchDesc { .switchDesc {
font-size: 12px; font-size: 12px;
font-weight: normal; font-weight: normal;
@ -128,4 +133,10 @@ export default {
color: #626573; color: #626573;
display: inline-block; display: inline-block;
} }
.error-msg {
color: #f56c6c;
font-size: 12px;
margin-left: 10px;
}
</style> </style>

35
src/views/elementGroups.vue

@ -133,9 +133,11 @@
</el-table-column> </el-table-column>
<el-table-column prop="name2" label="姓名" width="150"></el-table-column> <el-table-column prop="name2" label="姓名" width="150"></el-table-column>
<el-table-column prop="price" label="价格" width="150"> <el-table-column prop="statu" label="价格" width="150">
<template slot-scope="scope"> <template slot-scope="scope">
<span @click="handlePriceClick(scope.row)">{{ scope.row.price }}</span> <GuipSwitch v-model="scope.row.statu" activeText="默认类型boolean" inactiveText="关闭" :active-value="1" :inactive-value="0"
@change="onSwitchChange1(scope.row)">
</GuipSwitch>
</template> </template>
</el-table-column> </el-table-column>
<el-table-column prop="address" label="地址1" width="150"> </el-table-column> <el-table-column prop="address" label="地址1" width="150"> </el-table-column>
@ -373,7 +375,7 @@
<label for="">开关L</label> <label for="">开关L</label>
<!-- active-value 开启状态的值 --> <!-- active-value 开启状态的值 -->
<!-- inactive-value 关闭状态的值 --> <!-- inactive-value 关闭状态的值 -->
<GuipSwitch :modelValue="switchValue" activeText="默认类型boolean" inactiveText="关闭" <GuipSwitch v-model="form.switchValue3" activeText="默认类型boolean" inactiveText="关闭" :active-value="true" :inactive-value="false"
@change="onSwitchChange"> @change="onSwitchChange">
</GuipSwitch> </GuipSwitch>
</div> </div>
@ -386,12 +388,13 @@
</div> </div>
<div class="ele-item"> <div class="ele-item">
<label for="">开关L</label> <label for="">开关L</label>
<GuipSwitch :modelValue="switchValue1" :active-value="1" :inactive-value="0" activeText="number" <GuipSwitch v-model="switchValue1" :active-value="1" :inactive-value="0" activeText="number"
@change="onSwitchChange"> @change="onSwitchChange">
</GuipSwitch> </GuipSwitch>
<GuipSwitch :modelValue="switchValue2" active-value="0" inactive-value="1" activeText="string" <GuipSwitch v-model="form.switchValue2" active-value="0" inactive-value="1" activeText="string"
@change="onSwitchChange"> @change="onSwitchChange">
</GuipSwitch> </GuipSwitch>
<p>当前值switchValue2{{ this.form.switchValue2 }}</p>
</div> </div>
<div class="flex ele-item"> <div class="flex ele-item">
<label for="">inputdrop组合使用(默认使用)</label> <label for="">inputdrop组合使用(默认使用)</label>
@ -691,6 +694,7 @@ export default {
timer: null, timer: null,
date1: '', date1: '',
switchValue: true, switchValue: true,
switchValue3: true,
switchValue1: 1, switchValue1: 1,
switchValue2: '0', switchValue2: '0',
dialogVisible1: false, dialogVisible1: false,
@ -701,6 +705,8 @@ export default {
form: { form: {
username: '', username: '',
language: '', language: '',
switchValue3:true,
switchValue2: '0',
domain_set: '', domain_set: '',
domainSuffix: '11', domainSuffix: '11',
domainSuffix1: '.chachongz.com', domainSuffix1: '.chachongz.com',
@ -775,6 +781,7 @@ export default {
address: '上海市普陀区金沙江路 1518 弄', address: '上海市普陀区金沙江路 1518 弄',
price: '20', price: '20',
age: 20, age: 20,
statu:1,
// //
edit_name: '王小虎', edit_address: '上海市普陀区金沙江路 1518 弄' edit_name: '王小虎', edit_address: '上海市普陀区金沙江路 1518 弄'
}, { }, {
@ -783,6 +790,8 @@ export default {
address: '上海市普陀区金沙江路 151811 弄', address: '上海市普陀区金沙江路 151811 弄',
price: '10', price: '10',
age: 30, age: 30,
statu:1,
edit_name: '王小虎11', edit_address: '上海市普陀区金沙江路 151811 弄' edit_name: '王小虎11', edit_address: '上海市普陀区金沙江路 151811 弄'
}, { }, {
@ -790,6 +799,7 @@ export default {
name: '王小虎', name: '王小虎',
address: '上海市普陀区金沙江路 1518 弄', address: '上海市普陀区金沙江路 1518 弄',
price: '200', price: '200',
statu:0,
}, { }, {
date: '2016-05-01', date: '2016-05-01',
name: '王小虎', name: '王小虎',
@ -978,6 +988,10 @@ export default {
} }
}, },
methods: { methods: {
switchChange(value){
console.log(value,'====value');
console.log(this.switchValue1,'===uuuu');
},
handleEvent(data){ handleEvent(data){
console.log(data,'data') console.log(data,'data')
}, },
@ -1124,10 +1138,17 @@ export default {
return randomNumber return randomNumber
}, },
onSwitchChange(value) { onSwitchChange(value) {
console.log('Switch 状态变化:', this.switchValue, value); console.log('Switch/ 状态变化:',this.switchValue, value);
// console.log('Switch :',this.form.switchValue2,this.form.switchValue3, value);
},
onSwitchChange1(value) {
//
// console.log(this.tableData2,value,'this.tableData2===');
console.log('Switch/ 状态变化:', value.statu);
}, },
// ---start // ---start
openDialog() { openDialog() {
console.log(this.switchValue1,'switchValue1');
this.dialogVisible = true; this.dialogVisible = true;
}, },
openDialog1() { openDialog1() {
@ -1188,7 +1209,7 @@ export default {
console.log(data, 'radio--data'); console.log(data, 'radio--data');
}, },
selectChangeTest(val){ selectChangeTest(val){
console.log(val,'select-----选中的'); console.log(val,'select-----选中的',this.form.card,this.form.card1);
}, },
handleClick(row) { handleClick(row) {
console.log(row); console.log(row);

Loading…
Cancel
Save