2
0

AdvancedTable.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <template>
  2. <div>
  3. <div>
  4. <button
  5. v-for="column in orderedColumns"
  6. :key="column.name"
  7. class="button"
  8. @click="toggleColumnVisibility(column)"
  9. >
  10. {{
  11. `${
  12. this.enabledColumns.indexOf(column.name) !== -1
  13. ? "Hide"
  14. : "Show"
  15. } ${column.name} column`
  16. }}
  17. </button>
  18. </div>
  19. <table class="table">
  20. <thead>
  21. <draggable
  22. item-key="name"
  23. v-model="orderedColumns"
  24. v-bind="columnDragOptions"
  25. tag="tr"
  26. >
  27. <template #item="{ element: column }">
  28. <th
  29. :class="{ sortable: column.sortable }"
  30. v-if="enabledColumns.indexOf(column.name) !== -1"
  31. @click="changeSort(column)"
  32. >
  33. {{ column.displayName }}
  34. <span
  35. v-if="
  36. column.sortable && sort[column.sortProperty]
  37. "
  38. >({{ sort[column.sortProperty] }})</span
  39. >
  40. <input
  41. v-if="column.sortable"
  42. placeholder="Filter"
  43. @click.stop
  44. @keyup.enter="changeFilter(column, $event)"
  45. />
  46. </th>
  47. </template>
  48. </draggable>
  49. </thead>
  50. <tbody>
  51. <tr
  52. v-for="(item, itemIndex) in data"
  53. :key="item._id"
  54. :class="{
  55. selected: item.selected,
  56. highlighted: item.highlighted
  57. }"
  58. @click="clickItem(itemIndex, $event)"
  59. >
  60. <td
  61. v-for="column in sortedFilteredColumns"
  62. :key="`${item._id}-${column.name}`"
  63. >
  64. <slot
  65. :name="`column-${column.name}`"
  66. :item="item"
  67. ></slot>
  68. </td>
  69. </tr>
  70. </tbody>
  71. </table>
  72. <br />
  73. <div class="control">
  74. <label class="label">Items per page</label>
  75. <p class="control select">
  76. <select v-model.number="pageSize" @change="getData()">
  77. <option value="10">10</option>
  78. <option value="25">25</option>
  79. <option value="50">50</option>
  80. <option value="100">100</option>
  81. <option value="250">250</option>
  82. <option value="500">500</option>
  83. <option value="1000">1000</option>
  84. </select>
  85. </p>
  86. </div>
  87. <br />
  88. <p>Page {{ page }} / {{ lastPage }}</p>
  89. <br />
  90. <button class="button is-primary" @click="changePage(page - 1)">
  91. Go to previous page</button
  92. >&nbsp;
  93. <button class="button is-primary" @click="changePage(page + 1)">
  94. Go to next page</button
  95. >&nbsp;
  96. <button class="button is-primary" @click="changePage(1)">
  97. Go to first page</button
  98. >&nbsp;
  99. <button class="button is-primary" @click="changePage(lastPage)">
  100. Go to last page
  101. </button>
  102. </div>
  103. </template>
  104. <script>
  105. import { mapGetters } from "vuex";
  106. import draggable from "vuedraggable";
  107. import Toast from "toasters";
  108. import ws from "@/ws";
  109. export default {
  110. components: {
  111. draggable
  112. },
  113. props: {
  114. columns: { type: Array, default: null },
  115. dataAction: { type: String, default: null }
  116. },
  117. data() {
  118. return {
  119. page: 1,
  120. pageSize: 10,
  121. data: [],
  122. count: 0, // TODO Rename
  123. sort: {},
  124. filter: {},
  125. orderedColumns: [],
  126. enabledColumns: [],
  127. columnDragOptions() {
  128. return {
  129. animation: 200,
  130. group: "columns",
  131. disabled: false,
  132. ghostClass: "draggable-list-ghost",
  133. filter: ".ignore-elements",
  134. fallbackTolerance: 50
  135. };
  136. }
  137. };
  138. },
  139. computed: {
  140. properties() {
  141. return Array.from(
  142. new Set(
  143. this.sortedFilteredColumns.flatMap(
  144. column => column.properties
  145. )
  146. )
  147. );
  148. },
  149. lastPage() {
  150. return Math.ceil(this.count / this.pageSize);
  151. },
  152. sortedFilteredColumns() {
  153. return this.orderedColumns.filter(
  154. column => this.enabledColumns.indexOf(column.name) !== -1
  155. );
  156. },
  157. lastSelectedItemIndex() {
  158. return this.data.findIndex(item => item.highlighted);
  159. },
  160. ...mapGetters({
  161. socket: "websockets/getSocket"
  162. })
  163. },
  164. mounted() {
  165. this.orderedColumns = this.columns;
  166. this.enabledColumns = this.columns.map(column => column.name);
  167. ws.onConnect(this.init);
  168. },
  169. methods: {
  170. init() {
  171. this.getData();
  172. },
  173. getData() {
  174. this.socket.dispatch(
  175. this.dataAction,
  176. this.page,
  177. this.pageSize,
  178. this.properties,
  179. this.sort,
  180. this.filter,
  181. res => {
  182. console.log(111, res);
  183. new Toast(res.message);
  184. if (res.status === "success") {
  185. const { data, count } = res.data;
  186. this.data = data;
  187. this.count = count;
  188. }
  189. }
  190. );
  191. },
  192. changePage(page) {
  193. if (page < 1) return;
  194. if (page > this.lastPage) return;
  195. if (page === this.page) return;
  196. this.page = page;
  197. this.getData();
  198. },
  199. changeSort(column) {
  200. if (column.sortable) {
  201. const { sortProperty } = column;
  202. if (this.sort[sortProperty] === undefined)
  203. this.sort[sortProperty] = "ascending";
  204. else if (this.sort[sortProperty] === "ascending")
  205. this.sort[sortProperty] = "descending";
  206. else if (this.sort[sortProperty] === "descending")
  207. delete this.sort[sortProperty];
  208. this.getData();
  209. }
  210. },
  211. changeFilter(column, event) {
  212. if (column.filterable) {
  213. const { value } = event.target;
  214. const { filterProperty } = column;
  215. if (this.filter[filterProperty] !== undefined && value === "") {
  216. delete this.filter[filterProperty];
  217. } else if (this.filter[filterProperty] !== value) {
  218. this.filter[filterProperty] = value;
  219. } else return;
  220. this.getData();
  221. }
  222. },
  223. toggleColumnVisibility(column) {
  224. if (this.enabledColumns.indexOf(column.name) !== -1) {
  225. this.enabledColumns.splice(
  226. this.enabledColumns.indexOf(column.name),
  227. 1
  228. );
  229. } else {
  230. this.enabledColumns.push(column.name);
  231. }
  232. this.getData();
  233. },
  234. clickItem(itemIndex, event) {
  235. const { shiftKey, ctrlKey } = event;
  236. // Shift was pressed, so attempt to select all items between the clicked item and last clicked item
  237. if (shiftKey) {
  238. // If there is a last clicked item
  239. if (this.lastSelectedItemIndex >= 0) {
  240. // Clicked item is lower than last item, so select upwards until it reaches the last selected item
  241. if (itemIndex > this.lastSelectedItemIndex) {
  242. for (
  243. let itemIndexUp = itemIndex;
  244. itemIndexUp > this.lastSelectedItemIndex;
  245. itemIndexUp -= 1
  246. ) {
  247. this.data[itemIndexUp].selected = true;
  248. }
  249. }
  250. // Clicked item is higher than last item, so select downwards until it reaches the last selected item
  251. else if (itemIndex < this.lastSelectedItemIndex) {
  252. for (
  253. let itemIndexDown = itemIndex;
  254. itemIndexDown < this.lastSelectedItemIndex;
  255. itemIndexDown += 1
  256. ) {
  257. this.data[itemIndexDown].selected = true;
  258. }
  259. }
  260. }
  261. }
  262. // Ctrl was pressed, so toggle selected on the clicked item
  263. else if (ctrlKey) {
  264. this.data[itemIndex].selected = !this.data[itemIndex].selected;
  265. }
  266. // Neither ctrl nor shift were pressed, so unselect all items and set the clicked item to selected
  267. else {
  268. this.data = this.data.map(item => ({
  269. ...item,
  270. selected: false
  271. }));
  272. this.data[itemIndex].selected = true;
  273. }
  274. // Set the last clicked item to no longer be highlighted, if it exists
  275. if (this.lastSelectedItemIndex >= 0)
  276. this.data[this.lastSelectedItemIndex].highlighted = false;
  277. // Set the clicked item to be highlighted
  278. this.data[itemIndex].highlighted = true;
  279. }
  280. }
  281. };
  282. </script>
  283. <style lang="scss" scoped>
  284. .table {
  285. thead {
  286. tr {
  287. th {
  288. &.sortable {
  289. cursor: pointer;
  290. }
  291. }
  292. }
  293. }
  294. tbody {
  295. tr {
  296. &.selected {
  297. outline: 1px solid red;
  298. }
  299. &.highlighted {
  300. outline: 1px solid blue;
  301. }
  302. &.selected.highlighted {
  303. outline: 1px solid green;
  304. }
  305. }
  306. }
  307. }
  308. </style>