table_model.ts 641 B

12345678910111213141516171819202122232425262728293031323334353637
  1. export default class TableModel {
  2. columns: any[];
  3. rows: any[];
  4. type: string;
  5. constructor() {
  6. this.columns = [];
  7. this.rows = [];
  8. this.type = 'table';
  9. }
  10. sort(options) {
  11. if (options.col === null || this.columns.length <= options.col) {
  12. return;
  13. }
  14. this.rows.sort(function(a, b) {
  15. a = a[options.col];
  16. b = b[options.col];
  17. if (a < b) {
  18. return -1;
  19. }
  20. if (a > b) {
  21. return 1;
  22. }
  23. return 0;
  24. });
  25. this.columns[options.col].sort = true;
  26. if (options.desc) {
  27. this.rows.reverse();
  28. this.columns[options.col].desc = true;
  29. }
  30. }
  31. }