<!-- 属性表格编辑 -->
|
<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>
|