【问题标题】:How to fit p-dropdown inside of a table cell?如何在表格单元格内放置 p-dropdown?
【发布时间】:2019-09-26 13:09:39
【问题描述】:

我正在将 p-dropdown 添加到类似于此处所见的 p-table 中:https://www.primefaces.org/primeng/#/table/filter

p-dropdown 溢出到下一个单元格。如何防止 p-dropdown 流到下一个单元格?

我尝试了以下方法:
- 添加 [style]="{'width':'100%'}" 到 p-dropdown 元素
- 将 [autoWidth]="true" 添加到 p-dropdown 元素
- 为 p-dropdown 元素添加最大宽度

<p-table [columns]="cols" [value]="wfdata" [paginator]="true" [rows]="10">

...

<th *ngFor="let col of cols" [ngSwitch]="col.value">
          <p-dropdown *ngSwitchCase="'invoice_status'" 
                    [options]="statuses" 
                    [autoWidth]="true"
                    [style]="{'width':'100%'}"></p-dropdown>

...

</p-table>

【问题讨论】:

标签: javascript html css angular primeng


【解决方案1】:

使用它可能会解决您的问题

  <p-dropdown *ngSwitchCase="'status'" [options]="order_status" [style]="{ width: '100%', overflow: 'visible' }" appendTo="body"
          (onChange)="dt.filter($event.value, retailer.field, 'equals')"></p-dropdown>

