如何在primeng中设置冻结和未冻结列table中列的不同样式和宽度

How to set different style & width of the column in frozen & unfrozen columns table in primeng

我目前正在使用具有冻结列功能的 PrimeNG table 控件。

一切正常,我可以冻结列,但现在我想修改我在下面使用的冻结和解冻列样式以及列的自定义宽度 属性。

代码:

<p-table [columns]="scrollableCols" [frozenColumns]="frozenCols" [value]="cars" [scrollable]="true" scrollHeight="300px" frozenWidth="250px">
  <ng-template pTemplate="colgroup" let-columns>
    <colgroup>
      <col style="width:200px"> 
      <col style="width:50px">
      <col style="width:100px">
      <col style="width:100px"> 
      <col style="width:100px">
      <col style="width:100px">
    </colgroup>
  </ng-template>
  <ng-template pTemplate="header" let-columns>
    <tr>
      <th *ngFor="let col of columns">
        {{col.header}}
      </th>
    </tr>
  </ng-template>
  <ng-template pTemplate="body" let-rowData let-columns="columns">
    <tr>
      <td *ngFor="let col of columns">
        {{rowData[col.field]}}
      </td>
    </tr>
  </ng-template>
</p-table>

输出:

通过使用宽度,我能够实现我的列的宽度,但是对于前两列,冻结和解冻列 table 自动为所有 2 列采用相同的宽度我想分配不同的大小冻结和解冻列 s 中的列数。

是否可以为冻结和解冻列添加不同的大小?

你只需要 pTemplate="frozencolgroup"

<ng-template pTemplate="frozencolgroup" let-columns>
        <colgroup>
            <col style="width:200px">
            <col style="width:50px">
        </colgroup>
    </ng-template>
    <ng-template pTemplate="colgroup" let-columns>
        <colgroup>
            <col style="width:100px">
            <col style="width:100px">
            <col style="width:100px">
            <col style="width:100px">
        </colgroup>
    </ng-template>

演示 here

更新: 滚动 table 可以打破行高。下面的提示函数可以解决这个问题

makeRowsSameHeight() {
    setTimeout(() => {
      if ($('.ui-table-scrollable-wrapper').length) {
        let wrapper = $('.ui-table-scrollable-wrapper');
        wrapper.each(function () {
          let w = $(this);
          let frozen_rows: any = w.find('.ui-table-frozen-view tr');
          let unfrozen_rows = w.find('.ui-table-unfrozen-view tr');
          for (let i = 0; i < frozen_rows.length; i++) {
            if (frozen_rows.eq(i).height() > unfrozen_rows.eq(i).height()) {
              unfrozen_rows.eq(i).height(frozen_rows.eq(i).height());
            } else if (frozen_rows.eq(i).height() < unfrozen_rows.eq(i).height()) {
              frozen_rows.eq(i).height(unfrozen_rows.eq(i).height());
            }
          }
        });
      }
    });
  }

更新演示:https://stackblitz.com/edit/angular-primeng-table-frozen-columns-dpsm8l