Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
feat: 增加批量添加表单项
Browse files Browse the repository at this point in the history
  • Loading branch information
dream2023 committed Feb 6, 2020
1 parent ada93f6 commit e20defd
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 71 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@
"serialize-javascript": "^2.1.2",
"standard-version": "^7.0.1",
"vue": "^2.6.10",
"vue-ele-form": "^0.8.3",
"vue-ele-form": "^0.8.11",
"vue-ele-form-bmap": "^0.1.0",
"vue-ele-form-codemirror": "^0.1.0",
"vue-ele-form-dynamic": "^0.3.0",
"vue-ele-form-dynamic": "^0.4.0",
"vue-ele-form-gallery": "^0.1.1",
"vue-ele-form-image-uploader": "^0.1.4",
"vue-ele-form-json-editor": "^0.1.2",
Expand Down
159 changes: 159 additions & 0 deletions src/components/app-main/components/header-components/batchDialog.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<template>
<ele-form-dialog
v-model="formData"
:formDesc="formDesc"
labelPosition="left"
width="800px"
:dialogAttrs="{
'append-to-body': true
}"
:request-fn="handleAdd"
:visible="visible"
@update:visible="$emit('update:visible', $event)"
title="批量添加表单信息"
></ele-form-dialog>
</template>

<script>
import _ from 'lodash-es'
import comps from '@/comps.js'
import { addFormItem } from '@/tool.js'
import { mapState, mapMutations } from 'vuex'
import EleFormDialog from 'vue-ele-form/lib/EleFormDialog'
export default {
name: 'batchDialog',
components: { EleFormDialog },
props: {
visible: {
type: Boolean,
default: false
}
},
computed: {
...mapState(['list'])
},
data() {
return {
formData: {
type: 'dynamic',
data: []
},
formDesc: {
type: {
type: 'radio',
label: '添加方式',
default: 'dynamic',
options: [
{ text: '逐个新增', value: 'dynamic' },
{ text: '编辑对象', value: 'json-editor' },
{
text: '数据库导入(待开发)',
value: 'mysql',
attrs: { disabled: true }
}
]
},
data: {
type: data => data.type,
label: '表单项',
isTypeChangeReloadValue: false,
attrs: {
columns: [
{
type: 'el-input',
valueKey: 'field',
attrs: {
placeholder: '请输入 field'
}
},
{
type: 'el-input',
valueKey: 'label',
attrs: {
placeholder: '请输入 label'
}
},
{
type: 'el-select',
valueKey: 'type',
slots: {
default(h) {
return comps
.map(item => item.comps)
.flat()
.map(item =>
h('el-option', {
attrs: {
label: `${item.type} - ${item.label}`,
value: item.type
}
})
)
}
},
attrs: {
filterable: true,
placeholder: '请输入选择 type'
}
}
]
},
valueFormatter: arr =>
arr.map(item => _.pick(item, ['field', 'label', 'type'])),
rules: {
type: 'array',
validator: (rule, value, callback) => {
let errorArr = []
if (Array.isArray(value) && value.length) {
// 字段不可重复
errorArr = value
.map((item, index) =>
this.formDesc[item.field]
? `${index + 1}行的 field 重复`
: null
)
.filter(Boolean)
// 字段不可为空
errorArr = errorArr.concat(
value
.map((item, index) => {
const errMsg = []
if (!item.field) errMsg.push('field')
if (!item.label) errMsg.push('label')
if (!item.type) errMsg.push('type')
return errMsg.length
? `${index + 1}行的 ${errMsg.join('')} 未填写`
: null
})
.filter(Boolean)
)
}
errorArr.length
? callback(new Error(errorArr.join(' & ')))
: callback()
}
}
}
}
}
},
methods: {
...mapMutations(['updateList']),
handleAdd({ data }) {
if (Array.isArray(data) && data.length) {
const newFormItems = data.map(item => addFormItem(item))
this.updateList(this.list.concat(newFormItems))
this.$message.success('添加成功')
this.formData = {
type: 'dynamic',
data: []
}
this.$emit('update:visible', false)
}
}
}
}
</script>
11 changes: 10 additions & 1 deletion src/components/app-main/components/main-header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<el-button @click="isShowCode = true" icon="el-icon-tickets" type="text"
>生成代码</el-button
>
<el-button @click="isShowBatchDialog = true" icon="el-icon-plus" type="text"
>批量添加</el-button
>
<el-button @click="clearForm" icon="el-icon-delete" type="text"
>清空表单</el-button
>
Expand Down Expand Up @@ -55,6 +58,9 @@
<el-button @click="handleCopyHtml" type="primary">复制代码</el-button>
</div>
</el-dialog>

