table_model.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. interface Column {
  2. text: string;
  3. title?: string;
  4. type?: string;
  5. sort?: boolean;
  6. desc?: boolean;
  7. filterable?: boolean;
  8. unit?: string;
  9. }
  10. export default class TableModel {
  11. columns: Column[];
  12. rows: any[];
  13. type: string;
  14. columnMap: any;
  15. constructor() {
  16. this.columns = [];
  17. this.columnMap = {};
  18. this.rows = [];
  19. this.type = 'table';
  20. }
  21. sort(options) {
  22. if (options.col === null || this.columns.length <= options.col) {
  23. return;
  24. }
  25. this.rows.sort((a, b) => {
  26. a = a[options.col];
  27. b = b[options.col];
  28. // Sort null or undefined seperately from comparable values
  29. return +(a == null) - +(b == null) || +(a > b) || -(a < b);
  30. });
  31. if (options.desc) {
  32. this.rows.reverse();
  33. }
  34. this.columns[options.col].sort = true;
  35. this.columns[options.col].desc = options.desc;
  36. }
  37. addColumn(col) {
  38. if (!this.columnMap[col.text]) {
  39. this.columns.push(col);
  40. this.columnMap[col.text] = col;
  41. }
  42. }
  43. addRow(row) {
  44. this.rows.push(row);
  45. }
  46. }