数据可视化大屏
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lj-ag-grid-component.component.ts 4.7KB

преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { CommonModule } from '@angular/common';
  2. import { Component, OnInit, ViewChild, Input } from '@angular/core';
  3. import { AgGridAngular } from 'ag-grid-angular';
  4. import { ColDef, GridApi, GridOptions, GridSizeChangedEvent } from 'ag-grid-community';
  5. import { NzPaginationModule } from 'ng-zorro-antd/pagination';
  6. // eslint-disable-next-line import/no-unassigned-import
  7. import 'ag-grid-community/styles/ag-theme-quartz.css';
  8. import { LJDataVCardComponent } from '../../lj-card/lj-card.component';
  9. import { TableColumn, DataVLjTableComponent } from '../../lj-table/lj-table.component';
  10. @Component({
  11. selector: 'lj-app-ag-grid-component',
  12. standalone: true,
  13. templateUrl: './lj-ag-grid-component.component.html',
  14. styleUrls: ['./pagination-style/pagination.less', './lj-ag-grid-component.component.less'],
  15. imports: [CommonModule, AgGridAngular, NzPaginationModule, LJDataVCardComponent, DataVLjTableComponent]
  16. })
  17. export class LjAgGridComponentComponent implements OnInit {
  18. [x: string]: any;
  19. @ViewChild('myGrid') grid!: AgGridAngular;
  20. @Input() title: string = '';
  21. @Input() heightNum: Number = 0;
  22. // gridOptions: GridOptions = {
  23. // // Set grid width here
  24. // width: '100%'
  25. // };
  26. gridOptions: GridOptions = {
  27. headerHeight: 0,
  28. suppressHorizontalScroll: false,
  29. suppressNoRowsOverlay: true,
  30. domLayout: 'autoHeight'
  31. // columnDefs="columnDefs"
  32. };
  33. @Input() showEditButton: boolean = false;
  34. @Input() showSetting: boolean = false;
  35. @Input() showPageTurning: boolean = true;
  36. /**每页数 */
  37. public pageSize!: number;
  38. public paginationPageSizeSelector!: [number, number, number];
  39. /**当前页 */
  40. public pageIndex!: number;
  41. /**总数 */
  42. public pageRowTotal!: number;
  43. onGridSizeChanged(params: GridSizeChangedEvent) {
  44. // get the current grids width
  45. var gridWidth = document.querySelector('.ag-body-viewport')!.clientWidth;
  46. // keep track of which columns to hide/show
  47. var columnsToShow = [];
  48. var columnsToHide = [];
  49. // iterate over all columns (visible or not) and work out
  50. // now many columns can fit (based on their minWidth)
  51. var totalColsWidth = 0;
  52. var allColumns = params.api.getColumns();
  53. if (allColumns && allColumns.length > 0) {
  54. for (var i = 0; i < allColumns.length; i++) {
  55. var column = allColumns[i];
  56. totalColsWidth += column.getMinWidth() || 0;
  57. if (totalColsWidth > gridWidth) {
  58. columnsToHide.push(column.getColId());
  59. } else {
  60. columnsToShow.push(column.getColId());
  61. }
  62. }
  63. }
  64. // show/hide columns based on current grid width
  65. params.api.setColumnsVisible(columnsToShow, true);
  66. params.api.setColumnsVisible(columnsToHide, false);
  67. // wait until columns stopped moving and fill out
  68. // any available space to ensure there are no gaps
  69. window.setTimeout(() => {
  70. params.api.sizeColumnsToFit();
  71. }, 10);
  72. }
  73. //默认列配置
  74. @Input() defaultColDef: ColDef = {
  75. width: 80,
  76. editable: false,
  77. headerClass: 'ag-header-center',
  78. cellStyle: { 'font-weight': 'bold', textAlign: 'center', 'justify-content': 'center', 'line-height': '38px' }
  79. };
  80. /** 列头 */
  81. @Input() columnDefs: ColDef[] = [];
  82. /**行数据 */
  83. @Input() rowData: any[] = [];
  84. /**表格主题 */
  85. @Input() gridThemeClass: string = 'ag-theme-quartz';
  86. @Input() paginationThemeClass: string = 'ag-theme-quartz';
  87. /** 构造函数 */
  88. constructor() {}
  89. // 在父组件的类中
  90. /**初始化 */
  91. ngOnInit() {
  92. if (this.showPageTurning) {
  93. console.log('showPageTurning:', this.showPageTurning);
  94. } else {
  95. console.log('showPageTurning has not true');
  96. }
  97. if (this.title) {
  98. console.log('Title has been passed:', this.title);
  99. } else {
  100. console.log('Title has not been passed');
  101. }
  102. console.log('-----表格初始化-----');
  103. // this.title = '';
  104. this.pageSize = 3;
  105. this.pageIndex = 1;
  106. this.pageRowTotal = this.rowData.length;
  107. this.paginationPageSizeSelector = [200, 500, 1000];
  108. this.gridOptions = {
  109. headerHeight: 0,
  110. rowData: this.rowData,
  111. columnDefs: this.columnDefs
  112. };
  113. }
  114. //每页数
  115. onGridReady(params: any) {
  116. console.log('onGridReady');
  117. console.log(params);
  118. // setTimeout(() => {
  119. // params.api.sizeColumnsToFit();
  120. // }, 100);
  121. params.api.resetRowHeights();
  122. }
  123. nzPageSizeChange(_pageSize: any) {
  124. this.pageSize = _pageSize;
  125. }
  126. //当前页
  127. nzPageIndexChange(_pageIndex: any) {
  128. console.log(`nzPageIndexChange${_pageIndex}`);
  129. if (this.grid.api) {
  130. if (_pageIndex == 1) {
  131. /**去首页 */
  132. this.grid.api.paginationGoToFirstPage();
  133. } else {
  134. /** 页面跳转 */
  135. this.grid.api.paginationGoToPage(_pageIndex);
  136. }
  137. }
  138. }
  139. }