wujingjing
2024-07-08 ae78b3b194d399e3ba7bd3922f9f83ca0d7c762c
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<!-- 属性表格编辑 -->
<template>
    <!--  -->
 
    <el-form ref="propFormRef" :model="propFormValue" :rules="propFormRules" class="w100" :inline-message="true">
        <!-- property 数据展示表格 -->
        <el-table
            class="h100"
            border
            row-key="ID"
            :span-method="objectSpanMethod"
            :cell-style="{ textAlign: 'center' }"
            :header-cell-style="{ textAlign: 'center' }"
            :data="data"
            style="width: 100%"
            highlight-current-row
            row-class-name="cursor-pointer"
        >
            <el-table-column prop="GroupName" label="属性组" fixed="left" show-overflow-tooltip />
            <el-table-column prop="PropName" label="属性" show-overflow-tooltip />
 
            <el-table-column prop="PropInfo" label="值" show-overflow-tooltip>
                <template #default="scope">
                    <el-form-item :prop="scope.row.PropInfo.ID">
                        <!-- 有 choice 且非时间类型 -->
                        <el-select
                            style="width: 74%"
                            size="small"
                            clearable
                            allow-create
                            filterable
                            v-model="propFormValue[scope.row.PropInfo.ID]"
                            v-if="checkIsSelect(scope.row.PropInfo)"
                        >
                            <el-option v-for="item in scope.row.PropInfo?.ChoiceList" :label="item.Name" :value="item.Choice"></el-option>
                        </el-select>
 
                        <!-- 文本、多文本、数值、整数、长整数 -->
                        <el-input
                            v-else-if="INPUT_FORMAT.includes(scope.row.PropInfo.Format)"
                            style="width: 74%"
                            v-model="propFormValue[scope.row.PropInfo.ID]"
                            v-bind="getInputProps(scope.row.PropInfo.Format)"
                        ></el-input>
                        <!-- 时间输入(包括有 choice) -->
                        <el-date-picker
                            v-else-if="scope.row.PropInfo.Format === PropertyFormatEnum.Time"
                            style="width: 74%"
                            value-format="YYYY-MM-DD"
                            size="small"
                            v-model="propFormValue[scope.row.PropInfo.ID]"
                            :shortcuts="getDateShortcuts(scope.row.PropInfo)"
                        >
                        </el-date-picker>
                        <!-- 布尔值输入 -->
                        <el-radio-group
                            v-else-if="scope.row.PropInfo.Format === PropertyFormatEnum.Boolean"
                            style="width: 74%"
                            v-model="propFormValue[scope.row.PropInfo.ID]"
                        >
                            <el-radio label="true" size="small" :value="true"></el-radio>
                            <el-radio label="false" size="small" :value="false"></el-radio>
                        </el-radio-group>
                        <span class="ml13">
                            {{ scope.row.PropInfo.UnitName }}
                        </span>
                    </el-form-item>
                </template>
            </el-table-column>
        </el-table>
    </el-form>
</template>
 
<script setup lang="ts">
import { computed, ref, toRefs, watch } from 'vue';
import { PropertyFormatEnum, INPUT_FORMAT } from '/@/projectCom/basic/types';
 
import { FormInstance, FormRules } from 'element-plus';
import { booleanRegex, dateRegex, integerRegex, numberRegex } from '/@/utils/toolsValidate';
 
const props = defineProps({
    data: {
        type: Array,
    },
    modelValue: {
        type: Object,
    },
});
 
const emits = defineEmits<{
    (event: 'update:modelValue', value: Object): void;
}>();
 
const objectSpanMethod = ({ row, column, rowIndex, columnIndex }): { rowspan: number; colspan: number } => {
    if (columnIndex === 0) {
        return {
            rowspan: row.rowspan,
            colspan: 1,
        };
    }
};
const { data, modelValue } = toRefs(props);
 
const propFormValue = computed({
    get: () => {
        return modelValue.value;
    },
    set: (value) => {
        emits('update:modelValue', value);
    },
});
 
const checkIsSelect = (propInfo) => {
    return propInfo?.ChoiceList?.length !== 0 && propInfo.Format !== PropertyFormatEnum.Time;
};
 
const getDateShortcuts = (propInfo) => {
    if (propInfo?.ChoiceList?.length === 0) return [];
    return propInfo.ChoiceList.map((item) => {
        return {
            text: item.Name,
            value: item.Choice,
        };
    });
};
 
const getInputProps = (format: PropertyFormatEnum) => {
    const basicProps = { size: 'small' };
    let otherProps = {};
    switch (format) {
        case PropertyFormatEnum.MultiText:
            otherProps = {
                type: 'textarea',
                row: 3,
            };
 
        default:
            break;
    }
    return {
        ...basicProps,
        ...otherProps,
    } as any;
};
 
/**
 * 属性输入验证
 * @param rule
 * @param value
 * @param callback
 */
const getRowValidator = (propInfo) => {
    const validator = (rule, value, callback) => {
        if (!value && !propInfo.IsNull) {
            callback('必填项!');
        } else if (value && propInfo.Format === PropertyFormatEnum.Numeric && !numberRegex.test(value)) {
            callback('请输入数字!');
        } else if (value && [PropertyFormatEnum.Bigint, PropertyFormatEnum.Integer].includes(propInfo.Format) && !integerRegex.test(value)) {
            callback('请输入整数!');
        } else if (value && PropertyFormatEnum.Time === propInfo.Format && !dateRegex.test(value)) {
            callback('请输入正确的日期格式(yyyy-MM-dd HH:mm:ss 或 yyyy-MM-dd)');
        } else if (value && PropertyFormatEnum.Boolean === propInfo.Format && !booleanRegex.test(value)) {
            callback('请输入 true 或 false');
        } else {
            callback();
        }
    };
    return validator;
};
 
const rules = ref({});
watch(
    () => data.value,
    (val) => {
        for (const item of val as any[]) {
            rules.value[item.PropInfo.ID] = [
                {
                    validator: getRowValidator(item.PropInfo),
                    // 选择框只能使用 change 触发
                    trigger: checkIsSelect(item.PropInfo) ? 'change' : 'blur',
                },
            ];
        }
    }
);
 
const propFormRules = ref<FormRules>(rules.value);
const propFormRef = ref<FormInstance>(null);
 
defineExpose({
    formRef: propFormRef,
});
</script>
<style scoped lang="scss"></style>