controller.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular = require('angular');
  3. import _ = require('lodash');
  4. import moment = require('moment');
  5. import PanelMeta = require('app/features/panel/panel_meta');
  6. import {TableModel} from './table_model';
  7. export class TablePanelCtrl {
  8. constructor($scope, $rootScope, $q, panelSrv, panelHelper) {
  9. $scope.ctrl = this;
  10. $scope.pageIndex = 0;
  11. $scope.panelMeta = new PanelMeta({
  12. panelName: 'Table',
  13. editIcon: "fa fa-table",
  14. fullscreen: true,
  15. metricsEditor: true,
  16. });
  17. $scope.panelMeta.addEditorTab('Options', 'app/panels/table/options.html');
  18. $scope.panelMeta.addEditorTab('Time range', 'app/features/panel/partials/panelTime.html');
  19. var panelDefaults = {
  20. targets: [{}],
  21. transform: 'timeseries_to_rows',
  22. pageSize: 50,
  23. showHeader: true,
  24. columns: [],
  25. fields: [],
  26. sort: {col: null, desc: true},
  27. };
  28. $scope.init = function() {
  29. _.defaults($scope.panel, panelDefaults);
  30. if ($scope.panel.columns.length === 0) {
  31. }
  32. panelSrv.init($scope);
  33. };
  34. $scope.refreshData = function(datasource) {
  35. panelHelper.updateTimeRange($scope);
  36. return panelHelper.issueMetricQuery($scope, datasource)
  37. .then($scope.dataHandler, function(err) {
  38. $scope.seriesList = [];
  39. $scope.render([]);
  40. throw err;
  41. });
  42. };
  43. $scope.toggleColumnSort = function(col, colIndex) {
  44. if ($scope.panel.sort.col === colIndex) {
  45. if ($scope.panel.sort.desc) {
  46. $scope.panel.sort.desc = false;
  47. } else {
  48. $scope.panel.sort.col = null;
  49. }
  50. } else {
  51. $scope.panel.sort.col = colIndex;
  52. $scope.panel.sort.desc = true;
  53. }
  54. $scope.render();
  55. };
  56. $scope.dataHandler = function(results) {
  57. $scope.dataRaw = results.data;
  58. $scope.render();
  59. };
  60. $scope.render = function() {
  61. $scope.table = TableModel.transform($scope.dataRaw, $scope.panel);
  62. $scope.table.sort($scope.panel.sort);
  63. panelHelper.broadcastRender($scope, $scope.table, $scope.dataRaw);
  64. };
  65. $scope.init();
  66. }
  67. }