2
0

AdvancedTable.vue 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. <template>
  2. <div>
  3. <div
  4. class="table-outer-container"
  5. @mousemove="columnResizingMouseMove($event)"
  6. >
  7. <div class="table-header">
  8. <div>
  9. <tippy
  10. v-if="filters.length > 0"
  11. :touch="true"
  12. :interactive="true"
  13. placement="bottom"
  14. theme="search"
  15. ref="search"
  16. trigger="click"
  17. >
  18. <button class="button is-info">
  19. <i class="material-icons icon-with-button"
  20. >filter_list</i
  21. >
  22. Filters
  23. </button>
  24. <template #content>
  25. <div class="control is-grouped input-with-button">
  26. <p class="control select is-expanded">
  27. <select v-model="addFilterValue">
  28. <option
  29. v-for="type in filters"
  30. :key="type.name"
  31. :value="type"
  32. >
  33. {{ type.displayName }}
  34. </option>
  35. </select>
  36. </p>
  37. <p class="control">
  38. <button
  39. class="button material-icons is-success"
  40. @click="addFilterItem()"
  41. >
  42. control_point
  43. </button>
  44. </p>
  45. </div>
  46. <div
  47. v-for="(filter, index) in editingFilters"
  48. :key="`filter-${index}`"
  49. class="control is-grouped is-expanded"
  50. >
  51. <div class="control select">
  52. <select
  53. v-model="filter.filter"
  54. @change="changeFilterType(index)"
  55. >
  56. <option
  57. v-for="type in filters"
  58. :key="type.name"
  59. :value="type"
  60. >
  61. {{ type.displayName }}
  62. </option>
  63. </select>
  64. </div>
  65. <div class="control select">
  66. <select
  67. v-model="filter.filterType"
  68. :disabled="!filter.filterType"
  69. >
  70. <option
  71. v-for="filterType in filterTypes(
  72. filter.filter
  73. )"
  74. :key="filterType.name"
  75. :value="filterType.name"
  76. :selected="
  77. filter.filter
  78. .defaultFilterType ===
  79. filterType.name
  80. "
  81. >
  82. {{ filterType.displayName }}
  83. </option>
  84. </select>
  85. </div>
  86. <p class="control is-expanded">
  87. <input
  88. v-model="filter.data"
  89. class="input"
  90. type="text"
  91. placeholder="Search value"
  92. :disabled="!filter.filterType"
  93. />
  94. </p>
  95. <div class="control">
  96. <button
  97. class="button material-icons is-danger"
  98. @click="removeFilterItem(index)"
  99. >
  100. remove_circle_outline
  101. </button>
  102. </div>
  103. </div>
  104. <div
  105. v-if="editingFilters.length > 1"
  106. class="control is-expanded is-grouped"
  107. >
  108. <label class="control label"
  109. >Filter operator</label
  110. >
  111. <div class="control select is-expanded">
  112. <select v-model="filterOperator">
  113. <option
  114. v-for="operator in filterOperators"
  115. :key="operator.name"
  116. :value="operator.name"
  117. >
  118. {{ operator.displayName }}
  119. </option>
  120. </select>
  121. </div>
  122. </div>
  123. <div
  124. class="advanced-filter-bottom"
  125. v-if="editingFilters.length > 0"
  126. >
  127. <div class="control is-expanded">
  128. <button
  129. class="button is-info"
  130. @click="applyFilterAndGetData()"
  131. >
  132. <i
  133. class="
  134. material-icons
  135. icon-with-button
  136. "
  137. >filter_list</i
  138. >
  139. Apply filters
  140. </button>
  141. </div>
  142. </div>
  143. <div
  144. class="advanced-filter-bottom"
  145. v-else-if="editingFilters.length === 0"
  146. >
  147. <div class="control is-expanded">
  148. <button
  149. class="button is-info"
  150. @click="applyFilterAndGetData()"
  151. >
  152. <i
  153. class="
  154. material-icons
  155. icon-with-button
  156. "
  157. >filter_list</i
  158. >
  159. Apply filters
  160. </button>
  161. </div>
  162. </div>
  163. </template>
  164. </tippy>
  165. <tippy
  166. v-if="appliedFilters.length > 0"
  167. :touch="true"
  168. :interactive="true"
  169. theme="info"
  170. ref="activeFilters"
  171. >
  172. <div class="filters-indicator">
  173. {{ appliedFilters.length }}
  174. <i class="material-icons" @click.prevent="true"
  175. >filter_list</i
  176. >
  177. </div>
  178. <template #content>
  179. <p
  180. v-for="(filter, index) in appliedFilters"
  181. :key="`filter-${index}`"
  182. >
  183. {{ filter.filter.displayName }}
  184. {{ filter.filterType }} "{{ filter.data }}"
  185. {{
  186. appliedFilters.length === index + 1
  187. ? ""
  188. : filterOperator
  189. }}
  190. </p>
  191. </template>
  192. </tippy>
  193. <i
  194. v-else
  195. class="filters-indicator material-icons"
  196. content="No active filters"
  197. v-tippy="{ theme: 'info' }"
  198. >
  199. filter_list_off
  200. </i>
  201. </div>
  202. <div>
  203. <tippy
  204. v-if="hidableSortedColumns.length > 0"
  205. :touch="true"
  206. :interactive="true"
  207. placement="bottom"
  208. theme="dropdown"
  209. ref="editColumns"
  210. trigger="click"
  211. >
  212. <a class="button is-info" @click.prevent="true">
  213. <i class="material-icons icon-with-button">tune</i>
  214. Columns
  215. </a>
  216. <template #content>
  217. <draggable
  218. item-key="name"
  219. v-model="orderedColumns"
  220. v-bind="columnDragOptions"
  221. tag="div"
  222. draggable=".item-draggable"
  223. class="nav-dropdown-items"
  224. >
  225. <template #item="{ element: column }">
  226. <button
  227. v-if="column.name !== 'select'"
  228. :class="{
  229. sortable: column.sortable,
  230. 'item-draggable': column.draggable,
  231. 'nav-item': true
  232. }"
  233. @click.prevent="
  234. toggleColumnVisibility(column)
  235. "
  236. >
  237. <p
  238. class="
  239. control
  240. is-expanded
  241. checkbox-control
  242. "
  243. >
  244. <label class="switch">
  245. <input
  246. v-if="column.hidable"
  247. type="checkbox"
  248. :id="index"
  249. :checked="
  250. shownColumns.indexOf(
  251. column.name
  252. ) !== -1
  253. "
  254. @click="
  255. toggleColumnVisibility(
  256. column
  257. )
  258. "
  259. />
  260. <span
  261. :class="{
  262. slider: true,
  263. round: true,
  264. disabled:
  265. !column.hidable
  266. }"
  267. ></span>
  268. </label>
  269. <label :for="index">
  270. <span></span>
  271. <p>{{ column.displayName }}</p>
  272. </label>
  273. </p>
  274. </button>
  275. </template>
  276. </draggable>
  277. </template>
  278. </tippy>
  279. </div>
  280. </div>
  281. <div class="table-container">
  282. <table class="table">
  283. <thead>
  284. <draggable
  285. item-key="name"
  286. v-model="orderedColumns"
  287. v-bind="columnDragOptions"
  288. tag="tr"
  289. draggable=".item-draggable"
  290. >
  291. <template #item="{ element: column }">
  292. <th
  293. v-if="
  294. !(
  295. column.name === 'select' &&
  296. data.length === 0
  297. ) &&
  298. shownColumns.indexOf(column.name) !== -1
  299. "
  300. :class="{
  301. sortable: column.sortable,
  302. 'item-draggable': column.draggable
  303. }"
  304. :style="{
  305. minWidth: `${column.minWidth}px`,
  306. width: `${column.width}px`,
  307. maxWidth: `${column.maxWidth}px`
  308. }"
  309. >
  310. <p
  311. v-if="column.name === 'select'"
  312. class="checkbox"
  313. >
  314. <input
  315. type="checkbox"
  316. :checked="
  317. data.length ===
  318. selectedRows.length
  319. "
  320. @click="toggleAllRows()"
  321. />
  322. </p>
  323. <div v-else>
  324. <span>
  325. {{ column.displayName }}
  326. </span>
  327. <span
  328. v-if="column.pinable"
  329. content="Toggle Pinned Column"
  330. v-tippy
  331. @click="togglePinnedColumn(column)"
  332. >
  333. <span
  334. :class="{
  335. 'material-icons': true,
  336. active:
  337. pinnedColumns.indexOf(
  338. column.name
  339. ) !== -1
  340. }"
  341. >
  342. push_pin
  343. </span>
  344. </span>
  345. <span
  346. v-if="column.sortable"
  347. :content="`Sort by ${column.displayName}`"
  348. v-tippy
  349. >
  350. <span
  351. v-if="
  352. !sort[column.sortProperty]
  353. "
  354. class="material-icons"
  355. @click="changeSort(column)"
  356. >
  357. unfold_more
  358. </span>
  359. <span
  360. v-if="
  361. sort[
  362. column.sortProperty
  363. ] === 'ascending'
  364. "
  365. class="material-icons active"
  366. @click="changeSort(column)"
  367. >
  368. expand_more
  369. </span>
  370. <span
  371. v-if="
  372. sort[
  373. column.sortProperty
  374. ] === 'descending'
  375. "
  376. class="material-icons active"
  377. @click="changeSort(column)"
  378. >
  379. expand_less
  380. </span>
  381. </span>
  382. </div>
  383. <div
  384. class="resizer"
  385. v-if="column.resizable"
  386. @mousedown.prevent.stop="
  387. columnResizingMouseDown(
  388. column,
  389. $event
  390. )
  391. "
  392. @mouseup="columnResizingMouseUp()"
  393. @dblclick="columnResetWidth(column)"
  394. ></div>
  395. </th>
  396. </template>
  397. </draggable>
  398. </thead>
  399. <tbody>
  400. <tr
  401. v-for="(item, itemIndex) in data"
  402. :key="item._id"
  403. :class="{
  404. selected: item.selected,
  405. highlighted: item.highlighted
  406. }"
  407. >
  408. <td
  409. v-for="column in sortedFilteredColumns"
  410. :key="`${item._id}-${column.name}`"
  411. >
  412. <slot
  413. :name="`column-${column.name}`"
  414. :item="item"
  415. v-if="
  416. column.properties.length === 0 ||
  417. column.properties.every(
  418. property =>
  419. item[property] !== undefined
  420. )
  421. "
  422. ></slot>
  423. <p
  424. class="checkbox"
  425. v-if="column.name === 'select'"
  426. >
  427. <input
  428. type="checkbox"
  429. :checked="item.selected"
  430. @click="
  431. toggleSelectedRow(itemIndex, $event)
  432. "
  433. />
  434. </p>
  435. <div
  436. class="resizer"
  437. v-if="column.resizable"
  438. @mousedown.prevent.stop="
  439. columnResizingMouseDown(column, $event)
  440. "
  441. @mouseup="columnResizingMouseUp()"
  442. @dblclick="columnResetWidth(column)"
  443. ></div>
  444. </td>
  445. </tr>
  446. </tbody>
  447. </table>
  448. </div>
  449. <div class="table-footer">
  450. <div class="page-controls">
  451. <button
  452. :class="{ disabled: page === 1 }"
  453. class="button is-primary material-icons"
  454. :disabled="page === 1"
  455. @click="changePage(1)"
  456. content="First Page"
  457. v-tippy
  458. >
  459. skip_previous
  460. </button>
  461. <button
  462. :class="{ disabled: page === 1 }"
  463. class="button is-primary material-icons"
  464. :disabled="page === 1"
  465. @click="changePage(page - 1)"
  466. content="Previous Page"
  467. v-tippy
  468. >
  469. fast_rewind
  470. </button>
  471. <p>Page {{ page }} / {{ lastPage }}</p>
  472. <button
  473. :class="{ disabled: page === lastPage }"
  474. class="button is-primary material-icons"
  475. :disabled="page === lastPage"
  476. @click="changePage(page + 1)"
  477. content="Next Page"
  478. v-tippy
  479. >
  480. fast_forward
  481. </button>
  482. <button
  483. :class="{ disabled: page === lastPage }"
  484. class="button is-primary material-icons"
  485. :disabled="page === lastPage"
  486. @click="changePage(lastPage)"
  487. content="Last Page"
  488. v-tippy
  489. >
  490. skip_next
  491. </button>
  492. </div>
  493. <div class="page-size">
  494. <div class="control">
  495. <label class="label">Items per page</label>
  496. <p class="control select">
  497. <select
  498. v-model.number="pageSize"
  499. @change="changePageSize()"
  500. >
  501. <option value="10">10</option>
  502. <option value="25">25</option>
  503. <option value="50">50</option>
  504. <option value="100">100</option>
  505. <option value="250">250</option>
  506. <option value="500">500</option>
  507. <option value="1000">1000</option>
  508. </select>
  509. </p>
  510. </div>
  511. </div>
  512. </div>
  513. </div>
  514. <div
  515. v-if="selectedRows.length > 0"
  516. class="bulk-popup"
  517. :style="{
  518. top: bulkPopup.top + 'px',
  519. left: bulkPopup.left + 'px'
  520. }"
  521. >
  522. <button
  523. class="button is-primary"
  524. :content="
  525. selectedRows.length === 1
  526. ? `${selectedRows.length} row selected`
  527. : `${selectedRows.length} rows selected`
  528. "
  529. v-tippy
  530. >
  531. {{ selectedRows.length }}
  532. </button>
  533. <slot name="bulk-actions" :item="selectedRows" />
  534. <div class="right">
  535. <slot name="bulk-actions-right" :item="selectedRows" />
  536. <span
  537. class="material-icons drag-icon"
  538. @mousedown.left="onDragBox"
  539. @dblclick="resetBulkActionsPosition()"
  540. >
  541. drag_indicator
  542. </span>
  543. </div>
  544. </div>
  545. </div>
  546. </template>
  547. <script>
  548. import { mapGetters } from "vuex";
  549. import draggable from "vuedraggable";
  550. import Toast from "toasters";
  551. import ws from "@/ws";
  552. export default {
  553. components: {
  554. draggable
  555. },
  556. props: {
  557. /*
  558. Column properties:
  559. name: Unique lowercase name
  560. displayName: Nice name for the column header
  561. properties: The properties this column needs to show data
  562. sortable: Boolean for whether the order of a particular column can be changed
  563. sortProperty: The property the backend will sort on if this column gets sorted, e.g. title
  564. hidable: Boolean for whether a column can be hidden
  565. defaultVisibility: Default visibility for a column, either "shown" or "hidden"
  566. draggable: Boolean for whether a column can be dragged/reordered,
  567. resizable: Boolean for whether a column can be resized
  568. minWidth: Minimum width of column, e.g. 50px
  569. width: Width of column, e.g. 100px
  570. maxWidth: Maximum width of column, e.g. 150px
  571. */
  572. columnDefault: { type: Object, default: () => {} },
  573. columns: { type: Array, default: null },
  574. filters: { type: Array, default: null },
  575. dataAction: { type: String, default: null }
  576. },
  577. data() {
  578. return {
  579. page: 1,
  580. pageSize: 10,
  581. data: [],
  582. count: 0, // TODO Rename
  583. sort: {},
  584. orderedColumns: [],
  585. shownColumns: [],
  586. pinnedColumns: ["select"],
  587. columnDragOptions() {
  588. return {
  589. animation: 200,
  590. group: "columns",
  591. disabled: false,
  592. ghostClass: "draggable-list-ghost",
  593. filter: ".ignore-elements",
  594. fallbackTolerance: 50
  595. };
  596. },
  597. editingFilters: [],
  598. appliedFilters: [],
  599. filterOperator: "or",
  600. appliedFilterOperator: "or",
  601. filterOperators: [
  602. {
  603. name: "or",
  604. displayName: "OR"
  605. },
  606. {
  607. name: "and",
  608. displayName: "AND"
  609. },
  610. {
  611. name: "nor",
  612. displayName: "NOR"
  613. }
  614. ],
  615. resizing: {},
  616. allFilterTypes: {
  617. contains: {
  618. name: "contains",
  619. displayName: "Contains"
  620. },
  621. exact: {
  622. name: "exact",
  623. displayName: "Exact"
  624. },
  625. regex: {
  626. name: "regex",
  627. displayName: "Regex"
  628. }
  629. },
  630. bulkPopup: {
  631. top: 0,
  632. left: 0,
  633. pos1: 0,
  634. pos2: 0,
  635. pos3: 0,
  636. pos4: 0
  637. }
  638. };
  639. },
  640. computed: {
  641. properties() {
  642. return Array.from(
  643. new Set(
  644. this.sortedFilteredColumns.flatMap(
  645. column => column.properties
  646. )
  647. )
  648. );
  649. },
  650. lastPage() {
  651. return Math.ceil(this.count / this.pageSize);
  652. },
  653. sortedFilteredColumns() {
  654. return this.orderedColumns.filter(
  655. column => this.shownColumns.indexOf(column.name) !== -1
  656. );
  657. },
  658. hidableSortedColumns() {
  659. return this.orderedColumns.filter(column => column.hidable);
  660. },
  661. lastSelectedItemIndex() {
  662. return this.data.findIndex(item => item.highlighted);
  663. },
  664. selectedRows() {
  665. return this.data.filter(data => data.selected);
  666. },
  667. ...mapGetters({
  668. socket: "websockets/getSocket"
  669. })
  670. },
  671. mounted() {
  672. const columns = [
  673. {
  674. name: "select",
  675. displayName: "",
  676. properties: [],
  677. sortable: false,
  678. hidable: false,
  679. draggable: false,
  680. resizable: false,
  681. pinable: false,
  682. minWidth: 47,
  683. width: 47,
  684. maxWidth: 47
  685. },
  686. ...this.columns
  687. ];
  688. this.orderedColumns = columns.map(column => ({
  689. ...this.columnDefault,
  690. ...column
  691. }));
  692. // A column will be shown if the defaultVisibility is set to shown, OR if the defaultVisibility is not set to shown and hidable is false
  693. this.shownColumns = columns
  694. .filter(column => column.defaultVisibility !== "hidden")
  695. .map(column => column.name);
  696. const pageSize = parseInt(localStorage.getItem("adminPageSize"));
  697. if (!Number.isNaN(pageSize)) this.pageSize = pageSize;
  698. this.resetBulkActionsPosition();
  699. ws.onConnect(this.init);
  700. },
  701. methods: {
  702. init() {
  703. this.getData();
  704. },
  705. getData() {
  706. this.socket.dispatch(
  707. this.dataAction,
  708. this.page,
  709. this.pageSize,
  710. this.properties,
  711. this.sort,
  712. this.appliedFilters,
  713. this.appliedFilterOperator,
  714. res => {
  715. console.log(111, res);
  716. if (res.status === "success") {
  717. const { data, count } = res.data;
  718. this.data = data;
  719. this.count = count;
  720. } else {
  721. new Toast(res.message);
  722. }
  723. }
  724. );
  725. },
  726. changePageSize() {
  727. this.getData();
  728. localStorage.setItem("adminPageSize", this.pageSize);
  729. },
  730. changePage(page) {
  731. if (page < 1) return;
  732. if (page > this.lastPage) return;
  733. if (page === this.page) return;
  734. this.page = page;
  735. this.getData();
  736. },
  737. changeSort(column) {
  738. if (column.sortable) {
  739. const { sortProperty } = column;
  740. if (this.sort[sortProperty] === undefined)
  741. this.sort[sortProperty] = "ascending";
  742. else if (this.sort[sortProperty] === "ascending")
  743. this.sort[sortProperty] = "descending";
  744. else if (this.sort[sortProperty] === "descending")
  745. delete this.sort[sortProperty];
  746. this.getData();
  747. }
  748. },
  749. toggleColumnVisibility(column) {
  750. if (this.shownColumns.indexOf(column.name) !== -1) {
  751. if (this.shownColumns.length <= 2)
  752. return new Toast(
  753. `Unable to hide column ${column.displayName}, there must be at least 1 visibile column`
  754. );
  755. this.shownColumns.splice(
  756. this.shownColumns.indexOf(column.name),
  757. 1
  758. );
  759. } else {
  760. this.shownColumns.push(column.name);
  761. }
  762. return this.getData();
  763. },
  764. togglePinnedColumn(column) {
  765. if (this.pinnedColumns.indexOf(column.name) !== -1) {
  766. this.pinnedColumns.splice(
  767. this.pinnedColumns.indexOf(column.name),
  768. 1
  769. );
  770. } else {
  771. this.pinnedColumns.push(column.name);
  772. }
  773. },
  774. toggleSelectedRow(itemIndex, event) {
  775. const { shiftKey, ctrlKey } = event;
  776. // Shift was pressed, so attempt to select all items between the clicked item and last clicked item
  777. if (shiftKey) {
  778. // If there is a last clicked item
  779. if (this.lastSelectedItemIndex >= 0) {
  780. // Clicked item is lower than last item, so select upwards until it reaches the last selected item
  781. if (itemIndex > this.lastSelectedItemIndex) {
  782. for (
  783. let itemIndexUp = itemIndex;
  784. itemIndexUp > this.lastSelectedItemIndex;
  785. itemIndexUp -= 1
  786. ) {
  787. this.data[itemIndexUp].selected = true;
  788. }
  789. }
  790. // Clicked item is higher than last item, so select downwards until it reaches the last selected item
  791. else if (itemIndex < this.lastSelectedItemIndex) {
  792. for (
  793. let itemIndexDown = itemIndex;
  794. itemIndexDown < this.lastSelectedItemIndex;
  795. itemIndexDown += 1
  796. ) {
  797. this.data[itemIndexDown].selected = true;
  798. }
  799. }
  800. }
  801. }
  802. // Ctrl was pressed, so attempt to unselect all items between the clicked item and last clicked item
  803. else if (ctrlKey) {
  804. // If there is a last clicked item
  805. if (this.lastSelectedItemIndex >= 0) {
  806. // Clicked item is lower than last item, so unselect upwards until it reaches the last selected item
  807. if (itemIndex > this.lastSelectedItemIndex) {
  808. for (
  809. let itemIndexUp = itemIndex;
  810. itemIndexUp > this.lastSelectedItemIndex;
  811. itemIndexUp -= 1
  812. ) {
  813. this.data[itemIndexUp].selected = false;
  814. }
  815. }
  816. // Clicked item is higher than last item, so unselect downwards until it reaches the last selected item
  817. else if (itemIndex < this.lastSelectedItemIndex) {
  818. for (
  819. let itemIndexDown = itemIndex;
  820. itemIndexDown < this.lastSelectedItemIndex;
  821. itemIndexDown += 1
  822. ) {
  823. this.data[itemIndexDown].selected = false;
  824. }
  825. }
  826. }
  827. }
  828. // Neither ctrl nor shift were pressed, so toggle clicked item
  829. else {
  830. this.data[itemIndex].selected = !this.data[itemIndex].selected;
  831. }
  832. // Set the last clicked item to no longer be highlighted, if it exists
  833. if (this.lastSelectedItemIndex >= 0)
  834. this.data[this.lastSelectedItemIndex].highlighted = false;
  835. // Set the clicked item to be highlighted
  836. this.data[itemIndex].highlighted = true;
  837. },
  838. toggleAllRows() {
  839. if (this.data.length > this.selectedRows.length) {
  840. this.data = this.data.map(row => ({ ...row, selected: true }));
  841. } else {
  842. this.data = this.data.map(row => ({ ...row, selected: false }));
  843. }
  844. },
  845. addFilterItem() {
  846. this.editingFilters.push({
  847. data: "",
  848. filter: this.addFilterValue,
  849. filterType: this.addFilterValue.defaultFilterType
  850. });
  851. },
  852. removeFilterItem(index) {
  853. this.editingFilters.splice(index, 1);
  854. },
  855. columnResizingMouseDown(column, event) {
  856. this.resizing.resizing = true;
  857. this.resizing.resizingColumn = column;
  858. this.resizing.width = event.target.parentElement.offsetWidth;
  859. this.resizing.lastX = event.x;
  860. },
  861. columnResizingMouseMove(event) {
  862. if (this.resizing.resizing) {
  863. if (event.buttons !== 1) {
  864. this.resizing.resizing = false;
  865. }
  866. this.resizing.width -= this.resizing.lastX - event.x;
  867. this.resizing.lastX = event.x;
  868. if (
  869. this.resizing.resizingColumn.minWidth &&
  870. this.resizing.resizingColumn.maxWidth
  871. ) {
  872. this.resizing.resizingColumn.width = Math.max(
  873. Math.min(
  874. this.resizing.resizingColumn.maxWidth,
  875. this.resizing.width
  876. ),
  877. this.resizing.resizingColumn.minWidth
  878. );
  879. } else if (this.resizing.resizingColumn.minWidth) {
  880. this.resizing.resizingColumn.width = Math.max(
  881. this.resizing.width,
  882. this.resizing.resizingColumn.minWidth
  883. );
  884. } else if (this.resizing.resizingColumn.maxWidth) {
  885. this.resizing.resizingColumn.width = Math.min(
  886. this.resizing.resizingColumn.maxWidth,
  887. this.resizing.width
  888. );
  889. } else {
  890. this.resizing.resizingColumn.width = this.resizing.width;
  891. }
  892. console.log(`New width: ${this.resizing.width}px`);
  893. }
  894. },
  895. columnResizingMouseUp() {
  896. this.resizing.resizing = false;
  897. },
  898. columnResetWidth(column) {
  899. // eslint-disable-next-line no-param-reassign
  900. column.minWidth = column.maxWidth = "";
  901. },
  902. filterTypes(filter) {
  903. if (!filter || !filter.filterTypes) return [];
  904. return filter.filterTypes.map(
  905. filterType => this.allFilterTypes[filterType]
  906. );
  907. },
  908. changeFilterType(index) {
  909. this.editingFilters[index].filterType =
  910. this.editingFilters[index].filter.defaultFilterType;
  911. },
  912. onDragBox(e) {
  913. const e1 = e || window.event;
  914. e1.preventDefault();
  915. this.bulkPopup.pos3 = e1.clientX;
  916. this.bulkPopup.pos4 = e1.clientY;
  917. document.onmousemove = e => {
  918. const e2 = e || window.event;
  919. e2.preventDefault();
  920. // calculate the new cursor position:
  921. this.bulkPopup.pos1 = this.bulkPopup.pos3 - e.clientX;
  922. this.bulkPopup.pos2 = this.bulkPopup.pos4 - e.clientY;
  923. this.bulkPopup.pos3 = e.clientX;
  924. this.bulkPopup.pos4 = e.clientY;
  925. // set the element's new position:
  926. this.bulkPopup.top -= this.bulkPopup.pos2;
  927. this.bulkPopup.left -= this.bulkPopup.pos1;
  928. if (this.bulkPopup.top < 0) this.bulkPopup.top = 0;
  929. if (this.bulkPopup.top > document.body.clientHeight - 50)
  930. this.bulkPopup.top = document.body.clientHeight - 50;
  931. if (this.bulkPopup.left < 0) this.bulkPopup.left = 0;
  932. if (this.bulkPopup.left > document.body.clientWidth - 400)
  933. this.bulkPopup.left = document.body.clientWidth - 400;
  934. };
  935. document.onmouseup = () => {
  936. document.onmouseup = null;
  937. document.onmousemove = null;
  938. };
  939. },
  940. resetBulkActionsPosition() {
  941. this.bulkPopup.top = document.body.clientHeight - 56;
  942. this.bulkPopup.left = document.body.clientWidth / 2 - 200;
  943. },
  944. applyFilterAndGetData() {
  945. this.appliedFilters = JSON.parse(
  946. JSON.stringify(this.editingFilters)
  947. );
  948. this.appliedFilterOperator = this.filterOperator;
  949. this.getData();
  950. }
  951. }
  952. };
  953. </script>
  954. <style lang="scss" scoped>
  955. .night-mode {
  956. .table-outer-container {
  957. .table-container .table {
  958. &,
  959. thead th {
  960. background-color: var(--dark-grey-3);
  961. color: var(--light-grey-2);
  962. }
  963. tr {
  964. &:nth-child(even) {
  965. background-color: var(--dark-grey-2);
  966. }
  967. &:hover,
  968. &:focus,
  969. &.highlighted {
  970. background-color: var(--dark-grey-4);
  971. }
  972. }
  973. th,
  974. td {
  975. border-color: var(--dark-grey) !important;
  976. }
  977. }
  978. .table-header,
  979. .table-footer {
  980. background-color: var(--dark-grey-3);
  981. color: var(--light-grey-2);
  982. }
  983. }
  984. .bulk-popup {
  985. border: 0;
  986. background-color: var(--dark-grey-2);
  987. color: var(--white);
  988. .material-icons {
  989. color: var(--white);
  990. }
  991. }
  992. }
  993. .table-outer-container {
  994. border-radius: 5px;
  995. box-shadow: 0 2px 3px rgba(10, 10, 10, 0.1), 0 0 0 1px rgba(10, 10, 10, 0.1);
  996. margin: 10px 0;
  997. overflow: hidden;
  998. .table-container {
  999. overflow-x: auto;
  1000. table {
  1001. border-collapse: separate;
  1002. table-layout: fixed;
  1003. thead {
  1004. tr {
  1005. th {
  1006. height: 40px;
  1007. line-height: 40px;
  1008. border: 1px solid var(--light-grey-2);
  1009. border-width: 1px 1px 1px 0;
  1010. &:last-child {
  1011. border-width: 1px 0 1px;
  1012. }
  1013. &.sortable {
  1014. cursor: pointer;
  1015. }
  1016. & > div {
  1017. display: flex;
  1018. white-space: nowrap;
  1019. & > span {
  1020. margin-left: 5px;
  1021. &:first-child {
  1022. margin-left: 0;
  1023. margin-right: auto;
  1024. }
  1025. & > .material-icons {
  1026. font-size: 22px;
  1027. position: relative;
  1028. top: 6px;
  1029. cursor: pointer;
  1030. &.active {
  1031. color: var(--primary-color);
  1032. }
  1033. &:hover,
  1034. &:focus {
  1035. filter: brightness(90%);
  1036. }
  1037. }
  1038. }
  1039. }
  1040. }
  1041. }
  1042. }
  1043. tbody {
  1044. tr {
  1045. &.highlighted {
  1046. background-color: var(--light-grey);
  1047. }
  1048. td {
  1049. border: 1px solid var(--light-grey-2);
  1050. border-width: 0 1px 1px 0;
  1051. &:last-child {
  1052. border-width: 0 0 1px;
  1053. }
  1054. }
  1055. }
  1056. }
  1057. }
  1058. table thead tr th,
  1059. table tbody tr td {
  1060. position: relative;
  1061. white-space: nowrap;
  1062. text-overflow: ellipsis;
  1063. overflow: hidden;
  1064. .resizer {
  1065. height: 100%;
  1066. width: 5px;
  1067. background-color: transparent;
  1068. cursor: col-resize;
  1069. position: absolute;
  1070. right: 0;
  1071. top: 0;
  1072. }
  1073. }
  1074. }
  1075. .table-header,
  1076. .table-footer {
  1077. display: flex;
  1078. flex-direction: row;
  1079. flex-wrap: wrap;
  1080. justify-content: space-between;
  1081. line-height: 36px;
  1082. background-color: var(--white);
  1083. }
  1084. .table-header > div {
  1085. display: flex;
  1086. flex-direction: row;
  1087. > span > .button {
  1088. margin: 5px;
  1089. }
  1090. .filters-indicator {
  1091. line-height: 46px;
  1092. display: flex;
  1093. align-items: center;
  1094. column-gap: 4px;
  1095. }
  1096. }
  1097. .table-footer {
  1098. .page-controls,
  1099. .page-size > .control {
  1100. display: flex;
  1101. flex-direction: row;
  1102. margin-bottom: 0 !important;
  1103. button {
  1104. margin: 5px;
  1105. font-size: 20px;
  1106. }
  1107. p,
  1108. label {
  1109. margin: 5px;
  1110. font-size: 14px;
  1111. font-weight: 600;
  1112. }
  1113. &.select::after {
  1114. top: 18px;
  1115. }
  1116. }
  1117. }
  1118. }
  1119. .control.is-grouped {
  1120. display: flex;
  1121. & > .control {
  1122. &.label {
  1123. height: 36px;
  1124. background-color: var(--white);
  1125. border: 1px solid var(--light-grey-2);
  1126. color: var(--dark-grey-2);
  1127. appearance: none;
  1128. border-radius: 3px;
  1129. font-size: 14px;
  1130. line-height: 34px;
  1131. padding-left: 8px;
  1132. padding-right: 8px;
  1133. }
  1134. &.select.is-expanded > select {
  1135. width: 100%;
  1136. }
  1137. & > input,
  1138. & > select,
  1139. & > .button,
  1140. &.label {
  1141. border-radius: 0;
  1142. }
  1143. &:first-child {
  1144. & > input,
  1145. & > select,
  1146. & > .button,
  1147. &.label {
  1148. border-radius: 5px 0 0 5px;
  1149. }
  1150. }
  1151. &:last-child {
  1152. & > input,
  1153. & > select,
  1154. & > .button,
  1155. &.label {
  1156. border-radius: 0 5px 5px 0;
  1157. }
  1158. }
  1159. & > .button {
  1160. font-size: 22px;
  1161. }
  1162. }
  1163. }
  1164. .advanced-filter-bottom {
  1165. display: flex;
  1166. .button {
  1167. font-size: 16px !important;
  1168. width: 100%;
  1169. }
  1170. .control {
  1171. margin: 0 !important;
  1172. }
  1173. }
  1174. .bulk-popup {
  1175. display: flex;
  1176. position: fixed;
  1177. flex-direction: row;
  1178. width: 100%;
  1179. max-width: 400px;
  1180. line-height: 36px;
  1181. z-index: 5;
  1182. border: 1px solid var(--light-grey-3);
  1183. border-radius: 5px;
  1184. box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
  1185. background-color: var(--white);
  1186. color: var(--dark-grey);
  1187. padding: 5px;
  1188. .right {
  1189. display: flex;
  1190. flex-direction: row;
  1191. margin-left: auto;
  1192. }
  1193. .drag-icon {
  1194. position: relative;
  1195. top: 6px;
  1196. color: var(--dark-grey);
  1197. cursor: move;
  1198. }
  1199. }
  1200. </style>