增加formId属性以防止使用多个v-form-render组件时出现冲突。

master
vdpAdmin 2022-03-16 17:59:12 +08:00
parent 2e632e7a96
commit 6fc227cffc
7 changed files with 49 additions and 13 deletions

View File

@ -82,5 +82,11 @@ export default {
} }
}, },
setWidgetOption(optionName, optionValue) { //通用组件选项修改API
if (this.widget.options.hasOwnProperty(optionName)) {
this.widget.options[optionName] = optionValue
}
},
} }
} }

View File

@ -1,5 +1,5 @@
<template> <template>
<el-form-item :rules="nameRequiredRule"> <el-form-item prop="name" :rules="nameRequiredRule">
<span slot="label">{{i18nt('designer.setting.uniqueName')}} <span slot="label">{{i18nt('designer.setting.uniqueName')}}
<el-tooltip effect="light" :content="i18nt('designer.setting.editNameHelp')"> <el-tooltip effect="light" :content="i18nt('designer.setting.editNameHelp')">
<i class="el-icon-info"></i></el-tooltip> <i class="el-icon-info"></i></el-tooltip>

View File

@ -67,6 +67,7 @@
<el-button type="primary" @click="setFormEnabled">{{i18nt('designer.hint.enableForm')}}</el-button> <el-button type="primary" @click="setFormEnabled">{{i18nt('designer.hint.enableForm')}}</el-button>
<el-button type="" @click="showPreviewDialogFlag = false">{{i18nt('designer.hint.closePreview')}}</el-button> <el-button type="" @click="showPreviewDialogFlag = false">{{i18nt('designer.hint.closePreview')}}</el-button>
<el-button v-if="false" @click="printFormJson">PrintFormJson</el-button> <el-button v-if="false" @click="printFormJson">PrintFormJson</el-button>
<el-button v-if="true" @click="testValidate">TestValidate</el-button>
</div> </div>
</el-dialog> </el-dialog>
@ -589,6 +590,10 @@
console.log(tmpFS) console.log(tmpFS)
}, },
testValidate() {
console.log('test===', this.$refs['preForm'].validateForm())
},
handleFormChange(fieldName, newValue, oldValue, formModel) { handleFormChange(fieldName, newValue, oldValue, formModel) {
/* /*
console.log('---formChange start---') console.log('---formChange start---')

View File

@ -91,6 +91,12 @@ export default {
} }
}, },
setWidgetOption(optionName, optionValue) { //通用组件选项修改API
if (this.widget.options.hasOwnProperty(optionName)) {
this.widget.options[optionName] = optionValue
}
},
/** /**
* 获取子表单的行数 * 获取子表单的行数
*/ */

View File

@ -42,7 +42,7 @@
import './container-item/index' import './container-item/index'
import FieldComponents from '@/components/form-designer/form-widget/field-widget/index' import FieldComponents from '@/components/form-designer/form-widget/field-widget/index'
import { import {
deepClone, getAllContainerWidgets, getAllFieldWidgets, generateId, deepClone, getAllContainerWidgets, getAllFieldWidgets,
insertCustomCssToHead, insertCustomCssToHead,
insertGlobalFunctionsToHtml, insertGlobalFunctionsToHtml,
traverseFieldWidgets traverseFieldWidgets
@ -95,6 +95,7 @@
}, },
widgetRefList: {}, widgetRefList: {},
subFormRefList: {}, subFormRefList: {},
formId: null, //Idv-form-render
} }
}, },
computed: { computed: {
@ -148,6 +149,7 @@
}, },
methods: { methods: {
initFormObject() { initFormObject() {
this.formId = 'vfRender' + generateId()
this.insertCustomStyleAndScriptNode() this.insertCustomStyleAndScriptNode()
this.addFieldChangeEventHandler() this.addFieldChangeEventHandler()
this.addFieldValidateEventHandler() this.addFieldValidateEventHandler()
@ -170,11 +172,11 @@
insertCustomStyleAndScriptNode() { insertCustomStyleAndScriptNode() {
if (!!this.formConfig && !!this.formConfig.cssCode) { if (!!this.formConfig && !!this.formConfig.cssCode) {
insertCustomCssToHead(this.formConfig.cssCode) insertCustomCssToHead(this.formConfig.cssCode, this.formId)
} }
if (!!this.formConfig && !!this.formConfig.functions) { if (!!this.formConfig && !!this.formConfig.functions) {
insertGlobalFunctionsToHtml(this.formConfig.functions) insertGlobalFunctionsToHtml(this.formConfig.functions, this.formId)
} }
}, },
@ -259,7 +261,6 @@
addFieldChangeEventHandler() { addFieldChangeEventHandler() {
this.$off('fieldChange') // this.$off('fieldChange') //
this.$on('fieldChange', (fieldName, newValue, oldValue, subFormName, subFormRowIndex) => { this.$on('fieldChange', (fieldName, newValue, oldValue, subFormName, subFormRowIndex) => {
this.handleFieldDataChange(fieldName, newValue, oldValue, subFormName, subFormRowIndex) this.handleFieldDataChange(fieldName, newValue, oldValue, subFormName, subFormRowIndex)
this.$emit('formChange', fieldName, newValue, oldValue, this.formDataModel, subFormName, subFormRowIndex) this.$emit('formChange', fieldName, newValue, oldValue, this.formDataModel, subFormName, subFormRowIndex)
@ -572,8 +573,14 @@
this.$refs.renderForm.clearValidate(props) this.$refs.renderForm.clearValidate(props)
}, },
/* 验证表单通过返回true不通过返回false */
validateForm() { validateForm() {
// let result = null
this.$refs['renderForm'].validate((valid) => {
result = valid
})
return result
}, },
validateFields() { validateFields() {

View File

@ -76,7 +76,11 @@ export const loadExtension = function () {
{label: 'info', value: 'info'}, {label: 'info', value: 'info'},
{label: 'error', value: 'error'}, {label: 'error', value: 'error'},
] ]
PERegister.registerCPEditor('alert-type', 'alert-type-editor', // PERegister.registerCPEditor('alert-type', 'alert-type-editor',
// PEFactory.createSelectEditor('type', 'extension.setting.alertType',
// {optionItems: typeOptions}))
/* type属性映射已存在无须再注册故只需注册属性编辑器即可 */
Vue.component('alert-type-editor',
PEFactory.createSelectEditor('type', 'extension.setting.alertType', PEFactory.createSelectEditor('type', 'extension.setting.alertType',
{optionItems: typeOptions})) {optionItems: typeOptions}))

View File

@ -57,17 +57,21 @@ const createStyleSheet = function() {
return style.sheet; return style.sheet;
} }
export const insertCustomCssToHead = function (cssCode) { export const insertCustomCssToHead = function (cssCode, formId = '') {
let head = document.getElementsByTagName('head')[0] let head = document.getElementsByTagName('head')[0]
let oldStyle = document.getElementById('vform-custom-css') let oldStyle = document.getElementById('vform-custom-css')
if (!!oldStyle) { if (!!oldStyle) {
head.removeChild(oldStyle) //应该先清除后插入!! head.removeChild(oldStyle) //先清除后插入!!
}
if (!!formId) {
oldStyle = document.getElementById('vform-custom-css' + '-' + formId)
!!oldStyle && head.removeChild(oldStyle) //先清除后插入!!
} }
let newStyle = document.createElement('style') let newStyle = document.createElement('style')
newStyle.type = 'text/css' newStyle.type = 'text/css'
newStyle.rel = 'stylesheet' newStyle.rel = 'stylesheet'
newStyle.id = 'vform-custom-css' newStyle.id = !!formId ? 'vform-custom-css' + '-' + formId : 'vform-custom-css'
try { try {
newStyle.appendChild(document.createTextNode(cssCode)) newStyle.appendChild(document.createTextNode(cssCode))
} catch(ex) { } catch(ex) {
@ -77,13 +81,17 @@ export const insertCustomCssToHead = function (cssCode) {
head.appendChild(newStyle) head.appendChild(newStyle)
} }
export const insertGlobalFunctionsToHtml = function (functionsCode) { export const insertGlobalFunctionsToHtml = function (functionsCode, formId = '') {
let bodyEle = document.getElementsByTagName('body')[0] let bodyEle = document.getElementsByTagName('body')[0]
let oldScriptEle = document.getElementById('v_form_global_functions') let oldScriptEle = document.getElementById('v_form_global_functions')
!!oldScriptEle && bodyEle.removeChild(oldScriptEle) !!oldScriptEle && bodyEle.removeChild(oldScriptEle) //先清除后插入!!
if (!!formId) {
oldScriptEle = document.getElementById('v_form_global_functions' + '-' + formId)
!!oldScriptEle && bodyEle.removeChild(oldScriptEle) //先清除后插入!!
}
let newScriptEle = document.createElement('script') let newScriptEle = document.createElement('script')
newScriptEle.id = 'v_form_global_functions' newScriptEle.id = !!formId ? 'v_form_global_functions' + '-' + formId : 'v_form_global_functions'
newScriptEle.type = 'text/javascript' newScriptEle.type = 'text/javascript'
newScriptEle.innerHTML = functionsCode newScriptEle.innerHTML = functionsCode
bodyEle.appendChild(newScriptEle) bodyEle.appendChild(newScriptEle)