index.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. export default {
  2. data () {
  3. return {
  4. show: false,
  5. currObj: {}
  6. }
  7. },
  8. computed: {
  9. isAdd () {
  10. return !this.currObj.id
  11. },
  12. dialogTitle () {
  13. return this.isAdd ? `新增${this.type}` : `编辑${this.type}`
  14. }
  15. },
  16. created () {
  17. this.getList()
  18. },
  19. methods: {
  20. getDefaults () {
  21. return { name: '', remark: '' }
  22. },
  23. transform (data) {
  24. return data
  25. },
  26. toAdd () {
  27. this.currObj = this.getDefaults()
  28. this.show = true
  29. },
  30. beforeEdit ({ id, name, remark }) {
  31. return { id, name, remark }
  32. },
  33. toEdit (item) {
  34. this.currObj = this.beforeEdit(item)
  35. this.$currObj = { ...this.currObj }
  36. this.show = true
  37. },
  38. close () {
  39. this.$currObj = null
  40. this.show = false
  41. },
  42. check () {
  43. return false
  44. },
  45. search () {
  46. const options = this.options
  47. options.list = []
  48. options.totalCount = 0
  49. options.params.pageNum = 1
  50. this.getList()
  51. },
  52. getList () {
  53. const options = this.options
  54. options.error = false
  55. options.loading = true
  56. this.proxy.list(options.params).then(({ data, totalCount }) => {
  57. options.list = this.transform(data)
  58. options.totalCount = totalCount
  59. }, () => {
  60. options.error = true
  61. options.list = []
  62. }).finally(() => {
  63. options.loading = false
  64. })
  65. },
  66. afterAdd () { },
  67. save () {
  68. if (this.check(this.currObj)) {
  69. if (!this.isAdd && Object.keys(this.$currObj).every(key => this.currObj[key] === this.$currObj[key])) {
  70. this.close()
  71. return
  72. }
  73. this.proxy[this.isAdd ? 'add' : 'update'](this.currObj).then(() => {
  74. if (this.isAdd) {
  75. this.afterAdd()
  76. this.search()
  77. } else {
  78. this.getList()
  79. }
  80. this.close()
  81. })
  82. }
  83. },
  84. toDel (item) {
  85. const options = this.options || this
  86. this.proxy.del(item).then(() => {
  87. if (options.list.length === 1 && options.params.pageNum > 1) {
  88. options.params.pageNum -= 1
  89. }
  90. this.getList()
  91. })
  92. }
  93. }
  94. }