Creating a vertical stepper to display order statuses in your Angular component can be done easily using Angular Material, which provides a <mat-vertical-stepper> component. Below are the steps to implement a vertical stepper in your OrderView component.
Step 1: Set Up Angular Material
If you haven't set up Angular Material in your project yet, you can do so with the following command:
ng add @angular/material
Follow the prompts to set up your Angular Material configuration.
Step 2: Import Necessary Material Modules
In your Angular module (app.module.ts or a specific feature module), import the required Angular Material modules:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatStepperModule } from '@angular/material/stepper';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { AppComponent } from './app.component';
import { OrderViewComponent } from './order-view/order-view.component';
@NgModule({
declarations: [
AppComponent,
OrderViewComponent,
],
imports: [
BrowserModule,
BrowserAnimationsModule,
MatStepperModule,
MatButtonModule,
MatIconModule
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
Step 3: Create Your Order Status Data
In your order-view.component.ts, define the order statuses that you will display in the stepper:
import { Component } from '@angular/core';
@Component({
selector: 'app-order-view',
templateUrl: './order-view.component.html',
styleUrls: ['./order-view.component.css']
})
export class OrderViewComponent {
orderStatuses = [
{ label: 'Order Placed', completed: true },
{ label: 'Processing', completed: false },
{ label: 'Shipped', completed: false },
{ label: 'Delivered', completed: false },
{ label: 'Completed', completed: false },
];
}
Step 4: Implement the Stepper in the Template
Now, in your order-view.component.html, add the Angular Material vertical stepper using the defined order statuses:
<mat-vertical-stepper linear>
<mat-step *ngFor="let status of orderStatuses; let i = index" [completed]="status.completed">
<ng-template matStepLabel>{{ status.label }}</ng-template>
<p>Status: {{ status.completed ? 'Completed' : 'Pending' }}</p>
<div>
<button mat-button (click)="goToNext(i)" *ngIf="!status.completed">Next</button>
</div>
</mat-step>
</mat-vertical-stepper>
Step 5: Handle Next Steps (Optional)
You can also add functionality to move to the next step, although this is usually handled by the completion of each step. You can manage that with a method in your component:
goToNext(index: number) {
if (index < this.orderStatuses.length - 1) {
this.orderStatuses[index + 1].completed = true;
}
}
Step 6: Styling (Optional)
You can add additional CSS in order-view.component.css for more customization.
Conclusion
Now, with the above steps, you should have a vertical stepper that displays the status of your order. You can customize the labels, adding more functionality as necessary. If you want to indicate the actual progress of the order, you can adjust the completed property of each status based on your application's actual data.