AdvancedTable.vue 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  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;
  725. this.count = count;
  726. } else {
  727. new Toast(res.message);
  728. }
  729. }
  730. );
  731. },
  732. changePageSize() {
  733. this.getData();
  734. localStorage.setItem("adminPageSize", this.pageSize);
  735. },
  736. changePage(page) {
  737. if (page < 1) return;
  738. if (page > this.lastPage) return;
  739. if (page === this.page) return;
  740. this.page = page;
  741. this.getData();
  742. },
  743. changeSort(column) {
  744. if (column.sortable) {
  745. const { sortProperty } = column;
  746. if (this.sort[sortProperty] === undefined)
  747. this.sort[sortProperty] = "ascending";
  748. else if (this.sort[sortProperty] === "ascending")
  749. this.sort[sortProperty] = "descending";
  750. else if (this.sort[sortProperty] === "descending")
  751. delete this.sort[sortProperty];
  752. this.getData();
  753. }
  754. },
  755. toggleColumnVisibility(column) {
  756. if (this.shownColumns.indexOf(column.name) !== -1) {
  757. if (this.shownColumns.length <= 3)
  758. return new Toast(
  759. `Unable to hide column ${column.displayName}, there must be at least 1 visibile column`
  760. );
  761. this.shownColumns.splice(
  762. this.shownColumns.indexOf(column.name),
  763. 1
  764. );
  765. } else {
  766. this.shownColumns.push(column.name);
  767. }
  768. this.recalculateWidths();
  769. return this.getData();
  770. },
  771. toggleSelectedRow(itemIndex, event) {
  772. const { shiftKey, ctrlKey } = event;
  773. // Shift was pressed, so attempt to select all items between the clicked item and last clicked item
  774. if (shiftKey) {
  775. // If there is a last clicked item
  776. if (this.lastSelectedItemIndex >= 0) {
  777. // Clicked item is lower than last item, so select upwards until it reaches the last selected item
  778. if (itemIndex > this.lastSelectedItemIndex) {
  779. for (
  780. let itemIndexUp = itemIndex;
  781. itemIndexUp > this.lastSelectedItemIndex;
  782. itemIndexUp -= 1
  783. ) {
  784. this.data[itemIndexUp].selected = true;
  785. }
  786. }
  787. // Clicked item is higher than last item, so select downwards until it reaches the last selected item
  788. else if (itemIndex < this.lastSelectedItemIndex) {
  789. for (
  790. let itemIndexDown = itemIndex;
  791. itemIndexDown < this.lastSelectedItemIndex;
  792. itemIndexDown += 1
  793. ) {
  794. this.data[itemIndexDown].selected = true;
  795. }
  796. }
  797. }
  798. }
  799. // Ctrl was pressed, so attempt to unselect all items between the clicked item and last clicked item
  800. else if (ctrlKey) {
  801. // If there is a last clicked item
  802. if (this.lastSelectedItemIndex >= 0) {
  803. // Clicked item is lower than last item, so unselect upwards until it reaches the last selected item
  804. if (itemIndex > this.lastSelectedItemIndex) {
  805. for (
  806. let itemIndexUp = itemIndex;
  807. itemIndexUp > this.lastSelectedItemIndex;
  808. itemIndexUp -= 1
  809. ) {
  810. this.data[itemIndexUp].selected = false;
  811. }
  812. }
  813. // Clicked item is higher than last item, so unselect downwards until it reaches the last selected item
  814. else if (itemIndex < this.lastSelectedItemIndex) {
  815. for (
  816. let itemIndexDown = itemIndex;
  817. itemIndexDown < this.lastSelectedItemIndex;
  818. itemIndexDown += 1
  819. ) {
  820. this.data[itemIndexDown].selected = false;
  821. }
  822. }
  823. }
  824. }
  825. // Neither ctrl nor shift were pressed, so toggle clicked item
  826. else {
  827. this.data[itemIndex].selected = !this.data[itemIndex].selected;
  828. }
  829. // Set the last clicked item to no longer be highlighted, if it exists
  830. if (this.lastSelectedItemIndex >= 0)
  831. this.data[this.lastSelectedItemIndex].highlighted = false;
  832. // Set the clicked item to be highlighted
  833. this.data[itemIndex].highlighted = true;
  834. },
  835. toggleAllRows() {
  836. if (this.data.length > this.selectedRows.length) {
  837. this.data = this.data.map(row => ({ ...row, selected: true }));
  838. } else {
  839. this.data = this.data.map(row => ({ ...row, selected: false }));
  840. }
  841. },
  842. addFilterItem() {
  843. this.editingFilters.push({
  844. data: "",
  845. filter: this.addFilterValue,
  846. filterType: this.addFilterValue.defaultFilterType
  847. });
  848. },
  849. removeFilterItem(index) {
  850. this.editingFilters.splice(index, 1);
  851. },
  852. columnResizingMouseDown(column, event) {
  853. this.resizing.resizing = true;
  854. this.resizing.resizingColumn = column;
  855. this.resizing.width = event.target.parentElement.offsetWidth;
  856. this.resizing.lastX = event.x;
  857. },
  858. columnResizingMouseMove(event) {
  859. if (this.resizing.resizing) {
  860. if (event.buttons !== 1) {
  861. this.resizing.resizing = false;
  862. }
  863. this.resizing.width -= this.resizing.lastX - event.x;
  864. this.resizing.lastX = event.x;
  865. if (
  866. this.resizing.resizingColumn.minWidth &&
  867. this.resizing.resizingColumn.maxWidth
  868. ) {
  869. this.resizing.resizingColumn.width = Math.max(
  870. Math.min(
  871. this.resizing.resizingColumn.maxWidth,
  872. this.resizing.width
  873. ),
  874. this.resizing.resizingColumn.minWidth
  875. );
  876. } else if (this.resizing.resizingColumn.minWidth) {
  877. this.resizing.resizingColumn.width = Math.max(
  878. this.resizing.width,
  879. this.resizing.resizingColumn.minWidth
  880. );
  881. } else if (this.resizing.resizingColumn.maxWidth) {
  882. this.resizing.resizingColumn.width = Math.min(
  883. this.resizing.resizingColumn.maxWidth,
  884. this.resizing.width
  885. );
  886. } else {
  887. this.resizing.resizingColumn.width = this.resizing.width;
  888. }
  889. this.resizing.width = this.resizing.resizingColumn.width;
  890. console.log(`New width: ${this.resizing.width}px`);
  891. }
  892. },
  893. columnResizingMouseUp() {
  894. this.resizing.resizing = false;
  895. },
  896. columnResetWidth(column) {
  897. const index = this.orderedColumns.indexOf(column);
  898. if (column.defaultWidth && !Number.isNaN(column.defaultWidth))
  899. this.orderedColumns[index].width = column.defaultWidth;
  900. else if (
  901. column.calculatedWidth &&
  902. !Number.isNaN(column.calculatedWidth)
  903. )
  904. this.orderedColumns[index].width = column.calculatedWidth;
  905. },
  906. filterTypes(filter) {
  907. if (!filter || !filter.filterTypes) return [];
  908. return filter.filterTypes.map(
  909. filterType => this.allFilterTypes[filterType]
  910. );
  911. },
  912. changeFilterType(index) {
  913. this.editingFilters[index].filterType =
  914. this.editingFilters[index].filter.defaultFilterType;
  915. },
  916. onDragBox(e) {
  917. const e1 = e || window.event;
  918. e1.preventDefault();
  919. this.bulkPopup.pos3 = e1.clientX;
  920. this.bulkPopup.pos4 = e1.clientY;
  921. document.onmousemove = e => {
  922. const e2 = e || window.event;
  923. e2.preventDefault();
  924. // calculate the new cursor position:
  925. this.bulkPopup.pos1 = this.bulkPopup.pos3 - e.clientX;
  926. this.bulkPopup.pos2 = this.bulkPopup.pos4 - e.clientY;
  927. this.bulkPopup.pos3 = e.clientX;
  928. this.bulkPopup.pos4 = e.clientY;
  929. // set the element's new position:
  930. this.bulkPopup.top -= this.bulkPopup.pos2;
  931. this.bulkPopup.left -= this.bulkPopup.pos1;
  932. if (this.bulkPopup.top < 0) this.bulkPopup.top = 0;
  933. if (this.bulkPopup.top > document.body.clientHeight - 50)
  934. this.bulkPopup.top = document.body.clientHeight - 50;
  935. if (this.bulkPopup.left < 0) this.bulkPopup.left = 0;
  936. if (this.bulkPopup.left > document.body.clientWidth - 400)
  937. this.bulkPopup.left = document.body.clientWidth - 400;
  938. };
  939. document.onmouseup = () => {
  940. document.onmouseup = null;
  941. document.onmousemove = null;
  942. };
  943. },
  944. resetBulkActionsPosition() {
  945. this.bulkPopup.top = document.body.clientHeight - 56;
  946. this.bulkPopup.left = document.body.clientWidth / 2 - 200;
  947. },
  948. applyFilterAndGetData() {
  949. this.appliedFilters = JSON.parse(
  950. JSON.stringify(this.editingFilters)
  951. );
  952. this.appliedFilterOperator = this.filterOperator;
  953. this.getData();
  954. },
  955. recalculateWidths() {
  956. let noWidthCount = 0;
  957. let calculatedWidth = 0;
  958. this.orderedColumns.forEach(column => {
  959. if (this.shownColumns.indexOf(column.name) !== -1)
  960. if (Number.isFinite(column.width)) {
  961. calculatedWidth += column.width;
  962. } else if (Number.isFinite(column.defaultWidth)) {
  963. calculatedWidth += column.defaultWidth;
  964. } else {
  965. noWidthCount += 1;
  966. }
  967. });
  968. calculatedWidth = Math.floor(
  969. // max-width of table is 1880px
  970. (Math.min(1880, document.body.clientWidth) - calculatedWidth) /
  971. (noWidthCount - 1)
  972. );
  973. this.orderedColumns = this.orderedColumns.map(column => {
  974. const orderedColumn = column;
  975. if (
  976. this.shownColumns.indexOf(orderedColumn.name) !== -1 &&
  977. !Number.isFinite(orderedColumn.width)
  978. ) {
  979. if (Number.isFinite(orderedColumn.defaultWidth)) {
  980. orderedColumn.width = orderedColumn.defaultWidth;
  981. } else {
  982. // eslint-disable-next-line no-param-reassign
  983. orderedColumn.width = orderedColumn.calculatedWidth =
  984. Math.min(
  985. Math.max(
  986. orderedColumn.minWidth || 100, // fallback 100px min width
  987. calculatedWidth
  988. ),
  989. orderedColumn.maxWidth || 1000 // fallback 1000px max width
  990. );
  991. }
  992. }
  993. return orderedColumn;
  994. });
  995. }
  996. }
  997. };
  998. </script>
  999. <style lang="scss" scoped>
  1000. .night-mode {
  1001. .table-outer-container {
  1002. .table-container .table {
  1003. &,
  1004. thead th {
  1005. background-color: var(--dark-grey-3);
  1006. color: var(--light-grey-2);
  1007. }
  1008. tr {
  1009. th,
  1010. td {
  1011. border-color: var(--dark-grey) !important;
  1012. &:first-child {
  1013. background-color: var(--dark-grey-3) !important;
  1014. }
  1015. }
  1016. &:nth-child(even) {
  1017. &,
  1018. td:first-child {
  1019. background-color: var(--dark-grey-2) !important;
  1020. }
  1021. }
  1022. &:hover,
  1023. &:focus,
  1024. &.highlighted {
  1025. th,
  1026. td {
  1027. &,
  1028. &:first-child {
  1029. background-color: var(--dark-grey-4) !important;
  1030. }
  1031. }
  1032. }
  1033. }
  1034. }
  1035. .table-header,
  1036. .table-footer {
  1037. background-color: var(--dark-grey-3);
  1038. color: var(--light-grey-2);
  1039. }
  1040. }
  1041. .bulk-popup {
  1042. border: 0;
  1043. background-color: var(--dark-grey-2);
  1044. color: var(--white);
  1045. .material-icons {
  1046. color: var(--white);
  1047. }
  1048. }
  1049. }
  1050. .table-outer-container {
  1051. border-radius: 5px;
  1052. box-shadow: 0 2px 3px rgba(10, 10, 10, 0.1), 0 0 0 1px rgba(10, 10, 10, 0.1);
  1053. margin: 10px 0;
  1054. overflow: hidden;
  1055. .table-container {
  1056. overflow-x: auto;
  1057. table {
  1058. border-collapse: separate;
  1059. table-layout: fixed;
  1060. thead {
  1061. tr {
  1062. th {
  1063. height: 40px;
  1064. line-height: 40px;
  1065. border: 1px solid var(--light-grey-2);
  1066. border-width: 1px 1px 1px 0;
  1067. &:last-child {
  1068. border-width: 1px 0 1px;
  1069. }
  1070. &.sortable {
  1071. cursor: pointer;
  1072. }
  1073. & > div {
  1074. display: flex;
  1075. white-space: nowrap;
  1076. & > span {
  1077. margin-left: 5px;
  1078. &:first-child {
  1079. margin-left: 0;
  1080. margin-right: auto;
  1081. }
  1082. & > .material-icons {
  1083. font-size: 22px;
  1084. position: relative;
  1085. top: 6px;
  1086. cursor: pointer;
  1087. &.active {
  1088. color: var(--primary-color);
  1089. }
  1090. &:hover,
  1091. &:focus {
  1092. filter: brightness(90%);
  1093. }
  1094. }
  1095. }
  1096. }
  1097. }
  1098. }
  1099. }
  1100. tbody {
  1101. tr {
  1102. &.highlighted {
  1103. background-color: var(--light-grey);
  1104. }
  1105. td {
  1106. border: 1px solid var(--light-grey-2);
  1107. border-width: 0 1px 1px 0;
  1108. &:last-child {
  1109. border-width: 0 0 1px;
  1110. }
  1111. }
  1112. }
  1113. }
  1114. }
  1115. table thead tr,
  1116. table tbody tr {
  1117. th,
  1118. td {
  1119. position: relative;
  1120. white-space: nowrap;
  1121. text-overflow: ellipsis;
  1122. overflow: hidden;
  1123. &:first-child {
  1124. position: sticky;
  1125. left: 0;
  1126. background-color: var(--white);
  1127. z-index: 2;
  1128. }
  1129. .resizer {
  1130. height: 100%;
  1131. width: 5px;
  1132. background-color: transparent;
  1133. cursor: col-resize;
  1134. position: absolute;
  1135. right: 0;
  1136. top: 0;
  1137. }
  1138. }
  1139. &:nth-child(even) td:first-child {
  1140. background-color: #fafafa;
  1141. }
  1142. &:hover,
  1143. &:focus,
  1144. &.highlighted {
  1145. th,
  1146. td {
  1147. &,
  1148. &:first-child {
  1149. background-color: var(--light-grey);
  1150. }
  1151. }
  1152. }
  1153. }
  1154. }
  1155. .table-header,
  1156. .table-footer {
  1157. display: flex;
  1158. flex-direction: row;
  1159. flex-wrap: wrap;
  1160. justify-content: space-between;
  1161. line-height: 36px;
  1162. background-color: var(--white);
  1163. }
  1164. .table-header > div {
  1165. display: flex;
  1166. flex-direction: row;
  1167. > span > .button {
  1168. margin: 5px;
  1169. }
  1170. .filters-indicator {
  1171. line-height: 46px;
  1172. display: flex;
  1173. align-items: center;
  1174. column-gap: 4px;
  1175. }
  1176. }
  1177. .table-footer {
  1178. .page-controls,
  1179. .page-size > .control {
  1180. display: flex;
  1181. flex-direction: row;
  1182. margin-bottom: 0 !important;
  1183. button {
  1184. margin: 5px;
  1185. font-size: 20px;
  1186. }
  1187. p,
  1188. label {
  1189. margin: 5px;
  1190. font-size: 14px;
  1191. font-weight: 600;
  1192. }
  1193. &.select::after {
  1194. top: 18px;
  1195. }
  1196. }
  1197. }
  1198. }
  1199. .control.is-grouped {
  1200. display: flex;
  1201. & > .control {
  1202. &.label {
  1203. height: 36px;
  1204. background-color: var(--white);
  1205. border: 1px solid var(--light-grey-2);
  1206. color: var(--dark-grey-2);
  1207. appearance: none;
  1208. border-radius: 3px;
  1209. font-size: 14px;
  1210. line-height: 34px;
  1211. padding-left: 8px;
  1212. padding-right: 8px;
  1213. }
  1214. &.select.is-expanded > select {
  1215. width: 100%;
  1216. }
  1217. & > input,
  1218. & > select,
  1219. & > .button,
  1220. &.label {
  1221. border-radius: 0;
  1222. }
  1223. &:first-child {
  1224. & > input,
  1225. & > select,
  1226. & > .button,
  1227. &.label {
  1228. border-radius: 5px 0 0 5px;
  1229. }
  1230. }
  1231. &:last-child {
  1232. & > input,
  1233. & > select,
  1234. & > .button,
  1235. &.label {
  1236. border-radius: 0 5px 5px 0;
  1237. }
  1238. }
  1239. & > .button {
  1240. font-size: 22px;
  1241. }
  1242. }
  1243. @media screen and (max-width: 500px) {
  1244. &.advanced-filter {
  1245. flex-wrap: wrap;
  1246. .control.select {
  1247. width: 50%;
  1248. select {
  1249. width: 100%;
  1250. }
  1251. }
  1252. .control {
  1253. margin-bottom: 0 !important;
  1254. &:nth-child(1) select {
  1255. border-radius: 5px 0 0 0;
  1256. }
  1257. &:nth-child(2) select {
  1258. border-radius: 0 5px 0 0;
  1259. }
  1260. &:nth-child(3) input {
  1261. border-radius: 0 0 0 5px;
  1262. }
  1263. &:nth-child(4) button {
  1264. border-radius: 0 0 5px 0;
  1265. }
  1266. }
  1267. }
  1268. }
  1269. }
  1270. .advanced-filter-bottom {
  1271. display: flex;
  1272. .button {
  1273. font-size: 16px !important;
  1274. width: 100%;
  1275. }
  1276. .control {
  1277. margin: 0 !important;
  1278. }
  1279. }
  1280. .bulk-popup {
  1281. display: flex;
  1282. position: fixed;
  1283. flex-direction: row;
  1284. width: 100%;
  1285. max-width: 400px;
  1286. line-height: 36px;
  1287. z-index: 5;
  1288. border: 1px solid var(--light-grey-3);
  1289. border-radius: 5px;
  1290. box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
  1291. background-color: var(--white);
  1292. color: var(--dark-grey);
  1293. padding: 5px;
  1294. .right {
  1295. display: flex;
  1296. flex-direction: row;
  1297. margin-left: auto;
  1298. }
  1299. .drag-icon {
  1300. position: relative;
  1301. top: 6px;
  1302. color: var(--dark-grey);
  1303. cursor: move;
  1304. }
  1305. }
  1306. </style>