create a multi document interface using material tabs in angular where a search is…
- User
create a multi document interface using material tabs in angular where a search is present to search for loan and then clicking on result it opens a tab with close button and switching of tabs switches to the loan using the loan number and display its related forms
Assistant
I'll provide a comprehensive solution for creating a multi-document interface with Material tabs in Angular, including search functionality and dynamic tab management. I'll break this down into multiple components and services.
- First, let's create the necessary components and services:
// loan.service.ts import { Injectable } from '@angular/core'; import { BehaviorSubject, Observable } from 'rxjs'; export interface Loan { loanNumber: string; customerName: string; amount: number; status: string; } @Injectable({ providedIn: 'root' }) export class LoanService { private loans: Loan[] = [ { loanNumber: 'L001', customerName: 'John Doe', amount: 50000, status: 'Active' }, { loanNumber: 'L002', customerName: 'Jane Smith', amount: 75000, status: 'Pending' }, { loanNumber: 'L003', customerName: 'Mike Johnson', amount: 100000, status: 'Approved' } ]; // Search loans by loan number or customer name searchLoans(query: string): Loan[] { return this.loans.filter(loan => loan.loanNumber.toLowerCase().includes(query.toLowerCase()) || loan.customerName.toLowerCase().includes(query.toLowerCase()) ); } // Get loan by loan number getLoanByNumber(loanNumber: string): Loan | undefined { return this.loans.find(loan => loan.loanNumber === loanNumber); } } // loan-search.component.ts import { Component } from '@angular/core'; import { LoanService, Loan } from './loan.service'; @Component({ selector: 'app-loan-search', template: ` <mat-form-field> <input matInput placeholder="Search Loans" [(ngModel)]="searchQuery" (input)="searchLoans()"> </mat-form-field> <mat-list> <mat-list-item *ngFor="let loan of searchResults" (click)="openLoanTab(loan)"> {{ loan.loanNumber }} - {{ loan.customerName }} </mat-list-item> </mat-list> ` }) export class LoanSearchComponent { searchQuery: string = ''; searchResults: Loan[] = []; constructor( private loanService: LoanService, private tabService: TabService ) {} searchLoans() { if (this.searchQuery.length > 0) { this.searchResults = this.loanService.searchLoans(this.searchQuery); } else { this.searchResults = []; } } openLoanTab(loan: Loan) { this.tabService.addTab(loan); } } // tab.service.ts import { Injectable } from '@angular/core'; import { BehaviorSubject } from 'rxjs'; import { Loan } from './loan.service'; export interface TabItem { loanNumber: string; label: string; } @Injectable({ providedIn: 'root' }) export class TabService { private _tabs = new BehaviorSubject<TabItem[]>([]); tabs$ = this._tabs.asObservable(); private _activeTabIndex = new BehaviorSubject<number>(0); activeTabIndex$ = this._activeTabIndex.asObservable(); addTab(loan: Loan) { const currentTabs = this._tabs.value; // Check if tab already exists const existingTabIndex = currentTabs.findIndex( tab => tab.loanNumber === loan.loanNumber ); if (existingTabIndex === -1) { const newTabs = [ ...currentTabs, { loanNumber: loan.loanNumber, label: `Loan ${loan.loanNumber}` } ]; this._tabs.next(newTabs); this._activeTabIndex.next(newTabs.length - 1); } else { // If tab exists, switch to that tab this._activeTabIndex.next(existingTabIndex); } } removeTab(index: number) { const currentTabs = this._tabs.value; const newTabs = currentTabs.filter((_, i) => i !== index); this._tabs.next(newTabs); // Set active tab to the last tab or 0 if no tabs this._activeTabIndex.next( newTabs.length > 0 ? newTabs.length - 1 : 0 ); } setActiveTab(index: number) { this._activeTabIndex.next(index); } } // loan-detail.component.ts import { Component, Input, OnInit } from '@angular/core'; import { LoanService, Loan } from './loan.service'; @Component({ selector: 'app-loan-detail', template: ` <div *ngIf="loan"> <h2>Loan Details</h2> <p>Loan Number: {{ loan.loanNumber }}</p> <p>Customer Name: {{ loan.customerName }}</p> <p>Amount: {{ loan.amount | currency }}</p> <p>Status: {{ loan.status }}</p> <!-- Add more loan-related forms/details here --> </div> ` }) export class LoanDetailComponent implements OnInit { @Input() loanNumber!: string; loan: Loan | undefined; constructor(private loanService: LoanService) {} ngOnInit() { this.loan = this.loanService.getLoanByNumber(this.loanNumber); } } // main container component @Component({ selector: 'app-loan-workspace', template: ` <div class="loan-workspace"> <div class="search-panel"> <app-loan-search></app-loan-search> </div> <div class="tab-panel"> <mat-tab-group [(selectedIndex)]="activeTabIndex" (selectedIndexChange)="onTabChange($event)"> <mat-tab *ngFor="let tab of tabs; let i = index" [label]="tab.label"> <ng-template matTabContent> <app-loan-detail [loanNumber]="tab.loanNumber"> </app-loan-detail> <button mat-icon-button (click)="closeTab(i)"> <mat-icon>close</mat-icon> </button> </ng-template> </mat-tab> </mat-tab-group> </div> </div> ` }) export class LoanWorkspaceComponent implements OnInit { tabs: TabItem[] = []; activeTabIndex = 0; constructor( private tabService: TabService ) {} ngOnInit() { this.tabService.tabs$.subscribe(tabs => { this.tabs = tabs; }); this.tabService.activeTabIndex$.subscribe(index => { this.activeTabIndex = index; }); } onTabChange(index: number) { this.tabService.setActiveTab(index); } closeTab(index: number) { this.tabService.removeTab(index); } }- Module Configuration:
@NgModule({ imports: [ MatTabsModule, MatInputModule, MatListModule, MatButtonModule, MatIconModule ], declarations: [ LoanWorkspaceComponent, LoanSearchComponent, LoanDetailComponent ] }) export class LoanModule {}Key Features:
- Multi-document interface with Material tabs
- Dynamic tab creation based on loan search
- Prevent duplicate tabs
- Tab switching and closing functionality
- Centralized tab and loan management through services
- Loan search with filtering
To use this, you would place the
<app-loan-workspace>in your main application component.Recommendations for production:
- Add proper error handling
- Implement actual backend service calls
- Add more robust type checking
- Consider adding loading states
- Implement more comprehensive form validation
Would you like me to elaborate on any specific part of the implementation?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.