table_model.ts 878 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. export default class TableModel {
  2. columns: any[];
  3. rows: any[];
  4. type: string;
  5. columnMap: any;
  6. constructor() {
  7. this.columns = [];
  8. this.columnMap = {};
  9. this.rows = [];
  10. this.type = 'table';
  11. }
  12. sort(options) {
  13. if (options.col === null || this.columns.length <= options.col) {
  14. return;
  15. }
  16. this.rows.sort(function(a, b) {
  17. a = a[options.col];
  18. b = b[options.col];
  19. if (a < b) {
  20. return -1;
  21. }
  22. if (a > b) {
  23. return 1;
  24. }
  25. return 0;
  26. });
  27. this.columns[options.col].sort = true;
  28. if (options.desc) {
  29. this.rows.reverse();
  30. this.columns[options.col].desc = true;
  31. } else {
  32. this.columns[options.col].desc = false;
  33. }
  34. }
  35. addColumn(col) {
  36. if (!this.columnMap[col.text]) {
  37. this.columns.push(col);
  38. this.columnMap[col.text] = col;
  39. }
  40. }
  41. }