index.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <el-form
  3. class="save-form"
  4. v-bind="{...$attrs, ...$props}"
  5. v-on="$listeners"
  6. @submit.stop.native="submit"
  7. @reset.stop.native="reset"
  8. ref="form"
  9. >
  10. <slot/>
  11. </el-form>
  12. </template>
  13. <script>
  14. import { Searchs } from '@/helpers'
  15. export default {
  16. name: 'save-form',
  17. props: ['model', 'save-key'],
  18. data() {
  19. return {
  20. searchs: null
  21. }
  22. },
  23. mounted() {
  24. const searchs = new Searchs(this.saveKey || this.$route.path)
  25. this.searchs = searchs
  26. const active = searchs.get()
  27. for (const key in active.form) {
  28. if (active.form.hasOwnProperty(key)) {
  29. const item = active.form[key]
  30. this.model[key] = item
  31. }
  32. }
  33. if (this.saveKey) {
  34. this.searchs.update(this.$route.path, undefined, 'bind')
  35. }
  36. },
  37. methods: {
  38. submit(evt) {
  39. evt.stopPropagation()
  40. evt.stopImmediatePropagation()
  41. evt.preventDefault()
  42. this.searchs.update(this.model, undefined, 'form')
  43. if (this.$listeners.submit) {
  44. this.$listeners.submit(evt)
  45. }
  46. },
  47. reset() {
  48. if (this.$listeners.reset) {
  49. this.$listeners.reset()
  50. this.$nextTick(() => {
  51. this.resetFields()
  52. })
  53. } else {
  54. this.resetFields()
  55. }
  56. },
  57. save(search = null, type = 'form') {
  58. search = search ? search : this.model
  59. this.searchs.update(search, undefined, type)
  60. },
  61. validate(FC) {
  62. this.$refs.form.validate(valid => {
  63. FC(valid)
  64. if (valid) {
  65. this.searchs.update(this.model, undefined, 'form')
  66. }
  67. })
  68. },
  69. resetFields() {
  70. this.$refs.form.resetFields()
  71. this.searchs.update(this.model, undefined, 'form')
  72. this.searchs.update({}, undefined, 'page')
  73. }
  74. },
  75. }
  76. </script>
  77. <style lang="less" scoped>
  78. .save-form{
  79. /deep/ .el-input__inner{
  80. width: 180px;
  81. }
  82. /deep/ .el-form-item__content .el-col {
  83. width: 180px;
  84. }
  85. /deep/ .el-col.line{
  86. width: 15px!important;
  87. text-align: center;
  88. }
  89. /deep/ .el-date-editor--daterange{
  90. width: 375px;
  91. }
  92. /deep/ .el-form-item__content .el-col:last-child .el-form-item{
  93. margin-right: 0;
  94. }
  95. }
  96. </style>