طراحی یک گرید با Angular و ASP.NET Core - قسمت سوم - قالب پذیر ساختن گرید
نویسنده: وحید نصیری
تاریخ: ۱۳۹۶/۰۵/۱۳ ۱۱:۴۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
<tbody>
<tr *ngFor="let item of queryResult.items; let i = index">
<td class="text-center">{{ itemsPerPage * (currentPage - 1) + i + 1 }}</td>
<td class="text-center">{{ item.productId }}</td>
<td class="text-center">{{ item.productName }}</td>
<td class="text-center">{{ item.price | number:'.0' }}</td>
<td class="text-center">
<input id="item-{{ item.productId }}" type="checkbox" [checked]="item.isAvailable"
disabled="disabled" />
</td>
</tr>
</tbody>
</table> <tbody>
<tr *ngFor="let item of queryResult.items; let i = index">
<ng-container [ngTemplateOutlet]="loadTemplate(item)"
[ngOutletContext]="{ $implicit: item, idx: i }"></ng-container>
</tr>
</tbody>
<!--The Html Template for Read-Only Rows-->
<ng-template #readOnlyTemplate let-item let-i="idx">
<td class="text-center">{{ itemsPerPage * (currentPage - 1) + i + 1 }}</td>
<td class="text-center">{{ item.productId }}</td>
<td class="text-center">{{ item.productName }}</td>
<td class="text-center">{{ item.price | number:'.0' }}</td>
<td class="text-center">
<input id="item-{{ item.productId }}" type="checkbox" [checked]="item.isAvailable"
disabled="disabled" />
</td>
<td>
<input type="button" value="Edit" class="btn btn-default btn-xs" (click)="editItem(item)"
/>
</td>
<td>
<input type="button" value="Delete" (click)="deleteItem(item)" class="btn btn-danger btn-xs"
/>
</td>
</ng-template> @ViewChild("readOnlyTemplate") readOnlyTemplate: TemplateRef<any>;
<!--The Html Template for Editable Rows-->
<ng-template #editTemplate let-item let-i="idx">
<td class="text-center">{{ itemsPerPage * (currentPage - 1) + i + 1 }}</td>
<td class="text-center">{{ item.productId }}</td>
<td class="text-center">
<input type="text" [(ngModel)]="selectedItem.productName" class="form-control" />
</td>
<td class="text-center">
<input type="text" [(ngModel)]="selectedItem.price" class="form-control" />
</td>
<td class="text-center">
<input id="item-{{ item.productId }}" type="checkbox" [checked]="item.isAvailable"
[(ngModel)]="selectedItem.isAvailable" />
</td>
<td>
<input type="button" value="Save" (click)="saveItem()" class="btn btn-success btn-xs"
/>
</td>
<td>
<input type="button" value="Cancel" (click)="cancel()" class="btn btn-warning btn-xs"
/>
</td>
</ng-template> @ViewChild("editTemplate") editTemplate: TemplateRef<any>; @ViewChild("readOnlyTemplate") readOnlyTemplate: TemplateRef<any>;
@ViewChild("editTemplate") editTemplate: TemplateRef<any>;
selectedItem: AppProduct;
isNewRecord: boolean; editItem(item: AppProduct) {
this.selectedItem = item;
} loadTemplate(item: AppProduct) {
if (this.selectedItem && this.selectedItem.productId === item.productId) {
return this.editTemplate;
} else {
return this.readOnlyTemplate;
}
} <div class="panel"> <input type="button" value="Add new product" class="btn btn-primary" (click)="addItem()" /> </div>
addItem() {
this.selectedItem = new AppProduct(0, "", 0, false);
this.isNewRecord = true;
this.queryResult.items.push(this.selectedItem);
this.queryResult.totalItems++;
} cancel() {
this.selectedItem = null;
} deleteItem(item: AppProduct) {
this.productsService
.deleteAppProduct(item.productId)
.subscribe((resp: Response) => {
this.getPagedProductsList();
});
} saveItem() {
if (this.isNewRecord) {
this.productsService
.addAppProduct(this.selectedItem)
.subscribe((resp: AppProduct) => {
this.selectedItem.productId = resp.productId;
this.isNewRecord = false;
this.selectedItem = null;
});
} else {
this.productsService
.updateAppProduct(this.selectedItem.productId, this.selectedItem)
.subscribe((resp: AppProduct) => {
this.selectedItem = null;
});
}
}