To implement a multi-document interface (MDI) in Angular using Material Tabs, where you can search for loans and open each loan's details in a separate tab with a close button, follow these steps:
Step 1: Set Up the Angular Project
Install Angular CLI if not already installed:
bash Copy code
npm install -g @angular/cli
Create a new Angular project:
bash Copy code
ng new loan-management
cd loan-management
Add Angular Material to the project:
bash Copy code
ng add @angular/material
Step 2: Create Required Components
Generate components for the search bar, tabs, and loan details:
bash Copy code
ng generate component search-bar
ng generate component loan-tabs
ng generate component loan-details
Step 3: Define the Data Model
Create a Loan model to represent the loan data:
typescript Copy code
// src/app/models/loan.model.ts
export interface Loan {
loanNumber: string;
borrowerName: string;
amount: number;
status: string;
}
Step 4: Implement the Search Bar
In the search-bar component, create a search input field and emit the selected loan when a user selects a result.
HTML Template (search-bar.component.html)
html Copy code
<div class="search-container">
<mat-form-field appearance="outline" class="search-field">
<input matInput placeholder="Search Loans" [(ngModel)]="searchQuery" (input)="onSearch()" />
</mat-form-field>
<mat-list *ngIf="filteredLoans.length > 0">
<mat-list-item
*ngFor="let loan of filteredLoans"
(click)="selectLoan(loan)"
class="loan-item"
>
{{ loan.loanNumber }} - {{ loan.borrowerName }}
</mat-list-item>
</mat-list>
</div>
TypeScript Logic (search-bar.component.ts)
typescript Copy code
import { Component, EventEmitter, Output } from '@angular/core';
import { Loan } from '../models/loan.model';
@Component({
selector: 'app-search-bar',
templateUrl: './search-bar.component.html',
styleUrls: ['./search-bar.component.css'],
})
export class SearchBarComponent {
@Output() loanSelected = new EventEmitter<Loan>();
searchQuery: string = '';
loans: Loan[] = [
{ loanNumber: 'L123', borrowerName: 'John Doe', amount: 5000, status: 'Active' },
{ loanNumber: 'L456', borrowerName: 'Jane Smith', amount: 7000, status: 'Pending' },
{ loanNumber: 'L789', borrowerName: 'Alice Johnson', amount: 10000, status: 'Closed' },
];
filteredLoans: Loan[] = [];
onSearch() {
this.filteredLoans = this.loans.filter((loan) =>
loan.loanNumber.toLowerCase().includes(this.searchQuery.toLowerCase()) ||
loan.borrowerName.toLowerCase().includes(this.searchQuery.toLowerCase())
);
}
selectLoan(loan: Loan) {
this.loanSelected.emit(loan);
this.searchQuery = '';
this.filteredLoans = [];
}
}
Step 5: Implement the Tabbed Interface
In the loan-tabs component, use Angular Material Tabs to dynamically manage tabs for each loan.
HTML Template (loan-tabs.component.html)
html Copy code
<mat-tab-group [selectedIndex]="activeTabIndex" (selectedIndexChange)="onTabChange($event)">
<mat-tab *ngFor="let tab of tabs; let i = index" [label]="tab.label">
<ng-template mat-tab-label>
{{ tab.label }}
<button mat-icon-button (click)="closeTab(i)">
<mat-icon>close</mat-icon>
</button>
</ng-template>
<app-loan-details [loan]="tab.loan"></app-loan-details>
</mat-tab>
</mat-tab-group>
TypeScript Logic (loan-tabs.component.ts)
typescript Copy code
import { Component } from '@angular/core';
import { Loan } from '../models/loan.model';
interface Tab {
label: string;
loan: Loan;
}
@Component({
selector: 'app-loan-tabs',
templateUrl: './loan-tabs.component.html',
styleUrls: ['./loan-tabs.component.css'],
})
export class LoanTabsComponent {
tabs: Tab[] = [];
activeTabIndex: number = 0;
openTab(loan: Loan) {
const existingTab = this.tabs.find((tab) => tab.loan.loanNumber === loan.loanNumber);
if (!existingTab) {
this.tabs.push({ label: `Loan ${loan.loanNumber}`, loan });
this.activeTabIndex = this.tabs.length - 1;
} else {
this.activeTabIndex = this.tabs.indexOf(existingTab);
}
}
closeTab(index: number) {
this.tabs.splice(index, 1);
if (this.activeTabIndex >= index) {
this.activeTabIndex = Math.max(0, this.activeTabIndex - 1);
}
}
onTabChange(index: number) {
this.activeTabIndex = index;
}
}
Step 6: Display Loan Details
In the loan-details component, display the details of the selected loan.
HTML Template (loan-details.component.html)
html Copy code
<div class="loan-details">
<h3>Loan Details</h3>
<p><strong>Loan Number:</strong> {{ loan?.loanNumber }}</p>
<p><strong>Borrower Name:</strong> {{ loan?.borrowerName }}</p>
<p><strong>Amount:</strong> ${{ loan?.amount }}</p>
<p><strong>Status:</strong> {{ loan?.status }}</p>
</div>
TypeScript Logic (loan-details.component.ts)
typescript Copy code
import { Component, Input } from '@angular/core';
import { Loan } from '../models/loan.model';
@Component({
selector: 'app-loan-details',
templateUrl: './loan-details.component.html',
styleUrls: ['./loan-details.component.css'],
})
export class LoanDetailsComponent {
@Input() loan: Loan | null = null;
}
Step 7: Integrate Components
In the main app component, integrate the search bar and tabs.
HTML Template (app.component.html)
html Copy code
<div class="app-container">
<app-search-bar (loanSelected)="openLoanTab($event)"></app-search-bar>
<app-loan-tabs></app-loan-tabs>
</div>
TypeScript Logic (app.component.ts)
typescript Copy code
import { Component } from '@angular/core';
import { Loan } from './models/loan.model';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
constructor(private loanTabs: LoanTabsComponent) {}
openLoanTab(loan: Loan) {
this.loanTabs.openTab(loan);
}
}
Step 8: Add Styles
Add styles to make the interface visually appealing:
css Copy code
/* src/styles.css */
.search-container {
margin-bottom: 20px;
}
.loan-item {
cursor: pointer;
}
.loan-item:hover {
background-color: #f0f0f0;
}
.loan-details {
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
Final Notes
Ensure that all necessary Angular Material modules are imported in app.module.ts.
Use FormsModule for two-way binding in the search bar.
Test the application thoroughly to ensure smooth functionality.
This implementation provides a robust multi-document interface with dynamic tabs, search functionality, and close buttons for each tab.