table_model.ts 895 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. // Sort null or undefined seperately from comparable values
  20. return +(a == null) - +(b == null) || +(a > b) || -(a < b);
  21. });
  22. if (options.desc) {
  23. this.rows.reverse();
  24. }
  25. this.columns[options.col].sort = true;
  26. this.columns[options.col].desc = options.desc;
  27. }
  28. addColumn(col) {
  29. if (!this.columnMap[col.text]) {
  30. this.columns.push(col);
  31. this.columnMap[col.text] = col;
  32. }
  33. }
  34. addRow(row) {
  35. this.rows.push(row);
  36. }
  37. }