index.js 2.1 KB

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