import {ComponentContext} from "../../utils/ComponentContext"; import {TableDescriptor} from "../../utils/tables"; import {observer} from "mobx-react"; import {action, makeObservable} from "mobx"; import {FormSelect, Pagination, Table} from "react-bootstrap"; export interface DataTableProps { tableDescriptor: TableDescriptor; } @observer export class DataTable extends ComponentContext> { constructor(props: DataTableProps) { super(props); makeObservable(this); } header() { return {this.props.tableDescriptor.columns.map(column => {column.title})} } body() { const firstColumnKey = this.props.tableDescriptor.columns[0].key; return this.props.tableDescriptor.data.map(row => { const rowAny = row as any; return { this.props.tableDescriptor.columns.map(column => { return {column.format(rowAny[column.key])} }) } }); } isFirstPage() { if (typeof this.props.tableDescriptor.page === 'undefined') { return true; } return this.props.tableDescriptor.page === 0; } isLastPage() { if (typeof this.props.tableDescriptor.page === 'undefined' || typeof this.props.tableDescriptor.pageSize === 'undefined') { return true; } return this.props.tableDescriptor.page === (this.props.tableDescriptor.data.length / this.props.tableDescriptor.pageSize); } @action.bound goFirstPage() { if (typeof this.props.tableDescriptor.page === 'undefined') { return; } this.props.tableDescriptor.page = 0; } @action.bound goLastPage() { if (typeof this.props.tableDescriptor.page === 'undefined' || typeof this.props.tableDescriptor.pageSize === 'undefined') { return; } this.props.tableDescriptor.page = this.props.tableDescriptor.data.length / this.props.tableDescriptor.pageSize; } @action.bound goNextPage() { if (typeof this.props.tableDescriptor.page === 'undefined' || typeof this.props.tableDescriptor.pageSize === 'undefined') { return; } this.props.tableDescriptor.page++; } @action.bound goPrevPage() { if (typeof this.props.tableDescriptor.page === 'undefined') { return; } this.props.tableDescriptor.page--; } @action.bound changePageSize(e: any) { this.props.tableDescriptor.pageSize = parseInt(e.target.value); } footer() { const table = this.props.tableDescriptor; if (typeof table.page === 'undefined' || typeof table.pageSize === 'undefined') { return null; } return
{this.props.tableDescriptor.page}
} render() { const table = this.props.tableDescriptor; return {this.header()} {this.body()} { table.pageable && {this.footer()} }
} }