0% found this document useful (0 votes)
19 views5 pages

Student Management Component in Angular

The document outlines a practical task to design a component for managing student records, allowing users to add or remove students through a user interface. It includes HTML and TypeScript code for a student component that displays a table of students with options to update the number of students and remove individual entries. The document also provides styling details for the component's appearance.

Uploaded by

srushtird1410
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

Student Management Component in Angular

The document outlines a practical task to design a component for managing student records, allowing users to add or remove students through a user interface. It includes HTML and TypeScript code for a student component that displays a table of students with options to update the number of students and remove individual entries. The document also provides styling details for the component's appearance.

Uploaded by

srushtird1410
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PRACTICAL:-6

AIM: Design component to perform following tasks


A) To Add or Remove number of students using textbox and button controls
and display it in tabular structure format.
B) Give row level remove button option to student table and record should be
deleted when click on it.

Code:([Link])
<div class="panel-body">
<div>
<!-- Input for the number of students -->
<input type="number" min="0" [(ngModel)]="studentCount" />
<br /><br />
<button (click)="updateTable()">Update Table</button>
</div>
<br />
<!-- Table to display the students -->
<table border="1" cellpadding="10">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let student of students; let i = index">
<td>
<input
type="text"
[(ngModel)]="[Link]"
name="fname{{ i }}"
placeholder="First Name"
/>
</td>
<td>
<input
type="text"
[(ngModel)]="[Link]"
name="lname{{ i }}"
placeholder="Last Name"
/>
</td>
<td>
<input
type="number"
[(ngModel)]="[Link]"
name="age{{ i }}"
placeholder="Age"
/>
</td>
<td>
<button (click)="removeStudent(i)">Remove</button>
</td>
</tr>
</tbody>
</table>
</div>

Code:( [Link])
import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

@Component({
selector: 'app-student',
standalone: true,
imports: [CommonModule, FormsModule],
templateUrl: './[Link]',
styleUrls: ['./[Link]'],
})
export class StudentComponent {
studentCount: number = 0; // Controls the number of student rows
students: { fname: string; lname: string; age: number }[] = []; // Array of students

// Function to update the table based on the student count


updateTable() {
const currentLength = [Link];

if ([Link] > currentLength) {


// Add new rows
for (let i = currentLength; i < [Link]; i++) {
[Link]({ fname: '', lname: '', age: 0 });
}
} else if ([Link] < currentLength) {
// Remove excess rows
[Link] = [Link](0, [Link]);
}
}

// Function to remove a student from the list


removeStudent(index: number) {
[Link](index, 1); // Remove the student at the specified index
[Link] = [Link]; // Update the student count
}
}

Code:( [Link])
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

import { StudentComponent } from './student-component/[Link]';

@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule, FormsModule, StudentComponent],
templateUrl: './[Link]',
styleUrls: ['./[Link]'],
})
export class AppComponent {
title = 'Practical-6'; // Set a title for your application
}

Code:( [Link])
<div class="container">
<h1>{{ title }}</h1>

<!-- Embedding the StudentComponent here -->


<app-student></app-student>
</div>

Code:( [Link])
table {
width: 100%;
border-collapse: collapse;
}

th,
td {
border: 1px solid #ccc;
padding: 8px;
text-align: left;
}

th {
background-color: #f4f4f4;
}

button {
color: white;
background-color: red;
border: none;
padding: 5px 10px;
cursor: pointer;
}
button:hover {
background-color: darkred;
}

OUTPUT:-

Common questions

Powered by AI

The `updateTable` function adjusts the length of the `students` array to match the `studentCount`. If `studentCount` is greater than the current length of `students`, it adds new student objects with default values to the array. Conversely, if `studentCount` is less, it slices the array to remove excess student objects. This ensures that the number of rows in the table matches `studentCount` .

The component classes manage state and functionality by defining properties such as `studentCount` and `students` directly in the class, using simple methods for updates. While sufficient for this scope, optimization can include implementing state management solutions like NgRx for complex applications, better separating concerns. Additionally, reactive programming with RxJS can optimize UI updates and event handling, enhancing performance and scalability for more dynamic or larger datasets .

The CSS enhances the user interface by ensuring the table is full width and tables and cells have borders for clear separation. It sets padding for text readability, left-aligns text for consistency, and gives the header a distinct background color for clarity. Button styles, including hover effects, improve the user experience by making actions visually distinguishable and interactive .

Binding an input element directly to a model property using `ngModel` can lead to potential data integrity issues if the input data isn't validated before updating the model. Any undesired input, such as incorrect data types or values outside expected ranges, is immediately reflected in the model, potentially causing errors or corrupting data unless handled with additional validation logic. It's crucial to implement input validation and sanitization to protect data integrity .

Separating student management logic into a dedicated `StudentComponent` provides multiple architectural benefits: it encapsulates related functionality and UI, promoting reusability and testability. This separation enhances maintainability by isolating student-specific logic from other application logic, enabling independent development and improvements. It also supports Angular's modular design philosophy, which simplifies integrating and embedding components across the application .

The `AppComponent` incorporates the `StudentComponent` by embedding `<app-student></app-student>` within its HTML template. This method benefits the application structure by modularizing components, making the system more manageable and scalable. It allows the `StudentComponent` to be reused across different parts of the application without redundancy, adhering to DRY principles and enhancing overall maintainability .

The key advantages of using Angular's standalone components include greater flexibility in component management, reduced dependency on module architecture, and simplified application structure by eliminating the need for NgModules. Standalone components can ease integration into different parts of an application as well as into different applications, enhancing modularity, scalability, and maintainability .

The `StudentComponent` dynamically adjusts the `students` array size by adding empty student objects when `studentCount` increases and slicing the array to remove excess entries when it decreases. Performance considerations include the potentially high computational cost of large operations, like re-rendering the table for extensive additions or deletions. Efficient handling and limiting unnecessary updates are crucial to manage these operations' impact on performance .

The deletion of a student record is represented in HTML by a 'Remove' button displayed in each table row. When this button is clicked, it triggers the `removeStudent` function defined in the TypeScript file. This function uses the `splice` method to remove the student object at the specified index from the `students` array, effectively updating the `studentCount` and visually removing the row from the table .

The purpose of using `ngModel` is to implement two-way data binding between the input fields and the component properties. It allows the input values for each student's first name, last name, and age to automatically update the corresponding properties in the `students` array as a user types. This facilitates real-time user interaction with the data model in the Angular component .

You might also like