AdvancedTable.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <div>
  3. <table class="table">
  4. <thead>
  5. <tr>
  6. <th
  7. v-for="column in columns"
  8. :key="column.name"
  9. :class="{ sortable: column.sortable }"
  10. @click="changeSort(column)"
  11. >
  12. {{ column.displayName }}
  13. <span
  14. v-if="column.sortable && sort[column.sortProperty]"
  15. >({{ sort[column.sortProperty] }})</span
  16. >
  17. </th>
  18. </tr>
  19. </thead>
  20. <tbody>
  21. <tr v-for="item in data" :key="item._id">
  22. <td
  23. v-for="column in columns"
  24. :key="`${item._id}-${column.name}`"
  25. >
  26. <slot
  27. :name="`column-${column.name}`"
  28. :item="item"
  29. ></slot>
  30. </td>
  31. </tr>
  32. </tbody>
  33. </table>
  34. <br />
  35. <div class="control">
  36. <label class="label">Items per page</label>
  37. <p class="control select">
  38. <select v-model.number="pageSize" @change="getData()">
  39. <option value="10">10</option>
  40. <option value="25">25</option>
  41. <option value="50">50</option>
  42. <option value="100">100</option>
  43. <option value="250">250</option>
  44. <option value="500">500</option>
  45. <option value="1000">1000</option>
  46. </select>
  47. </p>
  48. </div>
  49. <br />
  50. <p>Page {{ page }} / {{ lastPage }}</p>
  51. <br />
  52. <button class="button is-primary" @click="changePage(page - 1)">
  53. Go to previous page</button
  54. >&nbsp;
  55. <button class="button is-primary" @click="changePage(page + 1)">
  56. Go to next page</button
  57. >&nbsp;
  58. <button class="button is-primary" @click="changePage(1)">
  59. Go to first page</button
  60. >&nbsp;
  61. <button class="button is-primary" @click="changePage(lastPage)">
  62. Go to last page
  63. </button>
  64. </div>
  65. </template>
  66. <script>
  67. import { mapGetters } from "vuex";
  68. import Toast from "toasters";
  69. import ws from "@/ws";
  70. export default {
  71. props: {
  72. columns: { type: Array, default: null },
  73. dataAction: { type: String, default: null }
  74. },
  75. data() {
  76. return {
  77. page: 1,
  78. pageSize: 10,
  79. data: [],
  80. count: 0, // TODO Rename
  81. sort: {
  82. title: "ascending"
  83. }
  84. };
  85. },
  86. computed: {
  87. properties() {
  88. return Array.from(
  89. new Set(this.columns.flatMap(column => column.properties))
  90. );
  91. },
  92. lastPage() {
  93. return Math.ceil(this.count / this.pageSize);
  94. },
  95. ...mapGetters({
  96. socket: "websockets/getSocket"
  97. })
  98. },
  99. mounted() {
  100. ws.onConnect(this.init);
  101. },
  102. methods: {
  103. init() {
  104. this.getData();
  105. },
  106. getData() {
  107. this.socket.dispatch(
  108. this.dataAction,
  109. this.page,
  110. this.pageSize,
  111. this.properties,
  112. this.sort,
  113. res => {
  114. console.log(111, res);
  115. new Toast(res.message);
  116. if (res.status === "success") {
  117. const { data, count } = res.data;
  118. this.data = data;
  119. this.count = count;
  120. }
  121. }
  122. );
  123. },
  124. changePage(page) {
  125. if (page < 1) return;
  126. if (page > this.lastPage) return;
  127. if (page === this.page) return;
  128. this.page = page;
  129. this.getData();
  130. },
  131. changeSort(column) {
  132. if (column.sortable) {
  133. if (this.sort[column.sortProperty] === undefined)
  134. this.sort[column.sortProperty] = "ascending";
  135. else if (this.sort[column.sortProperty] === "ascending")
  136. this.sort[column.sortProperty] = "descending";
  137. else if (this.sort[column.sortProperty] === "descending")
  138. delete this.sort[column.sortProperty];
  139. this.getData();
  140. }
  141. }
  142. }
  143. };
  144. </script>
  145. <style lang="scss" scoped>
  146. .table {
  147. .sortable {
  148. cursor: pointer;
  149. }
  150. }
  151. </style>