list.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. export default {
  2. data() {
  3. return {
  4. empty: false,
  5. loading: false,
  6. finished: false,
  7. page: 0,
  8. totalElements: 0,
  9. size: 20
  10. };
  11. },
  12. methods: {
  13. getData(isFirst = false, scrollTop = 0) {
  14. if (isFirst) {
  15. this.page = 0;
  16. this.list = [];
  17. this.$root.$el.scrollTop = scrollTop;
  18. }
  19. this.loading = true;
  20. this.finished = false;
  21. this.empty = false;
  22. let data = { page: this.page, size: this.size, sort: 'createdAt,desc' };
  23. if (this.beforeData) {
  24. data = {
  25. ...data,
  26. ...this.beforeData()
  27. };
  28. }
  29. if (this.httpType === 'get') {
  30. return this.$http.get(this.url, data, { body: 'json' }).then(res => {
  31. if (res.first) {
  32. this.list = [];
  33. }
  34. this.list = [...this.list, ...res.content];
  35. this.empty = res.empty;
  36. this.loading = false;
  37. this.finished = res.last;
  38. if (!this.finished) {
  39. this.page = this.page + 1;
  40. }
  41. this.totalElements = Number(res.totalElements);
  42. });
  43. } else {
  44. return this.$http.post(this.url, data, { body: 'json' }).then(res => {
  45. if (res.first) {
  46. this.list = [];
  47. }
  48. this.list = [...this.list, ...res.content];
  49. this.empty = res.empty;
  50. this.loading = false;
  51. this.finished = res.last;
  52. if (!this.finished) {
  53. this.page = this.page + 1;
  54. }
  55. this.totalElements = Number(res.totalElements);
  56. });
  57. }
  58. }
  59. }
  60. };