This commit is contained in:
Çetin
2025-07-24 18:21:45 +03:00
parent d949027ff5
commit 0d3f25a00d

View File

@@ -18,6 +18,7 @@ import { IconFieldModule } from 'primeng/iconfield';
import { TagModule } from 'primeng/tag'; import { TagModule } from 'primeng/tag';
import { Customer, CustomerService, Representative } from '../service/customer.service'; import { Customer, CustomerService, Representative } from '../service/customer.service';
import { Product, ProductService } from '../service/product.service'; import { Product, ProductService } from '../service/product.service';
import {ObjectUtils} from "primeng/utils";
interface expandedRows { interface expandedRows {
[key: string]: boolean; [key: string]: boolean;
@@ -238,80 +239,62 @@ interface expandedRows {
<div class="card"> <div class="card">
<div class="font-semibold text-xl mb-4">Row Expansion</div> <div class="font-semibold text-xl mb-4">Row Expansion</div>
<p-table [value]="products" dataKey="name" [expandedRowKeys]="expandedRows" responsiveLayout="scroll"> <p-table [value]="products" dataKey="id" [tableStyle]="{ 'min-width': '60rem' }" [expandedRowKeys]="expandedRows">
<ng-template #caption> <ng-template #caption>
<button pButton icon="pi pi-fw {{ isExpanded ? 'pi-minus' : 'pi-plus' }}" label="{{ isExpanded ? 'Collapse All' : 'Expand All' }}" (click)="expandAll()"></button> <button pButton icon="pi pi-fw {{ isExpanded ? 'pi-minus' : 'pi-plus' }}" label="{{ isExpanded ? 'Collapse All' : 'Expand All' }}" (click)="expandAll()"></button>
<div class="flex table-header"></div> <div class="flex table-header"></div>
</ng-template> </ng-template>
<ng-template #header> <ng-template #header>
<tr> <tr>
<th style="width: 3rem"></th> <th style="width: 5rem"></th>
<th pSortableColumn="name"> <th pSortableColumn="name">Name <p-sortIcon field="name" /></th>
Name
<p-sortIcon field="name"></p-sortIcon>
</th>
<th>Image</th> <th>Image</th>
<th pSortableColumn="price"> <th pSortableColumn="price">Price <p-sortIcon field="price" /></th>
Price <th pSortableColumn="category">Category <p-sortIcon field="category" /></th>
<p-sortIcon field="price"></p-sortIcon> <th pSortableColumn="rating">Reviews <p-sortIcon field="rating" /></th>
</th> <th pSortableColumn="inventoryStatus">Status <p-sortIcon field="inventoryStatus" /></th>
<th pSortableColumn="category">
Category
<p-sortIcon field="category"></p-sortIcon>
</th>
<th pSortableColumn="rating">
Reviews
<p-sortIcon field="rating"></p-sortIcon>
</th>
<th pSortableColumn="inventoryStatus">
Status
<p-sortIcon field="inventoryStatus"></p-sortIcon>
</th>
</tr> </tr>
</ng-template> </ng-template>
<ng-template #body let-product let-expanded="expanded"> <ng-template #body let-product let-expanded="expanded">
<tr> <tr>
<td> <td>
<button type="button" pButton pRipple [pRowToggler]="product" class="p-button-text p-button-rounded p-button-plain" [icon]="expanded ? 'pi pi-chevron-down' : 'pi pi-chevron-right'"></button> <p-button type="button" pRipple [pRowToggler]="product" [text]="true" [rounded]="true" [plain]="true" [icon]="expanded ? 'pi pi-chevron-down' : 'pi pi-chevron-right'" />
</td> </td>
<td style="min-width: 12rem;">{{ product.name }}</td> <td>{{ product.name }}</td>
<td><img [src]="'https://primefaces.org/cdn/primeng/images/demo/product/' + product.image" [alt]="product.name" width="50" class="shadow-lg" /></td> <td>
<td style="min-width: 8rem;">{{ product.price | currency: 'USD' }}</td> <img [src]="'https://primefaces.org/cdn/primeng/images/demo/product/' + product.image" [alt]="product.name" width="50" class="shadow-lg" />
<td style="min-width: 10rem;">{{ product.category }}</td> </td>
<td style="min-width: 10rem;"> <td>{{ product.price | currency: 'USD' }}</td>
<p-rating [ngModel]="product.rating" [readonly]="true"></p-rating> <td>{{ product.category }}</td>
<td>
<p-rating [ngModel]="product.rating" [readonly]="true" />
</td> </td>
<td> <td>
<p-tag [value]="product.inventoryStatus.toLowerCase()" [severity]="getSeverity(product.inventoryStatus)" styleClass="dark:bg-surface-900!" /> <p-tag [value]="product.inventoryStatus" [severity]="getSeverity(product.inventoryStatus)" />
</td> </td>
</tr> </tr>
</ng-template> </ng-template>
<ng-template #rowexpansion let-product> <ng-template #expandedrow let-product>
<tr> <tr>
<td colspan="7"> <td colspan="7">
<div class="p-3"> <div class="p-4">
<p-table [value]="product.orders" dataKey="id" responsiveLayout="scroll"> <h5>Orders for {{ product.name }}</h5>
<p-table [value]="product.orders" dataKey="id">
<ng-template #header> <ng-template #header>
<tr> <tr>
<th pSortableColumn="id"> <th pSortableColumn="id">Id <p-sortIcon field="price" /></th>
Id
<p-sortIcon field="price"></p-sortIcon>
</th>
<th pSortableColumn="customer"> <th pSortableColumn="customer">
Customer Customer
<p-sortIcon field="customer"></p-sortIcon> <p-sortIcon field="customer" />
</th>
<th pSortableColumn="date">
Date
<p-sortIcon field="date"></p-sortIcon>
</th> </th>
<th pSortableColumn="date">Date <p-sortIcon field="date" /></th>
<th pSortableColumn="amount"> <th pSortableColumn="amount">
Amount Amount
<p-sortIcon field="amount"></p-sortIcon> <p-sortIcon field="amount" />
</th> </th>
<th pSortableColumn="stats"> <th pSortableColumn="status">
Status Status
<p-sortIcon field="status"></p-sortIcon> <p-sortIcon field="status" />
</th> </th>
<th style="width: 4rem"></th> <th style="width: 4rem"></th>
</tr> </tr>
@@ -321,12 +304,14 @@ interface expandedRows {
<td>{{ order.id }}</td> <td>{{ order.id }}</td>
<td>{{ order.customer }}</td> <td>{{ order.customer }}</td>
<td>{{ order.date }}</td> <td>{{ order.date }}</td>
<td>{{ order.amount | currency: 'USD' }}</td>
<td> <td>
<span [class]="'order-badge order-' + order.status.toLowerCase()">{{ order.status }}</span> {{ order.amount | currency: 'USD' }}
</td> </td>
<td> <td>
<p-button type="button" icon="pi pi-search"></p-button> <p-tag [value]="order.status" [severity]="getSeverity(order.status)" />
</td>
<td>
<p-button type="button" icon="pi pi-search" />
</td> </td>
</tr> </tr>
</ng-template> </ng-template>
@@ -504,12 +489,26 @@ export class TableDemo implements OnInit {
} }
expandAll() { expandAll() {
if (!this.isExpanded) { if(ObjectUtils.isEmpty(this.expandedRows)) {
this.products.forEach((product) => (product && product.name ? (this.expandedRows[product.name] = true) : '')); this.expandedRows = this.products.reduce(
} else { (acc, p) => {
this.expandedRows = {}; if (p.id) {
acc[p.id] = true;
} }
this.isExpanded = !this.isExpanded; return acc;
},
{} as { [key: string]: boolean }
);
this.isExpanded = true;
} else {
this.collapseAll()
}
}
collapseAll() {
this.expandedRows = {};
this.isExpanded = false;
} }
formatCurrency(value: number) { formatCurrency(value: number) {