【问题标题】:Putting custom templating in one header only (PrimeNG)仅将自定义模板放在一个标题中(PrimeNG)
【发布时间】:2017-11-29 19:49:58
【问题描述】:

我试图在使用 PrimeNG 进行排序时一次仅将一个类(箭头 svg)应用于一个列标题,但是我的 ng-container 中的 ngIf 状态不起作用。我在排序时将所选字段设置为 selectedCol。这里有什么问题?

table-layout.component.html

<p-dataTable 
[value]="results" 
[paginator]="true" 
[rows]=6 
class="ds-u-sans ds-c-table" 
(onSort)="handleSorting($event)"
>
  <p-column 
  *ngFor="let col of cols" 
  [field]="col.field" 
  [header]="col.header" 
  [sortable]="col.sortable" 
  [filter]="col.filter"
  >
    <ng-container *ngIf="col.header === selectedCol">
      <ng-template pTemplate="header">
        <div [ngClass]="sorted ? 'up' : 'down'"></div>
      </ng-template>
    </ng-container>

  </p-column>
</p-dataTable>

table-layout.component.ts

import { BrowserModule } from '@angular/platform-browser'
import { Component, OnInit, NgModule, ViewEncapsulation } from '@angular/core'
import { HttpClient } from '@angular/common/http'

import { cols } from './cols'

@Component({
  selector: 'app-table-layout',
  templateUrl: './table-layout.component.html',
  styleUrls: ['./table-layout.component.css'],
  encapsulation: ViewEncapsulation.None
})

export class TableLayoutComponent implements OnInit {

  ROOT_URL: string = 'https://jsonplaceholder.typicode.com/users'
  results: any[]
  sorted: boolean = false;
  cols: any[]
  selectedCol: string

  constructor(private http: HttpClient) { }

  ngOnInit() {
    this.getData();
    this.cols = cols
  }

  getData() {
    this.http.get<any[]>(this.ROOT_URL).subscribe(data => this.results = data)
  }

  handleSorting(event) {
    this.sorted = !this.sorted
    this.selectedCol = event.field
  }

}

cols.ts

export const cols = [
    { 
        field: 'id', 
        header: 'ID', 
        sortable: true 
    },
    { 
        field: 'name', 
        header: 'Name', 
        sortable: true, 
        filter: true 
    },
    { 
        field: 'username', 
        header: 'Username', 
        sortable: true, 
        filter: true 
    },
    { 
        field: 'address.zipcode', 
        header: 'Zipcode', 
        sortable: true, 
        filter: true 
    }
  ]

【问题讨论】:

    标签: angular primeng ng-template


    【解决方案1】:
      <ng-template let-col pTemplate="header" > 
        <div [ngClass]="col.field === selectedCol ? 'up' : 'down'">
            {{col.header}}
        </div>
      </ng-template>
    

    试试这个。 Stackblitz:https://angular-9khpho.stackblitz.io/

    【讨论】:

      猜你喜欢
      • 2019-01-01
      • 2018-05-05
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      • 2014-09-03
      • 2015-01-20
      • 2019-03-15
      相关资源
      最近更新 更多