<!-- 批量添加 -->
<batch-dialog :visible.sync="isShowBatchDialog"></batch-dialog>
</div>
</template>

Expand All @@ -64,11 +70,13 @@ import _ from 'lodash-es'
import configList from '@/config'
import { mapState, mapMutations } from 'vuex'
import formAttrDefault from '@/store/formAttrDefault'
import batchDialog from './header-components/batchDialog'
const serialize = require('serialize-javascript')
const copy = require('clipboard-copy')
export default {
name: 'AppMainHeader',
components: { batchDialog },
computed: {
...mapState(['formAttr', 'list']),
filterFormAttr() {
Expand Down Expand Up @@ -146,7 +154,8 @@ export default {
formData: {},
isShowData: false,
isShowCode: false,
isPreview: false
isPreview: false,
isShowBatchDialog: false
}
},
methods: {
Expand Down
28 changes: 2 additions & 26 deletions src/components/app-main/components/main-left.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
</template>

<script>
import _ from 'lodash-es'
import comps from '@/comps'
import configList from '@/config'
import { addFormItem } from '@/tool.js'
import draggable from 'vuedraggable'
export default {
Expand All @@ -44,30 +43,7 @@ export default {
methods: {
// 拖拽后的数据
addFormItem({ label, type }) {
// 获取配置
// 关于 `attrsData` 和 `attrsDefaultData`, 及 `commonData` 和 `commonDefaultData` 的解释请看 src/config/README.md
const {
attrsData = {},
attrsDefaultData = {},
commonData = {},
commonDefaultData = {}
} = configList[type] || {}
return Object.assign(
{},
_.cloneDeep(commonDefaultData),
_.cloneDeep(commonData),
{
field: 'key_' + Date.now(),
label,
type,
// 组件属性
attrs: {
..._.cloneDeep(attrsDefaultData),
..._.cloneDeep(attrsData)
}
}
)
return addFormItem({ label, type, field: 'key_' + Date.now() })
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export default new Vuex.Store({
// 删除列表项
deleteItemByIndex (state, index) {
state.list.splice(index, 1)
},
// 修改列表
updateList (state, newList) {
state.list = newList
}
},
plugins: persistedstate()
Expand Down
27 changes: 27 additions & 0 deletions src/tool.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _ from 'lodash-es'
import configList from '@/config'

// 修改label => key + label 更明确告知用户属性名
export function changeFormLabel (obj = {}, exceptProperty = []) {
Expand Down Expand Up @@ -26,3 +27,29 @@ export function isVscode () {
return true
}
}

// 新 增表单项
export function addFormItem ({ type, label, field }) {
const {
attrsData = {},
attrsDefaultData = {},
commonData = {},
commonDefaultData = {}
} = configList[type] || {}

return Object.assign(
{},
_.cloneDeep(commonDefaultData),
_.cloneDeep(commonData),
{
field: field,
label,
type,
// 组件属性
attrs: {
..._.cloneDeep(attrsDefaultData),
..._.cloneDeep(attrsData)
}
}
)
}

0 comments on commit e20defd

Please sign in to comment.