index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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', 'noclear'],
  18. data() {
  19. return {
  20. searchs: null
  21. }
  22. },
  23. mounted() {
  24. this.setFormValue()
  25. },
  26. methods: {
  27. setFormValue() {
  28. const searchs = new Searchs(this.saveKey || this.$route.path)
  29. this.searchs = searchs
  30. const active = searchs.get()
  31. for (const key in active.form) {
  32. if (active.form.hasOwnProperty(key)) {
  33. const item = active.form[key]
  34. this.model[key] = item
  35. }
  36. }
  37. if (this.saveKey) {
  38. this.searchs.update(this.$route.path, undefined, 'bind')
  39. }
  40. },
  41. submit(evt) {
  42. evt.stopPropagation()
  43. evt.stopImmediatePropagation()
  44. evt.preventDefault()
  45. if (this.saveKey) {
  46. this.searchs.update(this.$route.path, undefined, 'bind')
  47. }
  48. this.searchs.update(this.model, undefined, 'form')
  49. // console.log( new Searchs().searchs)
  50. if (this.$listeners.submit) {
  51. this.$listeners.submit(evt)
  52. }
  53. },
  54. reset(evt) {
  55. evt.stopPropagation()
  56. evt.stopImmediatePropagation()
  57. evt.preventDefault()
  58. let setItemEvent = new Event('watchStorage')
  59. window.dispatchEvent(setItemEvent)
  60. if (this.$listeners.reset) {
  61. if (this.noclear == undefined) {
  62. this.$nextTick(() => {
  63. this.resetFields()
  64. // this.setFormValue()
  65. })
  66. } else {
  67. this.resetFields()
  68. }
  69. this.$listeners.reset()
  70. } else {
  71. this.resetFields()
  72. }
  73. },
  74. save(search = null, type = 'form') {
  75. search = search ? search : this.model
  76. this.searchs.update(search, undefined, type)
  77. },
  78. validate(FC) {
  79. this.$refs.form.validate(valid => {
  80. FC(valid)
  81. if (valid) {
  82. this.searchs.update(this.model, undefined, 'form')
  83. }
  84. })
  85. },
  86. resetFields() {
  87. this.$refs.form.resetFields()
  88. this.searchs.update(this.model, undefined, 'form')
  89. this.searchs.update({}, undefined, 'page')
  90. }
  91. },
  92. }
  93. </script>
  94. <style lang="less" scoped>
  95. .save-form{
  96. /deep/ .el-input__inner{
  97. width: 180px;
  98. }
  99. /deep/.el-date-editor.el-input{
  100. width: 180px;
  101. }
  102. /deep/ .el-form-item__content .el-col {
  103. width: 180px;
  104. }
  105. /deep/ .el-col.line{
  106. width: 15px!important;
  107. text-align: center;
  108. }
  109. /deep/ .el-date-editor--daterange{
  110. width: 375px;
  111. }
  112. /deep/ .el-form-item__content .el-col:last-child .el-form-item{
  113. margin-right: 0;
  114. }
  115. }
  116. </style>