AdvancedTable.vue 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. <template>
  2. <div>
  3. <div
  4. class="table-outer-container"
  5. @mousemove="columnResizingMouseMove($event)"
  6. >
  7. <div class="table-header">
  8. <div>
  9. <tippy
  10. v-if="filters.length > 0"
  11. :touch="true"
  12. :interactive="true"
  13. placement="bottom"
  14. theme="search"
  15. ref="search"
  16. trigger="click"
  17. >
  18. <button class="button is-info">
  19. <i class="material-icons icon-with-button"
  20. >filter_list</i
  21. >
  22. Filters
  23. </button>
  24. <template #content>
  25. <div class="control is-grouped input-with-button">
  26. <p class="control select is-expanded">
  27. <select v-model="addFilterValue">
  28. <option
  29. v-for="type in filters"
  30. :key="type.name"
  31. :value="type"
  32. >
  33. {{ type.displayName }}
  34. </option>
  35. </select>
  36. </p>
  37. <p class="control">
  38. <button
  39. class="button material-icons is-success"
  40. @click="addFilterItem()"
  41. >
  42. control_point
  43. </button>
  44. </p>
  45. </div>
  46. <div
  47. v-for="(filter, index) in editingFilters"
  48. :key="`filter-${index}`"
  49. class="control is-grouped is-expanded"
  50. >
  51. <div class="control select">
  52. <select
  53. v-model="filter.filter"
  54. @change="changeFilterType(index)"
  55. >
  56. <option
  57. v-for="type in filters"
  58. :key="type.name"
  59. :value="type"
  60. >
  61. {{ type.displayName }}
  62. </option>
  63. </select>
  64. </div>
  65. <div class="control select">
  66. <select
  67. v-model="filter.filterType"
  68. :disabled="!filter.filterType"
  69. >
  70. <option
  71. v-for="filterType in filterTypes(
  72. filter.filter
  73. )"
  74. :key="filterType.name"
  75. :value="filterType.name"
  76. :selected="
  77. filter.filter
  78. .defaultFilterType ===
  79. filterType.name
  80. "
  81. >
  82. {{ filterType.displayName }}
  83. </option>
  84. </select>
  85. </div>
  86. <p class="control is-expanded">
  87. <input
  88. v-model="filter.data"
  89. class="input"
  90. type="text"
  91. placeholder="Search value"
  92. :disabled="!filter.filterType"
  93. />
  94. </p>
  95. <div class="control">
  96. <button
  97. class="button material-icons is-danger"
  98. @click="removeFilterItem(index)"
  99. >
  100. remove_circle_outline
  101. </button>
  102. </div>
  103. </div>
  104. <div
  105. v-if="editingFilters.length > 1"
  106. class="control is-expanded is-grouped"
  107. >
  108. <label class="control label"
  109. >Filter operator</label
  110. >
  111. <div class="control select is-expanded">
  112. <select v-model="filterOperator">
  113. <option
  114. v-for="operator in filterOperators"
  115. :key="operator.name"
  116. :value="operator.name"
  117. >
  118. {{ operator.displayName }}
  119. </option>
  120. </select>
  121. </div>
  122. </div>
  123. <div
  124. class="advanced-filter-bottom"
  125. v-if="editingFilters.length > 0"
  126. >
  127. <div class="control is-expanded">
  128. <button
  129. class="button is-info"
  130. @click="applyFilterAndGetData()"
  131. >
  132. <i
  133. class="
  134. material-icons
  135. icon-with-button
  136. "
  137. >filter_list</i
  138. >
  139. Apply filters
  140. </button>
  141. </div>
  142. </div>
  143. <div
  144. class="advanced-filter-bottom"
  145. v-else-if="editingFilters.length === 0"
  146. >
  147. <div class="control is-expanded">
  148. <button
  149. class="button is-info"
  150. @click="applyFilterAndGetData()"
  151. >
  152. <i
  153. class="
  154. material-icons
  155. icon-with-button
  156. "
  157. >filter_list</i
  158. >
  159. Apply filters
  160. </button>
  161. </div>
  162. </div>
  163. </template>
  164. </tippy>
  165. <tippy
  166. v-if="appliedFilters.length > 0"
  167. :touch="true"
  168. :interactive="true"
  169. theme="info"
  170. ref="activeFilters"
  171. >
  172. <div class="filters-indicator">
  173. {{ appliedFilters.length }}
  174. <i class="material-icons" @click.prevent="true"
  175. >filter_list</i
  176. >
  177. </div>
  178. <template #content>
  179. <p
  180. v-for="(filter, index) in appliedFilters"
  181. :key="`filter-${index}`"
  182. >
  183. {{ filter.filter.displayName }}
  184. {{ filter.filterType }} "{{ filter.data }}"
  185. {{
  186. appliedFilters.length === index + 1
  187. ? ""
  188. : filterOperator
  189. }}
  190. </p>
  191. </template>
  192. </tippy>
  193. <i
  194. v-else
  195. class="filters-indicator material-icons"
  196. content="No active filters"
  197. v-tippy="{ theme: 'info' }"
  198. >
  199. filter_list_off
  200. </i>
  201. </div>
  202. <div>
  203. <tippy
  204. v-if="hidableSortedColumns.length > 0"
  205. :touch="true"
  206. :interactive="true"
  207. placement="bottom"
  208. theme="dropdown"
  209. ref="editColumns"
  210. trigger="click"
  211. >
  212. <a class="button is-info" @click.prevent="true">
  213. <i class="material-icons icon-with-button">tune</i>
  214. Columns
  215. </a>
  216. <template #content>
  217. <draggable
  218. item-key="name"
  219. v-model="orderedColumns"
  220. v-bind="columnDragOptions"
  221. tag="div"
  222. draggable=".item-draggable"
  223. class="nav-dropdown-items"
  224. >
  225. <template #item="{ element: column }">
  226. <button
  227. v-if="column.name !== 'select'"
  228. :class="{
  229. sortable: column.sortable,
  230. 'item-draggable': column.draggable,
  231. 'nav-item': true
  232. }"
  233. @click.prevent="
  234. toggleColumnVisibility(column)
  235. "
  236. >
  237. <p
  238. class="
  239. control
  240. is-expanded
  241. checkbox-control
  242. "
  243. >
  244. <label class="switch">
  245. <input
  246. v-if="column.hidable"
  247. type="checkbox"
  248. :id="index"
  249. :checked="
  250. shownColumns.indexOf(
  251. column.name
  252. ) !== -1
  253. "
  254. @click="
  255. toggleColumnVisibility(
  256. column
  257. )
  258. "
  259. />
  260. <span
  261. :class="{
  262. slider: true,
  263. round: true,
  264. disabled:
  265. !column.hidable
  266. }"
  267. ></span>
  268. </label>
  269. <label :for="index">
  270. <span></span>
  271. <p>{{ column.displayName }}</p>
  272. </label>
  273. </p>
  274. </button>
  275. </template>
  276. </draggable>
  277. </template>
  278. </tippy>
  279. </div>
  280. </div>
  281. <div class="table-container">
  282. <table class="table">
  283. <thead>
  284. <draggable
  285. item-key="name"
  286. v-model="orderedColumns"
  287. v-bind="columnDragOptions"
  288. tag="tr"
  289. draggable=".item-draggable"
  290. >
  291. <template #item="{ element: column }">
  292. <th
  293. :class="{
  294. sortable: column.sortable,
  295. 'item-draggable': column.draggable
  296. }"
  297. :style="{
  298. minWidth: `${column.minWidth}px`,
  299. width: `${column.width}px`,
  300. maxWidth: `${column.maxWidth}px`
  301. }"
  302. v-if="
  303. shownColumns.indexOf(column.name) !== -1
  304. "
  305. >
  306. <p
  307. v-if="column.name === 'select'"
  308. class="checkbox"
  309. >
  310. <input
  311. type="checkbox"
  312. :checked="
  313. data.length ===
  314. selectedRows.length
  315. "
  316. @click="toggleAllRows()"
  317. />
  318. </p>
  319. <div v-else>
  320. <span>
  321. {{ column.displayName }}
  322. </span>
  323. <span
  324. v-if="column.pinable"
  325. content="Toggle Pinned Column"
  326. v-tippy
  327. @click="togglePinnedColumn(column)"
  328. >
  329. <span
  330. :class="{
  331. 'material-icons': true,
  332. active:
  333. pinnedColumns.indexOf(
  334. column.name
  335. ) !== -1
  336. }"
  337. >
  338. push_pin
  339. </span>
  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
  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. pinnedColumns: ["select"],
  583. columnDragOptions() {
  584. return {
  585. animation: 200,
  586. group: "columns",
  587. disabled: false,
  588. ghostClass: "draggable-list-ghost",
  589. filter: ".ignore-elements",
  590. fallbackTolerance: 50
  591. };
  592. },
  593. editingFilters: [],
  594. appliedFilters: [],
  595. filterOperator: "or",
  596. appliedFilterOperator: "or",
  597. filterOperators: [
  598. {
  599. name: "or",
  600. displayName: "OR"
  601. },
  602. {
  603. name: "and",
  604. displayName: "AND"
  605. },
  606. {
  607. name: "nor",
  608. displayName: "NOR"
  609. }
  610. ],
  611. resizing: {},
  612. allFilterTypes: {
  613. contains: {
  614. name: "contains",
  615. displayName: "Contains"
  616. },
  617. exact: {
  618. name: "exact",
  619. displayName: "Exact"
  620. },
  621. regex: {
  622. name: "regex",
  623. displayName: "Regex"
  624. }
  625. },
  626. bulkPopup: {
  627. top: 0,
  628. left: 0,
  629. pos1: 0,
  630. pos2: 0,
  631. pos3: 0,
  632. pos4: 0
  633. }
  634. };
  635. },
  636. computed: {
  637. properties() {
  638. return Array.from(
  639. new Set(
  640. this.sortedFilteredColumns.flatMap(
  641. column => column.properties
  642. )
  643. )
  644. );
  645. },
  646. lastPage() {
  647. return Math.ceil(this.count / this.pageSize);
  648. },
  649. sortedFilteredColumns() {
  650. return this.orderedColumns.filter(
  651. column => this.shownColumns.indexOf(column.name) !== -1
  652. );
  653. },
  654. hidableSortedColumns() {
  655. return this.orderedColumns.filter(column => column.hidable);
  656. },
  657. lastSelectedItemIndex() {
  658. return this.data.findIndex(item => item.highlighted);
  659. },
  660. selectedRows() {
  661. return this.data.filter(data => data.selected);
  662. },
  663. ...mapGetters({
  664. socket: "websockets/getSocket"
  665. })
  666. },
  667. mounted() {
  668. const columns = [
  669. {
  670. name: "select",
  671. displayName: "",
  672. properties: [],
  673. sortable: false,
  674. hidable: false,
  675. draggable: false,
  676. resizable: false,
  677. pinable: false,
  678. minWidth: 47,
  679. width: 47,
  680. maxWidth: 47
  681. },
  682. ...this.columns
  683. ];
  684. this.orderedColumns = columns.map(column => ({
  685. ...this.columnDefault,
  686. ...column
  687. }));
  688. // 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
  689. this.shownColumns = columns
  690. .filter(column => column.defaultVisibility !== "hidden")
  691. .map(column => column.name);
  692. const pageSize = parseInt(localStorage.getItem("adminPageSize"));
  693. if (!Number.isNaN(pageSize)) this.pageSize = pageSize;
  694. this.resetBulkActionsPosition();
  695. ws.onConnect(this.init);
  696. },
  697. methods: {
  698. init() {
  699. this.getData();
  700. },
  701. getData() {
  702. this.socket.dispatch(
  703. this.dataAction,
  704. this.page,
  705. this.pageSize,
  706. this.properties,
  707. this.sort,
  708. this.appliedFilters,
  709. this.appliedFilterOperator,
  710. res => {
  711. console.log(111, res);
  712. if (res.status === "success") {
  713. const { data, count } = res.data;
  714. this.data = data;
  715. this.count = count;
  716. } else {
  717. new Toast(res.message);
  718. }
  719. }
  720. );
  721. },
  722. changePageSize() {
  723. this.getData();
  724. localStorage.setItem("adminPageSize", this.pageSize);
  725. },
  726. changePage(page) {
  727. if (page < 1) return;
  728. if (page > this.lastPage) return;
  729. if (page === this.page) return;
  730. this.page = page;
  731. this.getData();
  732. },
  733. changeSort(column) {
  734. if (column.sortable) {
  735. const { sortProperty } = column;
  736. if (this.sort[sortProperty] === undefined)
  737. this.sort[sortProperty] = "ascending";
  738. else if (this.sort[sortProperty] === "ascending")
  739. this.sort[sortProperty] = "descending";
  740. else if (this.sort[sortProperty] === "descending")
  741. delete this.sort[sortProperty];
  742. this.getData();
  743. }
  744. },
  745. toggleColumnVisibility(column) {
  746. if (this.shownColumns.indexOf(column.name) !== -1) {
  747. if (this.shownColumns.length <= 2)
  748. return new Toast(
  749. `Unable to hide column ${column.displayName}, there must be at least 1 visibile column`
  750. );
  751. this.shownColumns.splice(
  752. this.shownColumns.indexOf(column.name),
  753. 1
  754. );
  755. } else {
  756. this.shownColumns.push(column.name);
  757. }
  758. return this.getData();
  759. },
  760. togglePinnedColumn(column) {
  761. if (this.pinnedColumns.indexOf(column.name) !== -1) {
  762. this.pinnedColumns.splice(
  763. this.pinnedColumns.indexOf(column.name),
  764. 1
  765. );
  766. } else {
  767. this.pinnedColumns.push(column.name);
  768. }
  769. },
  770. toggleSelectedRow(itemIndex, event) {
  771. const { shiftKey } = event;
  772. // Shift was pressed, so attempt to select all items between the clicked item and last clicked item
  773. if (shiftKey) {
  774. // If there is a last clicked item
  775. if (this.lastSelectedItemIndex >= 0) {
  776. // Clicked item is lower than last item, so select upwards until it reaches the last selected item
  777. if (itemIndex > this.lastSelectedItemIndex) {
  778. for (
  779. let itemIndexUp = itemIndex;
  780. itemIndexUp > this.lastSelectedItemIndex;
  781. itemIndexUp -= 1
  782. ) {
  783. this.data[itemIndexUp].selected = true;
  784. }
  785. }
  786. // Clicked item is higher than last item, so select downwards until it reaches the last selected item
  787. else if (itemIndex < this.lastSelectedItemIndex) {
  788. for (
  789. let itemIndexDown = itemIndex;
  790. itemIndexDown < this.lastSelectedItemIndex;
  791. itemIndexDown += 1
  792. ) {
  793. this.data[itemIndexDown].selected = true;
  794. }
  795. }
  796. }
  797. } else {
  798. this.data[itemIndex].selected = !this.data[itemIndex].selected;
  799. }
  800. // Set the last clicked item to no longer be highlighted, if it exists
  801. if (this.lastSelectedItemIndex >= 0)
  802. this.data[this.lastSelectedItemIndex].highlighted = false;
  803. // Set the clicked item to be highlighted
  804. this.data[itemIndex].highlighted = true;
  805. },
  806. toggleAllRows() {
  807. if (this.data.length > this.selectedRows.length) {
  808. this.data = this.data.map(row => ({ ...row, selected: true }));
  809. } else {
  810. this.data = this.data.map(row => ({ ...row, selected: false }));
  811. }
  812. },
  813. addFilterItem() {
  814. this.editingFilters.push({
  815. data: "",
  816. filter: this.addFilterValue,
  817. filterType: this.addFilterValue.defaultFilterType
  818. });
  819. },
  820. removeFilterItem(index) {
  821. this.editingFilters.splice(index, 1);
  822. },
  823. columnResizingMouseDown(column, event) {
  824. this.resizing.resizing = true;
  825. this.resizing.resizingColumn = column;
  826. this.resizing.width = event.target.parentElement.offsetWidth;
  827. this.resizing.lastX = event.x;
  828. },
  829. columnResizingMouseMove(event) {
  830. if (this.resizing.resizing) {
  831. if (event.buttons !== 1) {
  832. this.resizing.resizing = false;
  833. }
  834. this.resizing.width -= this.resizing.lastX - event.x;
  835. this.resizing.lastX = event.x;
  836. if (
  837. this.resizing.resizingColumn.minWidth &&
  838. this.resizing.resizingColumn.maxWidth
  839. ) {
  840. this.resizing.resizingColumn.width = Math.max(
  841. Math.min(
  842. this.resizing.resizingColumn.maxWidth,
  843. this.resizing.width
  844. ),
  845. this.resizing.resizingColumn.minWidth
  846. );
  847. } else if (this.resizing.resizingColumn.minWidth) {
  848. this.resizing.resizingColumn.width = Math.max(
  849. this.resizing.width,
  850. this.resizing.resizingColumn.minWidth
  851. );
  852. } else if (this.resizing.resizingColumn.maxWidth) {
  853. this.resizing.resizingColumn.width = Math.min(
  854. this.resizing.resizingColumn.maxWidth,
  855. this.resizing.width
  856. );
  857. } else {
  858. this.resizing.resizingColumn.width = this.resizing.width;
  859. }
  860. console.log(`New width: ${this.resizing.width}px`);
  861. }
  862. },
  863. columnResizingMouseUp() {
  864. this.resizing.resizing = false;
  865. },
  866. columnResetWidth(column) {
  867. // eslint-disable-next-line no-param-reassign
  868. column.minWidth = column.maxWidth = "";
  869. },
  870. filterTypes(filter) {
  871. if (!filter || !filter.filterTypes) return [];
  872. return filter.filterTypes.map(
  873. filterType => this.allFilterTypes[filterType]
  874. );
  875. },
  876. changeFilterType(index) {
  877. this.editingFilters[index].filterType =
  878. this.editingFilters[index].filter.defaultFilterType;
  879. },
  880. onDragBox(e) {
  881. const e1 = e || window.event;
  882. e1.preventDefault();
  883. this.bulkPopup.pos3 = e1.clientX;
  884. this.bulkPopup.pos4 = e1.clientY;
  885. document.onmousemove = e => {
  886. const e2 = e || window.event;
  887. e2.preventDefault();
  888. // calculate the new cursor position:
  889. this.bulkPopup.pos1 = this.bulkPopup.pos3 - e.clientX;
  890. this.bulkPopup.pos2 = this.bulkPopup.pos4 - e.clientY;
  891. this.bulkPopup.pos3 = e.clientX;
  892. this.bulkPopup.pos4 = e.clientY;
  893. // set the element's new position:
  894. this.bulkPopup.top -= this.bulkPopup.pos2;
  895. this.bulkPopup.left -= this.bulkPopup.pos1;
  896. if (this.bulkPopup.top < 0) this.bulkPopup.top = 0;
  897. if (this.bulkPopup.top > document.body.clientHeight - 50)
  898. this.bulkPopup.top = document.body.clientHeight - 50;
  899. if (this.bulkPopup.left < 0) this.bulkPopup.left = 0;
  900. if (this.bulkPopup.left > document.body.clientWidth - 400)
  901. this.bulkPopup.left = document.body.clientWidth - 400;
  902. };
  903. document.onmouseup = () => {
  904. document.onmouseup = null;
  905. document.onmousemove = null;
  906. };
  907. },
  908. resetBulkActionsPosition() {
  909. this.bulkPopup.top = document.body.clientHeight - 56;
  910. this.bulkPopup.left = document.body.clientWidth / 2 - 200;
  911. },
  912. applyFilterAndGetData() {
  913. this.appliedFilters = JSON.parse(
  914. JSON.stringify(this.editingFilters)
  915. );
  916. this.appliedFilterOperator = this.filterOperator;
  917. this.getData();
  918. }
  919. }
  920. };
  921. </script>
  922. <style lang="scss" scoped>
  923. .night-mode {
  924. .table-outer-container {
  925. .table-container .table {
  926. &,
  927. thead th {
  928. background-color: var(--dark-grey-3);
  929. color: var(--light-grey-2);
  930. }
  931. tr {
  932. &:nth-child(even) {
  933. background-color: var(--dark-grey-2);
  934. }
  935. &:hover,
  936. &:focus,
  937. &.highlighted {
  938. background-color: var(--dark-grey-4);
  939. }
  940. }
  941. th,
  942. td {
  943. border-color: var(--dark-grey) !important;
  944. }
  945. }
  946. .table-header,
  947. .table-footer {
  948. background-color: var(--dark-grey-3);
  949. color: var(--light-grey-2);
  950. }
  951. }
  952. .bulk-popup {
  953. border: 0;
  954. background-color: var(--dark-grey-2);
  955. color: var(--white);
  956. .material-icons {
  957. color: var(--white);
  958. }
  959. }
  960. }
  961. .table-outer-container {
  962. border-radius: 5px;
  963. box-shadow: 0 2px 3px rgba(10, 10, 10, 0.1), 0 0 0 1px rgba(10, 10, 10, 0.1);
  964. margin: 10px 0;
  965. overflow: hidden;
  966. .table-container {
  967. overflow-x: auto;
  968. table {
  969. border-collapse: separate;
  970. table-layout: fixed;
  971. thead {
  972. tr {
  973. th {
  974. height: 40px;
  975. line-height: 40px;
  976. border: 1px solid var(--light-grey-2);
  977. border-width: 1px 1px 1px 0;
  978. &:last-child {
  979. border-width: 1px 0 1px;
  980. }
  981. &.sortable {
  982. cursor: pointer;
  983. }
  984. & > div {
  985. display: flex;
  986. white-space: nowrap;
  987. & > span {
  988. margin-left: 5px;
  989. &:first-child {
  990. margin-left: 0;
  991. margin-right: auto;
  992. }
  993. & > .material-icons {
  994. font-size: 22px;
  995. position: relative;
  996. top: 6px;
  997. cursor: pointer;
  998. &.active {
  999. color: var(--primary-color);
  1000. }
  1001. &:hover,
  1002. &:focus {
  1003. filter: brightness(90%);
  1004. }
  1005. }
  1006. }
  1007. }
  1008. }
  1009. }
  1010. }
  1011. tbody {
  1012. tr {
  1013. &.highlighted {
  1014. background-color: var(--light-grey);
  1015. }
  1016. td {
  1017. border: 1px solid var(--light-grey-2);
  1018. border-width: 0 1px 1px 0;
  1019. &:last-child {
  1020. border-width: 0 0 1px;
  1021. }
  1022. }
  1023. }
  1024. }
  1025. }
  1026. table thead tr th,
  1027. table tbody tr td {
  1028. position: relative;
  1029. white-space: nowrap;
  1030. text-overflow: ellipsis;
  1031. overflow: hidden;
  1032. .resizer {
  1033. height: 100%;
  1034. width: 5px;
  1035. background-color: transparent;
  1036. cursor: col-resize;
  1037. position: absolute;
  1038. right: 0;
  1039. top: 0;
  1040. }
  1041. }
  1042. }
  1043. .table-header,
  1044. .table-footer {
  1045. display: flex;
  1046. flex-direction: row;
  1047. flex-wrap: wrap;
  1048. justify-content: space-between;
  1049. line-height: 36px;
  1050. background-color: var(--white);
  1051. }
  1052. .table-header > div {
  1053. display: flex;
  1054. flex-direction: row;
  1055. > span > .button {
  1056. margin: 5px;
  1057. }
  1058. .filters-indicator {
  1059. line-height: 46px;
  1060. display: flex;
  1061. align-items: center;
  1062. column-gap: 4px;
  1063. }
  1064. }
  1065. .table-footer {
  1066. .page-controls,
  1067. .page-size > .control {
  1068. display: flex;
  1069. flex-direction: row;
  1070. margin-bottom: 0 !important;
  1071. button {
  1072. margin: 5px;
  1073. font-size: 20px;
  1074. }
  1075. p,
  1076. label {
  1077. margin: 5px;
  1078. font-size: 14px;
  1079. font-weight: 600;
  1080. }
  1081. &.select::after {
  1082. top: 18px;
  1083. }
  1084. }
  1085. }
  1086. }
  1087. .control.is-grouped {
  1088. display: flex;
  1089. & > .control {
  1090. &.label {
  1091. height: 36px;
  1092. background-color: var(--white);
  1093. border: 1px solid var(--light-grey-2);
  1094. color: var(--dark-grey-2);
  1095. appearance: none;
  1096. border-radius: 3px;
  1097. font-size: 14px;
  1098. line-height: 34px;
  1099. padding-left: 8px;
  1100. padding-right: 8px;
  1101. }
  1102. &.select.is-expanded > select {
  1103. width: 100%;
  1104. }
  1105. & > input,
  1106. & > select,
  1107. & > .button,
  1108. &.label {
  1109. border-radius: 0;
  1110. }
  1111. &:first-child {
  1112. & > input,
  1113. & > select,
  1114. & > .button,
  1115. &.label {
  1116. border-radius: 5px 0 0 5px;
  1117. }
  1118. }
  1119. &:last-child {
  1120. & > input,
  1121. & > select,
  1122. & > .button,
  1123. &.label {
  1124. border-radius: 0 5px 5px 0;
  1125. }
  1126. }
  1127. & > .button {
  1128. font-size: 22px;
  1129. }
  1130. }
  1131. }
  1132. .advanced-filter-bottom {
  1133. display: flex;
  1134. .button {
  1135. font-size: 16px !important;
  1136. width: 100%;
  1137. }
  1138. .control {
  1139. margin: 0 !important;
  1140. }
  1141. }
  1142. .bulk-popup {
  1143. display: flex;
  1144. position: fixed;
  1145. flex-direction: row;
  1146. width: 100%;
  1147. max-width: 400px;
  1148. line-height: 36px;
  1149. z-index: 5;
  1150. border: 1px solid var(--light-grey-3);
  1151. border-radius: 5px;
  1152. box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
  1153. background-color: var(--white);
  1154. color: var(--dark-grey);
  1155. padding: 5px;
  1156. .right {
  1157. display: flex;
  1158. flex-direction: row;
  1159. margin-left: auto;
  1160. }
  1161. .drag-icon {
  1162. position: relative;
  1163. top: 6px;
  1164. color: var(--dark-grey);
  1165. cursor: move;
  1166. }
  1167. }
  1168. </style>