【讨论】:

  • 添加溢出可见和附加到正文导致相同的方式 - 它仍然溢出到下一个单元格:(
  • &lt;p-dropdown *ngSwitchCase="'invoice_status'" [options]="statuses" [style]="{ width: '100%', overflow: 'visible' }" appendTo="body"&gt;&lt;/p-dropdown&gt;
  • 你能创建一个 StackBlitz 吗?
  • 即使这解决了问题——这对我来说不是,但显然是@TyCox94,因为他将此标记为正确答案——如果你能解释原因而不是仅仅给出一段“可能”解决问题的代码。
【解决方案2】:

你必须把 th 放在一个 tr 标签中:

参见网站的 Prime Ng 过滤器示例:

export class TableFilterDemo implements OnInit {

    cars: Car[];

    cols: any[];

    brands: SelectItem[];

    colors: SelectItem[];

    yearFilter: number;

    yearTimeout: any;

    constructor(private carService: CarService) { }

    ngOnInit() {
        this.carService.getCarsMedium().then(cars => this.cars = cars);

        this.brands = [
            { label: 'All Brands', value: null },
            { label: 'Audi', value: 'Audi' },
            { label: 'BMW', value: 'BMW' },
            { label: 'Fiat', value: 'Fiat' },
            { label: 'Honda', value: 'Honda' },
            { label: 'Jaguar', value: 'Jaguar' },
            { label: 'Mercedes', value: 'Mercedes' },
            { label: 'Renault', value: 'Renault' },
            { label: 'VW', value: 'VW' },
            { label: 'Volvo', value: 'Volvo' }
        ];

        this.colors = [
            { label: 'White', value: 'White' },
            { label: 'Green', value: 'Green' },
            { label: 'Silver', value: 'Silver' },
            { label: 'Black', value: 'Black' },
            { label: 'Red', value: 'Red' },
            { label: 'Maroon', value: 'Maroon' },
            { label: 'Brown', value: 'Brown' },
            { label: 'Orange', value: 'Orange' },
            { label: 'Blue', value: 'Blue' }
        ];

        this.cols = [
            { field: 'vin', header: 'Vin' },
            { field: 'year', header: 'Year' },
            { field: 'brand', header: 'Brand' },
            { field: 'color', header: 'Color' }
        ];
    }

    onYearChange(event, dt) {
        if (this.yearTimeout) {
            clearTimeout(this.yearTimeout);
        }

        this.yearTimeout = setTimeout(() => {
            dt.filter(event.value, 'year', 'gt');
        }, 250);
    }
}

和html文件:

<p-table #dt [columns]="cols" [value]="cars" [paginator]="true" [rows]="10">
    <ng-template pTemplate="caption">
        <div style="text-align: right">        
            <i class="fa fa-search" style="margin:4px 4px 0 0"></i>
            <input type="text" pInputText size="50" placeholder="Global Filter" (input)="dt.filterGlobal($event.target.value, 'contains')" style="width:auto">
        </div>
    </ng-template>
    <ng-template pTemplate="header" let-columns>
        <tr>
            <th *ngFor="let col of columns">
                {{col.header}}
            </th>
        </tr>
        <tr>
            <th *ngFor="let col of columns" [ngSwitch]="col.field">
                <input *ngSwitchCase="'vin'" pInputText type="text" (input)="dt.filter($event.target.value, col.field, col.filterMatchMode)">
                <div *ngSwitchCase="'year'">
                    Value < {{yearFilter}}
                    <i class="fa fa-close" (click)="yearFilter=null;dt.filter(null, col.field, col.filterMatchMode)" style="cursor:pointer" *ngIf="yearFilter"></i>
                    <p-slider [style]="{'width':'100%','margin-top':'8px'}" [(ngModel)]="yearFilter" [min]="1970" [max]="2010" (onChange)="onYearChange($event, dt)"></p-slider>
                </div>
                <p-dropdown *ngSwitchCase="'brand'" [options]="brands" [style]="{'width':'100%'}" (onChange)="dt.filter($event.value, col.field, 'equals')"></p-dropdown>
                <p-multiSelect *ngSwitchCase="'color'" [options]="colors" defaultLabel="All Colors" (onChange)="dt.filter($event.value, col.field, 'in')"></p-multiSelect>
            </th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" let-rowData let-columns="columns">
        <tr [pSelectableRow]="rowData">
            <td *ngFor="let col of columns">
                {{rowData[col.field]}}
            </td>
        </tr>
    </ng-template>
</p-table>

【讨论】:

    【解决方案3】:

    在 p-dropdown 组件内部有一个类 .ui-dropdown 这将 min-width 设置为固定值 12.5em; 所以如果您将最小宽度设置为 0initial 将解决问题

    style.scss

    .base-table { 
      p-dropdown {
        .ui-dropdown {
          min-width:0;
        }
      }
    }
    

    或者这样会重置所有项目中p-dropdown 组件的最小宽度

     p-dropdown {
        .ui-dropdown {
          min-width:0; // auto , initial ?
        }
      }
    

    demo ??

    【讨论】:

      【解决方案4】:

      您可以将min-width:100% 添加到 p-dropdown 元素:

      <p-dropdown [style]="{'width':'100%','min-width':'100%'}" *ngSwitchCase="'invoice_status'" [options]="statuses" [autoWidth]="true"></p-dropdown>
      

      【讨论】:

      • 这也会起作用,主要问题是默认值为12.5em,因此相关或0将解决问题??
      【解决方案5】:

      如果有人能派上用场,我会很高兴:

      p-table {
        ::ng-deep p-dropdown {
          .ui-dropdown {
            min-width: 0;
          }
        }
      }

      【讨论】:

      • 请解释一下你的片段,这是个问题吗?解决方案?
      • @R.S 它的解决方案
      【解决方案6】:

      您可以将 appendTo="body" 属性添加到 p-dropdown。如果您在 p-table 正文中包含下拉菜单,而不更改任何 CSS,它将解决此问题。

      AppendTo 在 primeNG p-dropdown 中附加叠加层的目标元素

      </p-table>
            ......
        <ng-template pTemplate="body" let-rowData>
          <tr>
             <td>
                <p-dropdown .... [options]="statuses" appendTo="body"></p-dropdown>
             </td>
          </tr>
        </ng-template>
      </p-table>
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      • 2020-09-07
      • 2010-10-06
      • 2011-04-03
      相关资源
      最近更新 更多