p-form.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <template>
  2. <div class="form-box">
  3. <div class="inner-box">
  4. <el-form
  5. :model="formData"
  6. :ref="form.formName"
  7. class="demo-dynamic"
  8. :label-width="form.labelWidth"
  9. :style="form.formStyle"
  10. >
  11. <el-form-item
  12. v-for="(item, index) in form.body"
  13. :label="item.label"
  14. :key="index"
  15. :rules="item.rules"
  16. :prop="item.value"
  17. >
  18. <el-input
  19. style="display: inline-block"
  20. v-if="item.el == 'input'"
  21. v-model="formData[item.value]"
  22. :placeholder="item.placeholder"
  23. :disabled="item.disabled"
  24. :clearable="item.clearable"
  25. ></el-input>
  26. <el-checkbox-group
  27. v-else-if="item.el == 'checkbox'"
  28. :placeholder="item.placeholder"
  29. v-model="formData[item.value]"
  30. :disabled="item.disabled"
  31. :clearable="item.clearable"
  32. >
  33. <el-checkbox
  34. v-for="items in item.options"
  35. :label="items.label"
  36. :key="items.value"
  37. >{{ items.label }}</el-checkbox
  38. >
  39. </el-checkbox-group>
  40. <el-select
  41. v-else-if="item.el == 'select'"
  42. v-model="formData[item.value]"
  43. :placeholder="item.placeholder"
  44. :multiple="item.multiple || false"
  45. filterable
  46. style="width: 100%"
  47. :clearable="item.clearable"
  48. >
  49. <el-option
  50. v-for="items in item.options"
  51. :key="items.value"
  52. :label="items.label"
  53. :value="items.value"
  54. :disabled="items.disabled"
  55. >
  56. </el-option>
  57. </el-select>
  58. <el-switch
  59. v-else-if="item.el == 'switch'"
  60. v-model="formData[item.value]"
  61. ></el-switch>
  62. <el-radio-group
  63. v-else-if="item.el == 'radio'"
  64. v-model="formData[item.value]"
  65. :clearable="item.clearable"
  66. >
  67. <el-radio
  68. v-for="items in item.options"
  69. :key="items.value"
  70. :label="items.value"
  71. :disabled="items.disabled"
  72. >{{ items.label }}</el-radio
  73. >
  74. </el-radio-group>
  75. <el-date-picker
  76. v-else-if="item.el == 'datetime'"
  77. style="width:100% !important"
  78. v-model="formData[item.value]"
  79. :placeholder="item.placeholder"
  80. value-format="YYYY-MM-DD"
  81. type="datetime"
  82. ></el-date-picker>
  83. <el-input
  84. v-else-if="item.el == 'password'"
  85. v-model="formData[item.value]"
  86. :placeholder="item.placeholder"
  87. :disabled="item.disabled"
  88. :clearable="item.clearable"
  89. :type="inputType"
  90. >
  91. <template slot="suffix">
  92. <el-tooltip
  93. class="item"
  94. effect="dark"
  95. content="点击查看密码"
  96. placement="top-start"
  97. >
  98. <i
  99. @click="changeView"
  100. :class="
  101. inputType == 'text'
  102. ? 'el-input__icon el-icon-success'
  103. : 'el-input__icon el-icon-view'
  104. "
  105. ></i>
  106. </el-tooltip>
  107. </template>
  108. </el-input>
  109. </el-form-item>
  110. <el-form-item v-if="form.action" style="text-align: right;">
  111. <el-button type="primary" @click="submitForm(form.formName)"
  112. >{{form.subBtn}}</el-button
  113. >
  114. <el-button @click="resetForm(form.formName)">{{form.restBtn}}</el-button>
  115. </el-form-item>
  116. </el-form>
  117. </div>
  118. </div>
  119. </template>
  120. <script>
  121. // form: {
  122. // initUrl: "/sysmgr/sys/login/addInit", // 初始化接口
  123. // url: "/market/cfgDataDict/queryList", // 提交接口
  124. // id: "", // 编辑时用的id 由初始化时获取
  125. // type: "post", // 请求方式
  126. // params: "", // get请求桉树
  127. // events: function () {
  128. // // 回调方法
  129. // $vm.$router.push("/leader");
  130. // },
  131. // data: {
  132. // // post请求参数
  133. // groupId: "",
  134. // groupName: "",
  135. // loginNameStr: "",
  136. // loginNoStr: "",
  137. // loginPasswd: "",
  138. // tenantId: null,
  139. // },
  140. // body: [
  141. // // 渲染表单
  142. // {
  143. // el: "input", // dom类型
  144. // placeholder: "请输入", // 提示语
  145. // label: "输入框", // 表单title
  146. // value: "loginNoStr", // 表单model绑定的值
  147. // rules: [
  148. // // 校验
  149. // { required: true, message: "请输入工号", trigger: "blur" },
  150. // ],
  151. // },
  152. // {
  153. // el: "checkbox", // input radio checkbox select datePicker timePicker switch
  154. // placeholder: "请输入",
  155. // label: "复选框",
  156. // value: "checkbox",
  157. // rules: [
  158. // { required: true, message: "请输入邮箱地址", trigger: "blur" },
  159. // ],
  160. // options: [
  161. // {
  162. // label: "选项1",
  163. // value: "1",
  164. // disabled: false,
  165. // },
  166. // {
  167. // label: "选项2",
  168. // value: "2",
  169. // disabled: false,
  170. // },
  171. // ],
  172. // },
  173. // {
  174. // el: "select", // input radio checkbox select datePicker timePicker switch
  175. // placeholder: "请输入",
  176. // value: "select",
  177. // label: "下拉框",
  178. // rules: [
  179. // { required: true, message: "请输入邮箱地址", trigger: "blur" },
  180. // ],
  181. // options: [
  182. // {
  183. // label: "选项1",
  184. // value: "1",
  185. // disabled: false,
  186. // },
  187. // {
  188. // label: "选项2",
  189. // value: "2",
  190. // disabled: false,
  191. // },
  192. // ],
  193. // },
  194. // {
  195. // el: "switch", // input radio checkbox select datePicker timePicker switch
  196. // placeholder: "请输入",
  197. // value: "switch",
  198. // label: "switch",
  199. // rules: [
  200. // { required: true, message: "请输入邮箱地址", trigger: "blur" },
  201. // ],
  202. // },
  203. // {
  204. // el: "radio", // input radio checkbox select datePicker timePicker switch
  205. // placeholder: "请输入",
  206. // label: "单选框",
  207. // value: "radio",
  208. // rules: [
  209. // { required: true, message: "请输入邮箱地址", trigger: "blur" },
  210. // ],
  211. // options: [
  212. // {
  213. // label: "选项1",
  214. // value: "1",
  215. // disabled: false,
  216. // },
  217. // {
  218. // label: "选项2",
  219. // value: "2",
  220. // disabled: false,
  221. // },
  222. // ],
  223. // },
  224. // ],
  225. // },
  226. export default {
  227. props: {
  228. formData: {
  229. required: true,
  230. type: Object,
  231. },
  232. form: {
  233. required: true,
  234. type: Object,
  235. },
  236. },
  237. data() {
  238. return {
  239. inputType: "password",
  240. };
  241. },
  242. methods: {
  243. changeView() {
  244. if (this.inputType == "password") {
  245. this.inputType = "text";
  246. } else {
  247. this.inputType = "password";
  248. }
  249. },
  250. submitForm(formName) {
  251. this.$refs[formName].validate((valid) => {
  252. if (valid) {
  253. let config = {
  254. url: this.form.url,
  255. method: this.form.type,
  256. };
  257. if (this.form.type == "post") {
  258. config.headers = {
  259. "Content-Type": "application/json",
  260. };
  261. config.data = this.formData;
  262. } else {
  263. config = {
  264. url: this.form.url,
  265. method: this.form.type,
  266. params: this.form.params,
  267. };
  268. }
  269. this.$http(config).then((res) => {
  270. if (res.data.result == 0) {
  271. this.$notify({
  272. title: "成功",
  273. message: res.data.desc,
  274. type: "success",
  275. });
  276. this.form.events();
  277. this.$emit("closeDialog");
  278. }
  279. });
  280. } else {
  281. console.log("error submit!!");
  282. return false;
  283. }
  284. });
  285. },
  286. resetForm(formName) {
  287. this.form.body.map((item) => {
  288. item.disabled = false;
  289. });
  290. this.$refs[formName].resetFields();
  291. this.$emit("closeDialog");
  292. },
  293. initData() {
  294. this.$http({
  295. url: this.form.initUrl,
  296. method: "post",
  297. headers: {
  298. "Content-Type": "application/json",
  299. },
  300. data: this.form.data,
  301. }).then((res) => {
  302. console.log(res);
  303. });
  304. },
  305. },
  306. mounted() {
  307. // this.initData();
  308. },
  309. };
  310. </script>