BaseForm.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <template>
  2. <a-card :bordered="false" v-show="visible" class="card" :title="modalTitle">
  3. <a-row :gutter="48" slot="extra">
  4. <a-col :md="48" :sm="48">
  5. <span class="table-page-search-submitButtons" style="float: right">
  6. <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
  7. <a-button style="margin-left: 8px" type="default" @click="handleCancel()">返回</a-button>
  8. </span>
  9. </a-col>
  10. </a-row>
  11. <a-form :form="form">
  12. <a-form-item v-show="false">
  13. <a-input v-decorator="['id']" type="hidden"/>
  14. </a-form-item>
  15. <row-list :col="2">
  16. <row-item>
  17. <a-form-item
  18. label="类型"
  19. :labelCol="BaseTool.Constant.labelCol"
  20. :wrapperCol="BaseTool.Constant.wrapperCol"
  21. >
  22. <a-select
  23. v-decorator="['type', {rules: [{required: true, message: '类型不能为空'}]}]"
  24. placeholder="请选择">
  25. <a-select-option
  26. v-for="(label,value) in typeMap"
  27. :key="value"
  28. :label="label"
  29. :value="parseInt(value)">{{ label }}
  30. </a-select-option>
  31. </a-select>
  32. </a-form-item>
  33. </row-item>
  34. <row-item>
  35. <a-form-item
  36. label="备注"
  37. :labelCol="BaseTool.Constant.labelCol"
  38. :wrapperCol="BaseTool.Constant.wrapperCol"
  39. >
  40. <a-input
  41. v-decorator="['remark', {rules: [{required: true, message: '备注不能为空'}]}]"/>
  42. </a-form-item>
  43. </row-item>
  44. <row-item>
  45. <a-form-item
  46. label="二级类别"
  47. :labelCol="BaseTool.Constant.labelCol"
  48. :wrapperCol="BaseTool.Constant.wrapperCol"
  49. >
  50. <a-input
  51. v-decorator="['secondTypeId', {rules: [{required: false, message: '二级类别不能为空'}]}]"/>
  52. </a-form-item>
  53. </row-item>
  54. </row-list>
  55. </a-form>
  56. <title-divider title="字段明细" width="90px"></title-divider>
  57. <div class="table-operator" style="margin-bottom: 8px;">
  58. <a-button size="small" type="primary" @click="handleParam">
  59. <a-icon type="plus"/>
  60. 添加
  61. </a-button>
  62. <a-button class="margin-left8" size="small" type="danger" @click="handleDelParam">
  63. <a-icon type="delete"/>
  64. 删除
  65. </a-button>
  66. </div>
  67. <a-table
  68. bordered
  69. :data-source="data"
  70. :columns="columns"
  71. tableLayout="auto"
  72. rowKey="fieldName"
  73. :row-selection="rowSelection">
  74. <span slot="action" slot-scope="record">
  75. <template>
  76. <a @click="handleParam(record)">修改</a>
  77. <a-divider type="vertical" />
  78. <a-popconfirm title="是否要删除该条数据?" @confirm="handleDelParam(record.fieldName)">
  79. <a>删除</a>
  80. </a-popconfirm>
  81. </template>
  82. </span>
  83. </a-table>
  84. <param-form ref="paramForm" @ok="handleParamList" />
  85. </a-card>
  86. </template>
  87. <script>
  88. import pick from 'lodash.pick'
  89. import ParamForm from './ParamForm'
  90. import { addCustomFieldTemplate, updateCustomFieldTemplate } from '@/api/customize/fieldTemplate'
  91. import DetailList from '@/components/tools/DetailList'
  92. export default {
  93. name: 'BaseCustomFieldTemplate',
  94. components: {
  95. ParamForm,
  96. DetailList
  97. },
  98. data () {
  99. return {
  100. confirmLoading: false,
  101. modalTitle: null,
  102. data: [],
  103. form: this.$form.createForm(this),
  104. visible: false,
  105. // 下拉框map
  106. typeMap: {},
  107. typeFieldMap: {},
  108. rowSelection: {
  109. onChange: (selectedRowKeys, selectedRows) => {
  110. this.selectedRowKeys = selectedRowKeys
  111. this.selectedRows = selectedRows
  112. }
  113. },
  114. selectedRowKeys: [],
  115. selectedRows: [],
  116. options: {
  117. rowSelection: {
  118. selectedRowKeys: this.selectedRowKeys
  119. }
  120. },
  121. // 表头
  122. columns: [
  123. {
  124. title: '序号',
  125. dataIndex: 'index',
  126. customRender: (text, record, index) => {
  127. return index + 1
  128. }
  129. },
  130. {
  131. title: '显示名称',
  132. dataIndex: 'label'
  133. },
  134. {
  135. title: '字段名称',
  136. dataIndex: 'fieldName'
  137. },
  138. {
  139. title: '类型',
  140. dataIndex: 'type',
  141. customRender: (text, record, index) => {
  142. return this.BaseTool.Object.getField(this.typeFieldMap, text)
  143. }
  144. },
  145. {
  146. title: '是否必填',
  147. dataIndex: 'required'
  148. },
  149. {
  150. title: '显示行数',
  151. dataIndex: 'rows',
  152. width: 120
  153. },
  154. {
  155. title: '排序',
  156. dataIndex: 'sort',
  157. width: 150
  158. },
  159. {
  160. title: '默认值',
  161. dataIndex: 'defaultValue',
  162. width: 150
  163. },
  164. {
  165. title: '最小长度',
  166. dataIndex: 'minLength',
  167. width: 150
  168. },
  169. {
  170. title: '最大长度',
  171. dataIndex: 'maxLength',
  172. width: 150
  173. },
  174. {
  175. title: '操作',
  176. key: 'action',
  177. align: 'center',
  178. scopedSlots: { customRender: 'action' }
  179. }
  180. ]
  181. }
  182. },
  183. props: {},
  184. created () {
  185. // 下拉框map
  186. this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.CUSTOM_FIELD_TEMPLATE_TYPE)
  187. this.typeFieldMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.CUSTOM_FIELD_TEMPLATE_FILED_TYPE)
  188. },
  189. methods: {
  190. base (record) {
  191. this.visible = true
  192. // 如果是空标识添加
  193. if (this.BaseTool.Object.isBlank(record)) {
  194. this.modalTitle = '添加'
  195. return
  196. }
  197. this.modalTitle = '编辑'
  198. const { form: { setFieldsValue } } = this
  199. this.data = JSON.parse(record.content)
  200. // 日期处理
  201. this.$nextTick(() => {
  202. setFieldsValue(Object.assign(pick(record, [
  203. 'id',
  204. 'content',
  205. 'type',
  206. 'remark',
  207. 'secondTypeId'
  208. ])))
  209. })
  210. },
  211. save () {
  212. const { form: { validateFieldsAndScroll } } = this
  213. this.confirmLoading = true
  214. validateFieldsAndScroll((errors, values) => {
  215. if (errors) {
  216. this.confirmLoading = false
  217. return
  218. }
  219. values.content = JSON.stringify(this.data)
  220. // 日期处理
  221. if (this.BaseTool.String.isBlank(values.id)) {
  222. addCustomFieldTemplate(values)
  223. .then(() => {
  224. this.handleCancel(values)
  225. }).catch(() => {
  226. this.confirmLoading = false
  227. })
  228. } else {
  229. updateCustomFieldTemplate(values)
  230. .then(() => {
  231. this.handleCancel(values)
  232. }).catch(() => {
  233. this.confirmLoading = false
  234. })
  235. }
  236. })
  237. },
  238. handleCancel (values) {
  239. this.visible = false
  240. this.confirmLoading = false
  241. this.form.resetFields()
  242. this.data = []
  243. if (this.BaseTool.Object.isNotBlank(values)) {
  244. this.$emit('ok', values)
  245. } else {
  246. this.$emit('ok')
  247. }
  248. },
  249. handleParam (record) {
  250. const modal = this.$refs.paramForm
  251. modal.base(record)
  252. },
  253. handleParamList (values) {
  254. let dataSource = [...this.data]
  255. if (dataSource == null) {
  256. dataSource = []
  257. }
  258. if (values.label == null || values.label === '') {
  259. return
  260. }
  261. let find = false
  262. for (let j = 0; j < dataSource.length; j++) {
  263. if (values.fieldName === dataSource[j].fieldName) {
  264. find = true
  265. this.data = dataSource.map(item => {
  266. return item.fieldName === values.fieldName ? values : item
  267. })
  268. break
  269. }
  270. }
  271. if (!find) {
  272. dataSource.push(values)
  273. }
  274. this.data = dataSource
  275. },
  276. handleDelParam (fieldName) {
  277. const dataSource = [...this.data]
  278. this.data = dataSource.filter(item => item.fieldName !== fieldName)
  279. }
  280. }
  281. }
  282. </script>