form.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <template>
  2. <div>
  3. <el-row :gutter="40">
  4. <template v-for="(item, k) in fields">
  5. <el-col v-if="!item.isHide" :span="(item.span||1)/columnNum * 24" :key="k">
  6. <el-form-item :label="item.label" :prop="item.id">
  7. <slot v-if="$scopedSlots[item.id]" :name="item.id" :config="item" />
  8. <template v-else>
  9. <el-input v-bind="item" v-if="item.type=='text'||item.type=='number'||item.type=='digit'" v-model="form[item.id]" :placeholder="item.placeholder" clearable/>
  10. <el-input
  11. v-bind="item"
  12. v-if="item.type=='textarea'"
  13. v-model="form[item.id]">
  14. </el-input>
  15. <el-date-picker
  16. v-else-if="item.type=='daterange'||item.type=='monthrange'"
  17. v-model="form[item.id]"
  18. :start-placeholder="item.label+ '起'"
  19. :end-placeholder="item.label+ '至'"
  20. align="right"
  21. unlink-panels
  22. :value-format="item.format||(item.type=='monthrange'? 'yyyy-MM' : 'yyyy-MM-dd' )"
  23. range-separator="至"
  24. :picker-options="item.pickerOptions || pickerOptions"
  25. v-bind="item"
  26. >
  27. </el-date-picker>
  28. <el-date-picker
  29. v-bind="item"
  30. clearable
  31. v-else-if="item.type=='date'||item.type=='year'||item.type=='month'||item.type=='week'||item.type=='dates'||item.type=='datetime'"
  32. v-model="form[item.id]"
  33. :value-format="item.format||(item.type=='year'? 'yyyy' : item.type=='month'? 'yyyy-MM' : item.type=='datetime'? 'yyyy-MM-dd HH:mm:ss' : 'yyyy-MM-dd' )"
  34. >
  35. </el-date-picker>
  36. <el-select
  37. v-else-if="item.type=='select'"
  38. v-model="form[item.id]"
  39. clearable
  40. v-bind="item"
  41. @change="onSelChange($event, item)"
  42. v-on="item.evts"
  43. >
  44. <el-option
  45. v-for="opt in item.optionList"
  46. :key="opt.value"
  47. :label="opt.label"
  48. :value="opt.value">
  49. </el-option>
  50. </el-select>
  51. <Upload
  52. :configs="item"
  53. v-else-if="item.type=='photo'"
  54. v-model="form[item.id]">
  55. </Upload>
  56. <el-image v-else-if="item.type=='image'" style="width: 100px; height: 100px" :src="form[item.id]" :preview-src-list="[item.srcList]"></el-image>
  57. <template v-else-if="item.type=='radio'">
  58. <el-radio
  59. v-for="opt in item.optionList"
  60. :key="opt.value"
  61. v-model="form[item.id]"
  62. @change="onSelChange($event, item)"
  63. :label="opt.value">{{opt.label}}</el-radio>
  64. </template>
  65. <template v-else-if="item.type=='checkbox'">
  66. <el-checkbox-group
  67. @change="onCheckboxChange($event, item)"
  68. v-model="form[item.id]">
  69. <el-checkbox
  70. v-for="opt in item.optionList"
  71. :key="opt.value"
  72. :label="opt.value">
  73. {{opt.label}}
  74. </el-checkbox>
  75. </el-checkbox-group>
  76. </template>
  77. <div v-else-if="item.type=='inputNumber'">
  78. <el-input-number v-model="form[item.id]" :controls-position="item.controlsPosition||''" v-bind="item.elmentCfg" :placeholder="item.placeholder" :min="item.min||0" :max="item.max"></el-input-number>
  79. <span v-if="item.afterText" class="ml5">{{item.afterText}}</span>
  80. </div>
  81. <template v-else-if="item.type=='richtext'">
  82. <QuillEditor
  83. ref="formQuillTextEditor"
  84. v-model="form[item.id]"
  85. @blur="onEditorBlur($event, item)"
  86. @focus="onEditorFocus($event, item)"
  87. @ready="onEditorReady($event, item)"
  88. @change="onEditorChange($event, item)">
  89. </QuillEditor>
  90. </template>
  91. <div v-else-if="item.type=='label'" >
  92. {{form[item.id]}}
  93. </div>
  94. </template>
  95. </el-form-item>
  96. <slot v-if="$scopedSlots[item.id+'_tips']" :name="item.id+'_tips'" :config="item" />
  97. </el-col>
  98. </template>
  99. </el-row>
  100. </div>
  101. </template>
  102. <script>
  103. import QuillEditor from '../QuillEditor'
  104. import Upload from './Upload'
  105. import {pickerOptions} from '../js/common'
  106. import Tools from '../../utils/tool'
  107. export default {
  108. props: {
  109. fields: {},
  110. columnNum: {},
  111. form: {},
  112. },
  113. components:{
  114. Upload,
  115. QuillEditor
  116. },
  117. computed:{
  118. },
  119. data(){
  120. return {
  121. rows: "",
  122. pickerOptions,
  123. }
  124. },
  125. created(){
  126. this.fields.forEach((v, i) => {
  127. this.loadDict(v)
  128. });
  129. },
  130. mounted() {
  131. },
  132. methods: {
  133. loadDict(item){
  134. if(!item.optionListLoadConfig){
  135. return
  136. }
  137. if(!Tools.dictLoad){
  138. return
  139. }
  140. Tools.dictLoad(item.optionListLoadConfig).then(res=>{
  141. item.optionList = res
  142. })
  143. },
  144. onSelChange(value, item){
  145. if(item.multiple){
  146. if(item.valueName.length < value.length){
  147. var exist = _.find(item.optionList, (v)=>{
  148. return v.value == value[value.length - 1]
  149. })
  150. item.valueName.push(exist.label)
  151. } else {
  152. if(value.length == 0){
  153. item.valueName = []
  154. } else {
  155. _.each(item._value_bak, (v, i)=>{
  156. if(value.indexOf(v) === -1){
  157. item.valueName.splice(i, 1)
  158. }
  159. })
  160. }
  161. }
  162. item._value_bak = _.assign([], value)
  163. }
  164. if(item.onChange){
  165. if(item.multiple){
  166. item.onChange(value, item.valueName)
  167. } else {
  168. var exist = _.filter(item.optionList, (v)=>{
  169. return v.value == value
  170. })
  171. item.onChange(exist)
  172. }
  173. }
  174. },
  175. onCheckboxChange(values, item){
  176. if(item.onChange){
  177. var exist = []
  178. values.forEach(m=>{
  179. exist = exist.concat(_.filter(item.optionList, (v)=>{
  180. return v.value == m
  181. }))
  182. })
  183. item.onChange(exist)
  184. }
  185. },
  186. //富文本编辑 新增 start
  187. // resetEditorOption(editorOption){
  188. // if(editorOption){
  189. // return editorOption
  190. // }
  191. // return defaultEditorOption
  192. // },
  193. onEditorBlur(editor, item) {
  194. this.$emit('onEditorBlur', {editor, item})
  195. },
  196. onEditorFocus(editor, item) {
  197. this.$emit('onEditorFocus', {editor, item})
  198. },
  199. onEditorReady(editor, item) {
  200. this.$emit('onEditorReady', {editor, item})
  201. },
  202. onEditorChange({editor, html, text}, item) {
  203. this.$emit('onEditorChange', {editor, html, text, item})
  204. },
  205. // newEditorbeforeupload(file){
  206. // var $uptypes = ["image/jpg", "image/png",'image/jpeg'];
  207. // const isJPG = $uptypes.indexOf(file.type) > -1;
  208. // const isLt3M = file.size / 1024 / 1024 < 3;
  209. // this.uploadLoading = this.$loading({
  210. // background: 'rgba(0, 0, 0, 0.2)',
  211. // text: '上传中'
  212. // })
  213. // if (!isJPG) {
  214. // this.uploadLoading.close()
  215. // this.$message.error("上传头像图片只能是 JPG/PNG 格式!");
  216. // }
  217. // if (!isLt3M) {
  218. // this.uploadLoading.close()
  219. // this.$message.error("上传头像图片大小不能超过 3MB!");
  220. // }
  221. // return isJPG && isLt3M;
  222. // },
  223. // newEditorSuccess(res, file, fileList){
  224. // var vm = this
  225. // if(res.status==200){
  226. // var image = Tools.formatImgUrl(res.obj.fullUri),
  227. // addImgRange = this.$refs.formQuillTextEditor.quill.getSelection()
  228. // var length = addImgRange.index,quill = vm.$refs.formQuillTextEditor.quill
  229. // quill.insertEmbed(addImgRange != null?length:0, 'image',image)
  230. // quill.setSelection(length + 1);
  231. // }else{
  232. // vm.$message.error("图片上传失败!");
  233. // }
  234. // if (this.uploadLoading) {
  235. // this.uploadLoading.close()
  236. // }
  237. // },
  238. // addQuillTitle () {
  239. // const oToolBar = document.querySelector('.ql-toolbar')
  240. // const aButton = oToolBar.querySelectorAll('button')
  241. // const aSelect = oToolBar.querySelectorAll('select')
  242. // aButton.forEach(function (item) {
  243. // if (item.className === 'ql-script') {
  244. // item.value === 'sub' ? (item.title = '下标') : (item.title = '上标')
  245. // } else if (item.className === 'ql-indent') {
  246. // item.value === '+1' ? (item.title = '向右缩进') : (item.title = '向左缩进')
  247. // } else {
  248. // item.title = titleConfig[item.classList[0]]
  249. // }
  250. // })
  251. // aSelect.forEach(function (item) {
  252. // item.parentNode.title = titleConfig[item.classList[0]]
  253. // })
  254. // },
  255. //富文本编辑 新增 end
  256. }
  257. }
  258. </script>
  259. <style scoped lang="scss">
  260. .form-com{
  261. .el-form-item{
  262. width: auto;
  263. /deep/ .el-form-item__content{
  264. text-align: left;
  265. }
  266. /deep/ .ql-editor{
  267. min-height: 150px;
  268. }
  269. }
  270. }
  271. </style>