AdvancedTable.vue 36 KB

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