1
0

AdvancedTable.vue 32 KB

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