te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>angular - Shrink ag-grid table if enough space - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angular - Shrink ag-grid table if enough space - Stack Overflow

programmeradmin3浏览0评论

I have not found any example or reference about how to shrink an ag-grid table if it already has enough space to fit all columns. The screenshot shows a table spanning the whole window space available, but I would like to center it instead adding auto horizontal margin or vertical align.

I am using Angular 19 and [email protected].

This is my base code:

tableponent.ts

<ag-grid-angular
  domLayout="autoHeight"
  [rowData]="rowData"
  [columnDefs]="colDefs"
  [gridOptions]="gridOptions"
  (gridReady)="onGridReady($event)"
/>

tableponent.ts

import { Component, ElementRef, Input, ViewChild } from "@angular/core";
import { PartService } from "src/app/services/part.service";
import { Part } from "src/app/models/part.model";
import { CommonModule, DatePipe } from "@angular/common";
import { ImgEditComponent } from "src/app/img/edit/editponent";
import { ImgDeleteComponent } from "src/app/img/delete/deleteponent";
import { RouterLink } from "@angular/router";
import { AgGridAngular } from "ag-grid-angular"; // Angular Data Grid Component
import type { ColDef, GridReadyEvent, GridOptions } from "ag-grid-community";

@Component({
  standalone: true,
  selector: "part-table",
  imports: [CommonModule, AgGridAngular],
  templateUrl: "./part-tableponent.html",
  providers: [DatePipe],
  host: { class: 'contents' },
})
export class PartTableComponent {
  private partService: PartService;
  private datePipe: DatePipe;

  parts: Part[] = [];
  @Input() editable: boolean = false;

  @ViewChild("partTable", { static: false }) table!: ElementRef;

  rowData: any[] = []; // Array of row data
  colDefs: ColDef[] = [
    { field: "reference", headerName: "Reference", width: 140, maxWidth: 180 },
    { field: "name", headerName: "Name", minWidth: 150 },
    { field: "coating", headerName: "Coating", width: 100, maxWidth: 120 },
    { field: "comment", headerName: "Comment",minWidth: 150 },
    { field: "client_id", headerName: "Client ID", width: 150, maxWidth: 180 },
    { field: "material_id", headerName: "Material ID", minWidth: 100 },
    {
      field: "created_at",
      headerName: "Created At",
      valueFormatter: (params) =>
        this.datePipe.transform(params.value, "MMM d, y, h:mm a") ?? "",
      width: 180,
      maxWidth: 200,
    },
  ];
  
  gridOptions: GridOptions = {
    columnDefs: this.colDefs,
    rowData: this.rowData,
    domLayout: "autoHeight",
    defaultColDef: {
      resizable: true,
      sortable: true,
    },
  };
  
  constructor(partService: PartService, datePipe: DatePipe) {
    this.partService = partService;
    this.datePipe = datePipe;
  }

  onGridReady(params: GridReadyEvent): void {
    this.partService.getParts().subscribe((data: Part[] | null) => {
      if (data) {
        this.parts = data;
        this.rowData = this.parts.map((part: Part) => ({
          reference: part.reference ?? "",
          name: part.name ?? "",
          coating: part.coating ?? "None",
          comment: partment ?? "",
          client_id: part.client_id ?? "",
          material_id: part.material_id ?? "None",
          created_at: part.created_at ?? "",
        }));
      }
    });
  }
}
发布评论

评论列表(0)

  1. 暂无评论