hookehuyr

单选多选控件新增补充选项功能完善

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: 2023-01-30 14:23:30 4 + * @LastEditTime: 2023-02-01 00:19:34
5 * @FilePath: /data-table/src/components/CheckboxField/index.vue 5 * @FilePath: /data-table/src/components/CheckboxField/index.vue
6 * @Description: 多项选择控件 6 * @Description: 多项选择控件
7 --> 7 -->
...@@ -15,26 +15,33 @@ ...@@ -15,26 +15,33 @@
15 </span> 15 </span>
16 </div> 16 </div>
17 <div v-if="item.component_props.note" class="note" v-html="item.component_props.note" /> 17 <div v-if="item.component_props.note" class="note" v-html="item.component_props.note" />
18 - <van-field :name="item.key" :rules="item.rules" :border="false"> 18 + <van-field :rules="item.rules" :border="false">
19 <template #input> 19 <template #input>
20 <van-checkbox-group 20 <van-checkbox-group
21 - v-model="item.value" 21 + v-model="checkbox_value"
22 - @change="onChange(item)"
23 :direction="item.component_props.direction" 22 :direction="item.component_props.direction"
24 :max="item.component_props.max" 23 :max="item.component_props.max"
25 style="width: 100%" 24 style="width: 100%"
26 > 25 >
26 + <template v-for="x in item.component_props.options" :key="x.title">
27 <van-checkbox 27 <van-checkbox
28 - v-for="x in item.component_props.options" 28 + @click="onClick(x)"
29 - :key="x.value" 29 + :name="x.title"
30 - :name="x.value"
31 icon-size="1rem" 30 icon-size="1rem"
32 shape="square" 31 shape="square"
33 :checked-color="themeVars.radioColor" 32 :checked-color="themeVars.radioColor"
34 style="margin-bottom: 0.25rem" 33 style="margin-bottom: 0.25rem"
35 >{{ x.title }}</van-checkbox 34 >{{ x.title }}</van-checkbox
36 > 35 >
37 - <van-field v-if="has_add_info" :name="add_info_name" v-model="add_info" label="" placeholder="请输入补充信息" style="border: 1px solid #eaeaea;border-radius: 0.25rem; padding: 0.25rem 0.5rem;" /> 36 + <van-field
37 + v-if="checkbox_value.includes(x.value)"
38 + @blur="onBlur(x)"
39 + v-model="x.affix"
40 + label=""
41 + placeholder="请输入补充信息"
42 + style="border: 1px solid #eaeaea;border-radius: 0.25rem; padding: 0.25rem 0.5rem;"
43 + />
44 + </template>
38 </van-checkbox-group> 45 </van-checkbox-group>
39 </template> 46 </template>
40 </van-field> 47 </van-field>
...@@ -59,17 +66,34 @@ onMounted(() => { ...@@ -59,17 +66,34 @@ onMounted(() => {
59 const HideShow = computed(() => { 66 const HideShow = computed(() => {
60 return !props.item.component_props.disabled 67 return !props.item.component_props.disabled
61 }) 68 })
62 -// TEST: 测试新功能:选择其他选项时,下方出现输入框,如果其他项被选中,输入框值为最终录入值。 69 +
63 -// 绑定值发生变化时回调,处理选项为其他时的输入项录入 70 +const emit = defineEmits(["active"]);
64 -const has_add_info = ref(true); // TODO: 文字不一定是其他,后续可能需要字段绑定一个值,标识是否有其他输入框进行判断 71 +const checkbox_value = ref(props.item.component_props.default);
65 -const add_info = ref(''); 72 +const affix_value = ref({});
66 -const add_info_name = ref(props.item.key + '#'); 73 +
67 -const add_info_key = ref('其他'); // TODO: 以后动态获取 74 +const onClick = (item) => {
68 -const onChange = (item) => { 75 + item.checked = !item.checked;
69 - console.warn(item); 76 + handleEmit(item)
77 +}
78 +const onBlur = (item) => {
79 + handleEmit(item)
80 +}
81 +const handleEmit = (item) => {
82 + // 选中状态添加属性
83 + if (item.checked) {
84 + affix_value.value[item.value] = item.affix
85 + } else {
86 + // 为选中删除属性
87 + delete affix_value.value[item.value]
88 + }
89 + // 发送自定义数据结构
90 + props.item.value = { key: props.item.key, value: checkbox_value.value, affix: affix_value.value, type: "checkbox" };
91 + emit("active", props.item.value);
70 } 92 }
71 onMounted(() => { 93 onMounted(() => {
72 - add_info_name.value = `${props.item.key}#${add_info_key.value}` 94 + // 发送自定义数据结构
95 + props.item.value = { key: props.item.key, value: checkbox_value.value, affix: affix_value.value, type: "checkbox" };
96 + emit("active", props.item.value);
73 }) 97 })
74 </script> 98 </script>
75 99
......
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: 2023-01-30 14:22:57 4 + * @LastEditTime: 2023-01-31 23:22:38
5 * @FilePath: /data-table/src/components/RadioField/index.vue 5 * @FilePath: /data-table/src/components/RadioField/index.vue
6 * @Description: 单项选择控件 6 * @Description: 单项选择控件
7 --> 7 -->
...@@ -12,24 +12,31 @@ ...@@ -12,24 +12,31 @@
12 }}<span v-if="item.component_props.required">&nbsp;*</span> 12 }}<span v-if="item.component_props.required">&nbsp;*</span>
13 </div> 13 </div>
14 <div v-if="item.component_props.note" class="note" v-html="item.component_props.note" /> 14 <div v-if="item.component_props.note" class="note" v-html="item.component_props.note" />
15 - <van-field :name="item.key" :rules="item.rules"> 15 + <van-field :rules="item.rules">
16 <template #input> 16 <template #input>
17 <van-radio-group 17 <van-radio-group
18 @change="onChange(item)" 18 @change="onChange(item)"
19 - v-model="item.value" 19 + v-model="radio_value"
20 :direction="item.component_props.direction" 20 :direction="item.component_props.direction"
21 style="width: 100%" 21 style="width: 100%"
22 > 22 >
23 + <template v-for="x in item.component_props.options" :key="x.value">
23 <van-radio 24 <van-radio
24 - v-for="x in item.component_props.options"
25 - :key="x.value"
26 :name="x.value" 25 :name="x.value"
27 icon-size="1rem" 26 icon-size="1rem"
28 :checked-color="themeVars.radioColor" 27 :checked-color="themeVars.radioColor"
29 style="margin-bottom: 0.25rem" 28 style="margin-bottom: 0.25rem"
30 >{{ x.title }}</van-radio 29 >{{ x.title }}</van-radio
31 > 30 >
32 - <van-field v-if="has_add_info" :name="add_info_name" v-model="add_info" label="" placeholder="请输入补充信息" style="border: 1px solid #eaeaea;border-radius: 0.25rem; padding: 0.25rem 0.5rem;" /> 31 + <van-field
32 + v-if="radio_value === x.value"
33 + @blur="onBlur(x)"
34 + v-model="x.affix"
35 + label=""
36 + placeholder="请输入补充信息"
37 + style="border: 1px solid #eaeaea;border-radius: 0.25rem; padding: 0.25rem 0.5rem;"
38 + />
39 + </template>
33 </van-radio-group> 40 </van-radio-group>
34 </template> 41 </template>
35 </van-field> 42 </van-field>
...@@ -51,17 +58,34 @@ const themeVars = { ...@@ -51,17 +58,34 @@ const themeVars = {
51 const HideShow = computed(() => { 58 const HideShow = computed(() => {
52 return !props.item.component_props.disabled 59 return !props.item.component_props.disabled
53 }) 60 })
54 -// TEST: 测试新功能:选择其他选项时,下方出现输入框,如果其他项被选中,输入框值为最终录入值。 61 +
55 -// 绑定值发生变化时回调,处理选项为其他时的输入项录入 62 +const emit = defineEmits(["active"]);
56 -const has_add_info = ref(true); // TODO: 文字不一定是其他,后续可能需要字段绑定一个值,标识是否有其他输入框进行判断 63 +const radio_value = ref(props.item.component_props.default);
57 -const add_info = ref(''); 64 +const affix_value = ref('');
58 -const add_info_name = ref(props.item.key + '#'); 65 +
59 -const add_info_key = ref('其他'); // TODO: 以后动态获取
60 const onChange = (item) => { 66 const onChange = (item) => {
61 - console.warn(item); 67 + clearAffix()
68 +}
69 +const onBlur = (item) => {
70 + clearAffix()
71 + affix_value.value = item.affix
72 + // 发送自定义数据结构
73 + props.item.value = { key: props.item.key, value: radio_value.value, affix: affix_value.value, type: "radio" };
74 + emit("active", props.item.value);
75 +}
76 +const clearAffix = () => {
77 + const options = props.item.component_props.options;
78 + // 为选中项目的补充清空
79 + options.forEach(element => {
80 + if (element.value !== radio_value.value) {
81 + element.affix = ''
82 + }
83 + });
62 } 84 }
63 onMounted(() => { 85 onMounted(() => {
64 - add_info_name.value = `${props.item.key}#${add_info_key.value}` 86 + // 发送自定义数据结构
87 + props.item.value = { key: props.item.key, value: radio_value.value, affix: affix_value.value, type: "radio" };
88 + emit("active", props.item.value);
65 }) 89 })
66 </script> 90 </script>
67 91
......
1 <!-- 1 <!--
2 * @Date: 2022-09-08 15:47:54 2 * @Date: 2022-09-08 15:47:54
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2022-12-28 18:51:11 4 + * @LastEditTime: 2023-01-31 22:57:40
5 * @FilePath: /data-table/src/components/RatePickerField/index.vue 5 * @FilePath: /data-table/src/components/RatePickerField/index.vue
6 * @Description: 评分选择控件 6 * @Description: 评分选择控件
7 --> 7 -->
......
1 <!-- 1 <!--
2 * @Date: 2022-07-18 10:22:22 2 * @Date: 2022-07-18 10:22:22
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2023-01-30 12:39:07 4 + * @LastEditTime: 2023-02-01 00:19:00
5 * @FilePath: /data-table/src/views/index.vue 5 * @FilePath: /data-table/src/views/index.vue
6 * @Description: 首页 6 * @Description: 首页
7 --> 7 -->
...@@ -287,6 +287,20 @@ const onActive = (item) => { ...@@ -287,6 +287,20 @@ const onActive = (item) => {
287 if (item.type === "rate") { 287 if (item.type === "rate") {
288 postData.value = _.assign(postData.value, { [item.key]: item.value }); 288 postData.value = _.assign(postData.value, { [item.key]: item.value });
289 } 289 }
290 + if (item.type === "radio") {
291 + postData.value = _.assign(postData.value, { [item.key]: item.affix ? item.affix : item.value });
292 + }
293 + if (item.type === "checkbox") {
294 + const checkbox_value = _.cloneDeep(item.value)
295 + checkbox_value.forEach((element, index) => {
296 + for (const key in item.affix) {
297 + if (item.affix[key] && element === key) {
298 + checkbox_value[index] = item.affix[key]
299 + }
300 + }
301 + });
302 + postData.value = _.assign(postData.value, { [item.key]: checkbox_value });
303 + }
290 }; 304 };
291 305
292 // 检验没有绑定name的输入项 306 // 检验没有绑定name的输入项
...@@ -363,22 +377,23 @@ const preValidData = (values) => { ...@@ -363,22 +377,23 @@ const preValidData = (values) => {
363 // 过滤掉标识为 ignore,undefined 的字段数据 377 // 过滤掉标识为 ignore,undefined 的字段数据
364 let { ignore, undefined, ...rest_data } = values; 378 let { ignore, undefined, ...rest_data } = values;
365 // TAG: 处理其他补充消息输入框内容 379 // TAG: 处理其他补充消息输入框内容
366 - for (const key in rest_data) { 380 + // for (const key in rest_data) {
367 - if (key.includes('#')) { // #号为自定义标识 381 + // if (key.includes('#')) { // #号为自定义标识
368 - const [input_key, input_value] = key.split('#'); // 原始key值标记 382 + // const [input_key, input_value] = key.split('#'); // 原始key值标记
369 - if (typeof rest_data[input_key] === 'object') { // 多选框值为数组 383 + // if (typeof rest_data[input_key] === 'object') { // 多选框值为数组
370 - rest_data[input_key].forEach((element, index)=> { 384 + // rest_data[input_key].forEach((element, index)=> {
371 - if (element === input_value) { // 标签名等于选择的值 385 + // if (element === input_value) { // 标签名等于选择的值
372 - rest_data[input_key][index] = rest_data[key]; 386 + // // rest_data[input_key][index] = rest_data[key] ? rest_data[key] : input_value;
373 - } 387 + // rest_data[input_key][index] = rest_data[key];
374 - }); 388 + // }
375 - } else { // 单选框值为字符串 389 + // });
376 - if (rest_data[input_key] === input_value) { // 标签名等于选择的值 390 + // } else { // 单选框值为字符串
377 - rest_data[input_key] = rest_data[key]; 391 + // if (rest_data[input_key] === input_value) { // 标签名等于选择的值
378 - } 392 + // rest_data[input_key] = rest_data[key] ? rest_data[key] : input_value;
379 - } 393 + // }
380 - } 394 + // }
381 - } 395 + // }
396 + // }
382 // console.warn(postData.value); 397 // console.warn(postData.value);
383 // 合并自定义字段到提交表单字段 398 // 合并自定义字段到提交表单字段
384 return _.assign(postData.value, rest_data); 399 return _.assign(postData.value, rest_data);
......