hookehuyr

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

1 <!-- 1 <!--
2 * @Date: 2022-08-30 11:34:19 2 * @Date: 2022-08-30 11:34:19
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2024-11-20 17:52:28 4 + * @LastEditTime: 2024-11-22 10:33:52
5 * @FilePath: /data-table/src/components/CheckboxField/index.vue 5 * @FilePath: /data-table/src/components/CheckboxField/index.vue
6 * @Description: 多项选择控件 6 * @Description: 多项选择控件
7 --> 7 -->
...@@ -104,6 +104,27 @@ const handleEmit = (item) => { ...@@ -104,6 +104,27 @@ const handleEmit = (item) => {
104 // 发送自定义数据结构 104 // 发送自定义数据结构
105 props.item.value = { key: props.item.key, value: checkbox_value.value, affix: affix_value.value, type: "checkbox" }; 105 props.item.value = { key: props.item.key, value: checkbox_value.value, affix: affix_value.value, type: "checkbox" };
106 emit("active", props.item.value); 106 emit("active", props.item.value);
107 + // 适配cookie保存未完成表单
108 + const checkbox_v = _.cloneDeep(checkbox_value.value)
109 + checkbox_v?.forEach((element, index) => {
110 + for (const key in affix_value.value) {
111 + if (affix_value.value[key] && element === key) {
112 + checkbox_value[index] = affix_value.value[key]
113 + }
114 + }
115 + });
116 + const currentValue = checkbox_v;
117 + const existingCookie = Cookies.get($route.query.code);
118 +
119 + if (existingCookie) {
120 + // 如果Cookie存在,更新它
121 + let obj = JSON.parse(existingCookie);
122 + obj[props.item.key] = currentValue; // 替换掉旧值
123 + Cookies.set($route.query.code, JSON.stringify(obj), { expires: 1 });
124 + } else {
125 + // 如果Cookie不存在,新增它
126 + Cookies.set($route.query.code, JSON.stringify({ [props.item.key]: currentValue }), { expires: 1 });
127 + }
107 } 128 }
108 onMounted(() => { 129 onMounted(() => {
109 // TAG:默认值处理 130 // TAG:默认值处理
...@@ -153,33 +174,6 @@ onMounted(() => { ...@@ -153,33 +174,6 @@ onMounted(() => {
153 props.item.value = { key: props.item.key, value: checkbox_value.value, affix: affix_value.value, type: "checkbox" }; 174 props.item.value = { key: props.item.key, value: checkbox_value.value, affix: affix_value.value, type: "checkbox" };
154 emit("active", props.item.value); 175 emit("active", props.item.value);
155 }) 176 })
156 -
157 -// 适配cookie保存未完成表单
158 -watch(
159 - () => props.item.value,
160 - (v) => {
161 - const checkbox_value = _.cloneDeep(v.value)
162 - checkbox_value?.forEach((element, index) => {
163 - for (const key in v.affix) {
164 - if (v.affix[key] && element === key) {
165 - checkbox_value[index] = v.affix[key]
166 - }
167 - }
168 - });
169 - const currentValue = checkbox_value;
170 - const existingCookie = Cookies.get($route.query.code);
171 -
172 - if (existingCookie) {
173 - // 如果Cookie存在,更新它
174 - let obj = JSON.parse(existingCookie);
175 - obj[props.item.key] = currentValue; // 替换掉旧值
176 - Cookies.set($route.query.code, JSON.stringify(obj), { expires: 1 });
177 - } else {
178 - // 如果Cookie不存在,新增它
179 - Cookies.set($route.query.code, JSON.stringify({ [props.item.key]: currentValue }), { expires: 1 });
180 - }
181 - }
182 - );
183 </script> 177 </script>
184 178
185 <style lang="less" scoped> 179 <style lang="less" scoped>
......
1 <!-- 1 <!--
2 * @Date: 2022-08-30 11:34:19 2 * @Date: 2022-08-30 11:34:19
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2024-11-20 17:43:24 4 + * @LastEditTime: 2024-11-22 10:31:54
5 * @FilePath: /data-table/src/components/RadioField/index.vue 5 * @FilePath: /data-table/src/components/RadioField/index.vue
6 * @Description: 单项选择控件 6 * @Description: 单项选择控件
7 --> 7 -->
...@@ -113,6 +113,19 @@ const onChange = (item) => { ...@@ -113,6 +113,19 @@ const onChange = (item) => {
113 // 发送自定义数据结构 113 // 发送自定义数据结构
114 props.item.value = { key: props.item.key, value: radio_value.value, affix: affix_value.value, type: "radio" }; 114 props.item.value = { key: props.item.key, value: radio_value.value, affix: affix_value.value, type: "radio" };
115 emit("active", props.item.value); 115 emit("active", props.item.value);
116 + // 适配cookie保存未完成表单
117 + const currentValue = affix_value.value ? affix_value.value : radio_value.value;
118 + const existingCookie = Cookies.get($route.query.code);
119 +
120 + if (existingCookie) {
121 + // 如果Cookie存在,更新它
122 + let obj = JSON.parse(existingCookie);
123 + obj[props.item.key] = currentValue; // 替换掉旧值
124 + Cookies.set($route.query.code, JSON.stringify(obj), { expires: 1 });
125 + } else {
126 + // 如果Cookie不存在,新增它
127 + Cookies.set($route.query.code, JSON.stringify({ [props.item.key]: currentValue }), { expires: 1 });
128 + }
116 } 129 }
117 const onBlur = (item) => { 130 const onBlur = (item) => {
118 clearAffix() 131 clearAffix()
...@@ -174,25 +187,6 @@ const showUrl = (rule) => { ...@@ -174,25 +187,6 @@ const showUrl = (rule) => {
174 location.href = rule.desc_url 187 location.href = rule.desc_url
175 } 188 }
176 const rule_content = ref(""); 189 const rule_content = ref("");
177 -
178 -// 适配cookie保存未完成表单
179 -watch(
180 - () => props.item.value,
181 - (v) => {
182 - const currentValue = v.affix ? v.affix : v.value;
183 - const existingCookie = Cookies.get($route.query.code);
184 -
185 - if (existingCookie) {
186 - // 如果Cookie存在,更新它
187 - let obj = JSON.parse(existingCookie);
188 - obj[props.item.key] = currentValue; // 替换掉旧值
189 - Cookies.set($route.query.code, JSON.stringify(obj), { expires: 1 });
190 - } else {
191 - // 如果Cookie不存在,新增它
192 - Cookies.set($route.query.code, JSON.stringify({ [props.item.key]: currentValue }), { expires: 1 });
193 - }
194 - }
195 - );
196 </script> 190 </script>
197 191
198 <style lang="less" scoped> 192 <style lang="less" scoped>
......