| <template> |
| <div class="subtle-box mb-3" v-if="tableDataToUse"> |
| <span v-if="tableDataToUse.headers.length > 0" class="table-title px-2"> |
| {{ caption }} |
| </span> |
| <slot name="afterHeader"></slot> |
| |
| <div class="table-responsive"> |
| <table :class="'table table-striped table-hover table-sm mb-0 pb-0 table-borderless' + |
| (noFixedTable ? '' : ' fixed-table') |
| "> |
| <thead style="border-top: 0"> |
| <tr> |
| <th v-for="header of tableDataToUse.headers" v-bind:key="header.text" |
| class="sticky-header px-2 cell" :style="'font-weight: 550;' + |
| (header.width |
| ? 'width:' + header.width + 'px;' |
| : '') + |
| (isHeaderSortable(header) |
| ? 'cursor: pointer;' |
| : '') |
| " @click="headerClick(header)"> |
| <span v-if="isHeaderSortable(header)"> |
| <Icon style="color: #212529" :icon="['fa', headerIcon(header)]" /> |
| </span> |
| |
| <Tooltip :tip="getHeaderToolTipText(header)"> |
| {{ header.text }} |
| </Tooltip> |
| </th> |
| </tr> |
| </thead> |
| <tfoot> |
| <tr> |
| <td :colspan="tableData?.headers.length" class="px-2"> |
| Download as |
| <a @click.prevent="download('csv')" class="link-primary">CSV</a>, |
| <a @click.prevent="download('xlsx')" class="link-primary">XLSX</a>, or |
| <a @click.prevent="download('json')" class="link-primary">JSON</a> |
| </td> |
| </tr> |
| </tfoot> |
| <tbody> |
| <tr v-for="(row, rowIdx) of tableDataToUse.rows" v-bind:key="rowIdx"> |
| <td v-for="header of tableDataToUse.headers" v-bind:key="header.text" |
| @click="rowClicked(rowIdx, getCell(row[header.text]).val.toString())" class="cell px-2" |
| :style="clickableRows ? 'cursor: pointer;' : ''"> |
| <Tooltip :tip="getCellToolTipText( |
| getCell(row[header.text]) |
| ) |
| "> |
| <span v-html="getCell(row[header.text]).val"></span> |
| <div v-if=" |
| showIcon(getCell(row[header.text]), row) |
| " class="icon-clickable" @click.stop=" |
| iconClicked( |
| getCell(row[header.text]) |
| .iconClickEmitName, |
| row |
| ) |
| "> |
| <Icon :icon="getCell(row[header.text]) |
| .iconClasses |
| " /> |
| </div> |
| </Tooltip> |
| </td> |
| </tr> |
| </tbody> |
| </table> |
| </div> |
| </div> |
| </template> |
| |
| <script lang="ts"> |
| import { saveData } from "@/Core/FS/FS"; |
| import Tooltip from "@/UI/MessageAlerts/Tooltip.vue"; |
| import { Options, Vue } from "vue-class-component"; |
| import { Prop } from "vue-property-decorator"; |
| import { ITableData, CellValue, ICellValue, IHeader } from "./Types"; |
| import Icon from "../Icon.vue"; |
| import { IDataRows } from "@/Core/FS/FSInterfaces"; |
| import { slugify } from "@/Core/Utils/StringUtils"; |
| import { dynamicImports } from "@/Core/DynamicImports"; |
| import { addToast } from "@/UI/MessageAlerts/Toasts/ToastManager"; |
| import { PopupVariant } from "@/UI/MessageAlerts/Popups/InterfacesAndEnums"; |
| |
| |
| |
| interface ITableDataInternal { |
| headers: IHeader[]; |
| rows: { [key: string]: ICellValue }[]; |
| } |
| |
| |
| |
| |
| @Options({ |
| components: { |
| Tooltip, |
| Icon, |
| }, |
| }) |
| export default class Table extends Vue { |
| @Prop({ default: { headers: [], rows: [] } }) tableData!: ITableData; |
| @Prop({ default: 2 }) precision!: number; |
| @Prop({ default: "" }) caption!: string; |
| @Prop({ required: true }) downloadFilenameBase!: string; |
| @Prop({ default: true }) noFixedTable!: boolean; |
| @Prop({ default: false }) clickableRows!: boolean; |
| @Prop({ default: "" }) initialSortColumnName!: string; |
| @Prop({ default: "asc" }) initialSortOrder!: string; |
| |
| sortColumnName = ""; |
| sortOrder = "asc"; |
| |
| |
| |
| |
| |
| |
| get tableDataToUse(): ITableDataInternal { |
| const dataToUse = { |
| headers: this.tableData.headers.map((h) => h), |
| rows: [] as { [key: string]: ICellValue }[], |
| }; |
| |
| |
| |
| for (const row of this.tableData.rows) { |
| const newRow: { [key: string]: ICellValue } = {}; |
| for (const header of this.tableData.headers) { |
| let rowVal = row[header.text] as CellValue; |
| |
| if (rowVal === undefined) { |
| |
| rowVal = { val: "" }; |
| } |
| |
| |
| if (typeof rowVal === "string" || typeof rowVal === "number") { |
| rowVal = { val: rowVal }; |
| } |
| |
| |
| if (typeof rowVal.val === "number" && rowVal.val % 1 !== 0) { |
| |
| rowVal.val = rowVal.val.toFixed(this.precision); |
| |
| if (rowVal.val === "-0") { |
| rowVal.val = "0"; |
| } |
| |
| |
| rowVal.val = Number(rowVal.val); |
| } |
| |
| newRow[header.text] = rowVal; |
| |
| newRow["metaData"] = { |
| val: 0, |
| metaData: { treeNodeId: row.id }, |
| }; |
| } |
| dataToUse.rows.push(newRow); |
| } |
| |
| |
| const headersToHide = this.tableData.headers.filter((header) => { |
| return !this.showColumn(header, dataToUse); |
| }); |
| |
| for (const header of headersToHide) { |
| |
| const headerIndex = dataToUse.headers.indexOf(header); |
| dataToUse.headers.splice(headerIndex, 1); |
| |
| |
| for (const row of dataToUse.rows) { |
| delete row[header.text]; |
| } |
| } |
| |
| |
| if (this.sortColumnName !== "") { |
| dataToUse.rows.sort((a, b) => { |
| const colName = this.sortColumnName as string; |
| const aVal = a[colName] as ICellValue; |
| const bVal = b[colName] as ICellValue; |
| |
| const val1 = |
| aVal.sortVal !== undefined ? aVal.sortVal : aVal.val; |
| const val2 = |
| bVal.sortVal !== undefined ? bVal.sortVal : bVal.val; |
| |
| if (val1 < val2) { |
| return this.sortOrder === "asc" ? -1 : 1; |
| } else if (val1 > val2) { |
| return this.sortOrder === "asc" ? 1 : -1; |
| } else { |
| return 0; |
| } |
| }); |
| } |
| |
| return dataToUse; |
| } |
| |
| |
| |
| |
| |
| |
| |
| getHeaderToolTipText(header: IHeader): string { |
| return header.note !== undefined ? header.note : header.text; |
| } |
| |
| |
| |
| |
| |
| |
| |
| getCellToolTipText(cellValue: ICellValue): string | number { |
| |
| const div = document.createElement("div"); |
| div.innerHTML = cellValue.val as string; |
| |
| return div.textContent || div.innerText || cellValue.val; |
| } |
| |
| |
| |
| |
| |
| |
| |
| isHeaderSortable(header: IHeader): boolean { |
| return header.sortable !== false; |
| } |
| |
| |
| |
| |
| |
| |
| |
| headerIcon(header: IHeader): string { |
| if (header.text === this.sortColumnName) { |
| return this.sortOrder === "asc" ? "sort-up" : "sort-down"; |
| } else { |
| return "sort"; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| headerClick(header: IHeader) { |
| if (!this.isHeaderSortable(header)) { |
| return; |
| } |
| |
| |
| |
| if (this.sortColumnName === header.text) { |
| |
| switch (this.sortOrder) { |
| case "asc": |
| this.sortOrder = "desc"; |
| break; |
| case "desc": |
| this.sortOrder = "asc"; |
| this.sortColumnName = ""; |
| break; |
| } |
| } else { |
| this.sortColumnName = header.text; |
| this.sortOrder = "asc"; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| getCell(cell: CellValue): ICellValue { |
| return cell as ICellValue; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| showIcon(cell: ICellValue, row: { [key: string]: CellValue }): boolean { |
| if (cell.iconClasses === undefined) { |
| |
| return false; |
| } |
| |
| if (cell.iconShowFilterFunc === undefined) { |
| |
| return true; |
| } |
| |
| return cell.iconShowFilterFunc(row); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| showColumn(header: IHeader, tableData: ITableData): boolean { |
| if (header.showColumnFunc === undefined) { |
| |
| return true; |
| } |
| |
| return header.showColumnFunc(tableData); |
| |
| |
| |
| } |
| |
| |
| |
| |
| |
| |
| iconClicked( |
| emitName: string | undefined, |
| row: { [key: string]: CellValue } |
| ) { |
| this.$emit(emitName as string, row); |
| } |
| |
| |
| |
| |
| |
| |
| |
| async rowClicked(rowIdx: number, cellTxt?: string) { |
| if (this.tableDataToUse === undefined) { |
| return; |
| } |
| |
| |
| let toEmit = { |
| ...this.tableDataToUse.rows[rowIdx], |
| }; |
| |
| if (this.tableDataToUse.rows[rowIdx].metaData.metaData) { |
| toEmit = { |
| ...toEmit, |
| ...this.tableDataToUse.rows[rowIdx].metaData.metaData, |
| }; |
| if (toEmit.metaData) { |
| delete toEmit.metaData; |
| } |
| } |
| this.$emit("rowClicked", toEmit); |
| |
| |
| |
| if (cellTxt) { |
| const clipboardJs = await dynamicImports.clipboardJs.module; |
| clipboardJs.copy(cellTxt); |
| addToast( |
| "Copied", |
| "Text copied to clipboard.", |
| PopupVariant.Success, |
| undefined, |
| { duration: 2000 } |
| ); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| download(format: string) { |
| |
| const filename = slugify(this.downloadFilenameBase) + "." + format; |
| |
| const dataToExport = this.tableDataToUse; |
| if (!dataToExport) { |
| return; |
| } |
| const newRows = dataToExport.rows.map( |
| (r: { [key: string]: ICellValue }) => { |
| const row: { [key: string]: any } = {}; |
| for (const header of dataToExport.headers) { |
| const cell = r[header.text]; |
| if (cell !== undefined) { |
| const val = cell.val; |
| |
| const div = document.createElement("div"); |
| div.innerHTML = String(val); |
| const valStripped = div.textContent || div.innerText || val; |
| |
| row[header.text] = valStripped; |
| } |
| } |
| return row; |
| } |
| ); |
| const dataToSave = { |
| headers: dataToExport.headers.map((h: IHeader) => h.text), |
| rows: newRows as IDataRows, |
| }; |
| |
| saveData(dataToSave, filename, format); |
| } |
| |
| |
| |
| |
| mounted() { |
| this.sortColumnName = this.initialSortColumnName; |
| this.sortOrder = this.initialSortOrder; |
| } |
| } |
| </script> |
| |
| |
| <style scoped lang="scss"> |
| .sticky-header { |
| position: sticky; |
| top: 0; |
| } |
| |
| caption { |
| padding-top: 0.15rem; |
| } |
| |
| table { |
| font-size: 14px; |
| } |
| |
| .icon-clickable, |
| .icon-clickable * { |
| cursor: pointer !important; |
| } |
| |
| .fixed-table { |
| table-layout: fixed; |
| } |
| |
| .fixed-table td, |
| .fixed-table th { |
| white-space: nowrap; |
| overflow: hidden; |
| text-overflow: ellipsis; |
| } |
| |
| .cell { |
| white-space: nowrap; |
| } |
| </style> |
| |
| <style lang="scss"> |
| .table-title { |
| font-weight: 550; |
| font-size: 16px; |
| } |
| </style> |
| @/Core/FS/FS |
| |