form.vue 8.2 KB

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