hookehuyr

fix 操作改变数值时才保存默认值

<!--
* @Date: 2022-08-30 11:34:19
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2024-11-20 17:52:28
* @LastEditTime: 2024-11-22 10:33:52
* @FilePath: /data-table/src/components/CheckboxField/index.vue
* @Description: 多项选择控件
-->
......@@ -104,6 +104,27 @@ const handleEmit = (item) => {
// 发送自定义数据结构
props.item.value = { key: props.item.key, value: checkbox_value.value, affix: affix_value.value, type: "checkbox" };
emit("active", props.item.value);
// 适配cookie保存未完成表单
const checkbox_v = _.cloneDeep(checkbox_value.value)
checkbox_v?.forEach((element, index) => {
for (const key in affix_value.value) {
if (affix_value.value[key] && element === key) {
checkbox_value[index] = affix_value.value[key]
}
}
});
const currentValue = checkbox_v;
const existingCookie = Cookies.get($route.query.code);
if (existingCookie) {
// 如果Cookie存在,更新它
let obj = JSON.parse(existingCookie);
obj[props.item.key] = currentValue; // 替换掉旧值
Cookies.set($route.query.code, JSON.stringify(obj), { expires: 1 });
} else {
// 如果Cookie不存在,新增它
Cookies.set($route.query.code, JSON.stringify({ [props.item.key]: currentValue }), { expires: 1 });
}
}
onMounted(() => {
// TAG:默认值处理
......@@ -153,33 +174,6 @@ onMounted(() => {
props.item.value = { key: props.item.key, value: checkbox_value.value, affix: affix_value.value, type: "checkbox" };
emit("active", props.item.value);
})
// 适配cookie保存未完成表单
watch(
() => props.item.value,
(v) => {
const checkbox_value = _.cloneDeep(v.value)
checkbox_value?.forEach((element, index) => {
for (const key in v.affix) {
if (v.affix[key] && element === key) {
checkbox_value[index] = v.affix[key]
}
}
});
const currentValue = checkbox_value;
const existingCookie = Cookies.get($route.query.code);
if (existingCookie) {
// 如果Cookie存在,更新它
let obj = JSON.parse(existingCookie);
obj[props.item.key] = currentValue; // 替换掉旧值
Cookies.set($route.query.code, JSON.stringify(obj), { expires: 1 });
} else {
// 如果Cookie不存在,新增它
Cookies.set($route.query.code, JSON.stringify({ [props.item.key]: currentValue }), { expires: 1 });
}
}
);
</script>
<style lang="less" scoped>
......
<!--
* @Date: 2022-08-30 11:34:19
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2024-11-20 17:43:24
* @LastEditTime: 2024-11-22 10:31:54
* @FilePath: /data-table/src/components/RadioField/index.vue
* @Description: 单项选择控件
-->
......@@ -113,6 +113,19 @@ const onChange = (item) => {
// 发送自定义数据结构
props.item.value = { key: props.item.key, value: radio_value.value, affix: affix_value.value, type: "radio" };
emit("active", props.item.value);
// 适配cookie保存未完成表单
const currentValue = affix_value.value ? affix_value.value : radio_value.value;
const existingCookie = Cookies.get($route.query.code);
if (existingCookie) {
// 如果Cookie存在,更新它
let obj = JSON.parse(existingCookie);
obj[props.item.key] = currentValue; // 替换掉旧值
Cookies.set($route.query.code, JSON.stringify(obj), { expires: 1 });
} else {
// 如果Cookie不存在,新增它
Cookies.set($route.query.code, JSON.stringify({ [props.item.key]: currentValue }), { expires: 1 });
}
}
const onBlur = (item) => {
clearAffix()
......@@ -174,25 +187,6 @@ const showUrl = (rule) => {
location.href = rule.desc_url
}
const rule_content = ref("");
// 适配cookie保存未完成表单
watch(
() => props.item.value,
(v) => {
const currentValue = v.affix ? v.affix : v.value;
const existingCookie = Cookies.get($route.query.code);
if (existingCookie) {
// 如果Cookie存在,更新它
let obj = JSON.parse(existingCookie);
obj[props.item.key] = currentValue; // 替换掉旧值
Cookies.set($route.query.code, JSON.stringify(obj), { expires: 1 });
} else {
// 如果Cookie不存在,新增它
Cookies.set($route.query.code, JSON.stringify({ [props.item.key]: currentValue }), { expires: 1 });
}
}
);
</script>
<style lang="less" scoped>
......