- ng new multiSelectCheckbox
Step 2
Open the newly created project in visual studio code and install bootstrap in this project.
- npm install bootstrap --save
Now, open styles.css file and add Bootstrap file reference. To add reference in styles.css file add this line.
- @import '~bootstrap/dist/css/bootstrap.min.css';
Step 3 Now, let's create a new component, by using following command.
Now create a new class, to create class use the following command.
- ng generate class blankRow
Now, open the class and add the following code.
- export class BlankRow {
- RollNo:number;
- Name:string;
- Medium:any;
- Class:any;
- Section:any;
- Books:any=[];
- SelectedBooks:any=[];
- Subject=[];
- SelectedSubject=[];
- }
Step 5
Now, open home component.ts file and add the following code in this file.
- import {
- Component,
- OnInit
- } from '@angular/core';
- import {
- BlankRow
- } from '../models/blankRow';
- @Component({
- selector: 'app-home',
- templateUrl: './home.component.html',
- styleUrls: ['./home.component.css']
- })
- export class HomeComponent implements OnInit {
- blankRowArray: Array < BlankRow > = [];
- blankRowData = new BlankRow();
- hideMultiSelectDropdownAll: boolean[] = [];
- hideMultiSelectDropdown: boolean[] = [];
- hideMultiSelectedSubjectDropdown: boolean[] = [];
- hideMultiSelectedSubjectDropdownAll: boolean[] = [];
- tempData = [];
- savedSubjects = [];
- Books = [];
- Subject = [];
- constructor() {}
- ngOnInit() {
- this.Subject = [{
- value: "English",
- IsChecked: false
- }, {
- value: "History",
- IsChecked: false
- }, {
- value: "Geography",
- IsChecked: false
- }, {
- value: "Hindi",
- IsChecked: false
- }, {
- value: "Marathi",
- IsChecked: false
- }, {
- value: "Civics",
- IsChecked: false
- }, {
- value: "Science",
- IsChecked: false
- }, {
- value: "Mathematics",
- IsChecked: false
- }];
- this.Books = [{
- value: "CBSE Class 10 English Literature Reader Book",
- IsChecked: false
- }, {
- value: "CBSE Class 10 English Book",
- IsChecked: false
- }, {
- value: "CBSE Class 10th Maths Book",
- IsChecked: false
- }, {
- value: "CBSE Class 10th Hindi Book",
- IsChecked: false
- }, {
- value: "CBSE Class 10 Science Book",
- IsChecked: false
- }, {
- value: "Class 10 CBSE Geography Book",
- IsChecked: false
- }, {
- value: "Class 10th Economics Book",
- IsChecked: false
- }, {
- value: "CBSE Class 10 Sanskrit Book",
- IsChecked: false
- }];
- }
- addBlankRow() {
- const blankRowData = new BlankRow();
- blankRowData.RollNo = 0,
- blankRowData.Name = '',
- blankRowData.Medium = 0,
- blankRowData.Class = 0,
- blankRowData.Section = 0,
- blankRowData.Books = [],
- blankRowData.Subject = [],
- blankRowData.SelectedSubject = [{
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }],
- blankRowData.SelectedBooks = [{
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }],
- this.blankRowArray.push(blankRowData)
- }
- openMultiSelectDD(i) {
- for (var x = 0; x < this.blankRowArray.length; x++) {
- this.hideMultiSelectDropdownAll[x] = false;
- this.hideMultiSelectedSubjectDropdownAll[x] = false;
- this.hideMultiSelectedSubjectDropdown[x] = false;
- }
- this.hideMultiSelectDropdownAll[i] = true;
- this.hideMultiSelectDropdown[i] = !this.hideMultiSelectDropdown[i];
- }
- openMultiSelectDDForSubject(i) {
- for (var x = 0; x < this.blankRowArray.length; x++) {
- this.hideMultiSelectedSubjectDropdownAll[x] = false;
- this.hideMultiSelectDropdownAll[x] = false;
- this.hideMultiSelectDropdown[x] = false;
- }
- this.hideMultiSelectedSubjectDropdownAll[i] = true;
- this.hideMultiSelectedSubjectDropdown[i] = !this.hideMultiSelectedSubjectDropdown[i];
- }
-
- booksChecked(list: any, i, x, isChecked: boolean) {
- let selectedBooks = list.value;
- if (isChecked) {
- this.blankRowArray[i].Books.push(selectedBooks);
- this.blankRowArray[i].SelectedBooks[x].IsChecked = true;
- } else {
- this.blankRowArray[i].Books = this.blankRowArray[i].Books.filter(obj => obj !== selectedBooks);
- this.blankRowArray[i].SelectedBooks[x].IsChecked = false;
- }
- }
-
- onSubjectChecked(list: any, i, x, isChecked: boolean) {
- let selectedSubject = list.value;
- if (this.blankRowArray[i].Subject.length < 2) {
- if (isChecked) {
- if (this.blankRowArray[i].Subject.length == 0) {
- this.tempData = [];
- }
- if (this.tempData.length < 2) {
- this.tempData.push(x);
- }
- if (this.tempData.length == 2) {
- let saveSub = this.tempData.join(',');
- this.savedSubjects[i] = saveSub;
- console.log(this.savedSubjects[i]);
- }
- this.blankRowArray[i].Subject.push(selectedSubject);
- this.blankRowArray[i].SelectedSubject[x].IsChecked = true;
- this.Subject[x].IsChecked = true;
- } else {
- this.tempData.filter(obj => obj !== x);
- this.blankRowArray[i].Subject = this.blankRowArray[i].Subject.filter(obj => obj !== selectedSubject)
- this.blankRowArray[i].SelectedSubject[x].IsChecked = false;
- }
- } else {
- if (isChecked) {
- this.blankRowArray[i].Subject[0] = this.blankRowArray[i].Subject[1];
- this.blankRowArray[i].SelectedSubject[x].IsChecked = true;
- let saveSub0 = this.savedSubjects[i].split(',')[0] ? this.savedSubjects[i].split(',')[0] : this.tempData[0];
- let saveSub1 = this.savedSubjects[i].split(',')[1] ? this.savedSubjects[i].split(',')[1] : this.tempData[1];
- var temp = saveSub0;
- this.tempData[0] = saveSub1;
- this.tempData[1] = x;
- this.blankRowArray[i].SelectedSubject[temp].IsChecked = false;
- this.Subject[x].IsChecked = true;
- this.blankRowArray[i].Subject[1] = selectedSubject;
- this.savedSubjects[i] = this.tempData.join(',');
- } else {
- var temp = this.tempData.find(a => a == x);
- this.tempData = [];
- this.tempData[0] = temp;
- this.blankRowArray[i].Subject = this.blankRowArray[i].Subject.filter(obj => obj !== selectedSubject)
- this.blankRowArray[i].SelectedSubject[x].IsChecked = false;
- }
- }
- }
- deleteRow(index) {
- this.blankRowArray.splice(index, 1);
- }
- }
- deleteRow(index) {
- this.blankRowArray.splice(index, 1);
- }
deleteRow method is use to delete created row.
- addBlankRow() {
- const blankRowData = new BlankRow();
- blankRowData.RollNo = 0,
- blankRowData.Name = '',
- blankRowData.Medium = 0,
- blankRowData.Class = 0,
- blankRowData.Section = 0,
- blankRowData.Books = [],
- blankRowData.Subject = [],
- blankRowData.SelectedSubject = [{
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }],
- blankRowData.SelectedBooks = [{
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }, {
- IsChecked: false
- }],
- this.blankRowArray.push(blankRowData)
- }
addBlankRow method is used to add a new blank row on the click of the Add new Row button.
- openMultiSelectDD(i) {
- for (var x = 0; x < this.blankRowArray.length; x++) {
- this.hideMultiSelectDropdownAll[x] = false;
- this.hideMultiSelectedSubjectDropdownAll[x] = false;
- this.hideMultiSelectedSubjectDropdown[x] = false;
- }
- this.hideMultiSelectDropdownAll[i] = true;
- this.hideMultiSelectDropdown[i] = !this.hideMultiSelectDropdown[i];
- }
openMultiSelectDD method is used to open dropdowns (related to books) when it gets clicked. I used "for" loop here, to close all particular dropdowns and open only selected ones.
- openMultiSelectDDForSubject(i) {
- for (var x = 0; x < this.blankRowArray.length; x++) {
- this.hideMultiSelectedSubjectDropdownAll[x] = false;
- this.hideMultiSelectDropdownAll[x] = false;
- this.hideMultiSelectDropdown[x] = false;
- }
- this.hideMultiSelectedSubjectDropdownAll[i] = true;
- this.hideMultiSelectedSubjectDropdown[i] = !this.hideMultiSelectedSubjectDropdown[i];
- }
openMultiSelectDDForSubject method is used to open dropdown (related to subject) when it gets clicked. I used "for" loop here to close all particular dropdowns and opened only the selected one.
-
- booksChecked(list: any, i, x, isChecked: boolean) {
- let selectedBooks = list.value;
- if (isChecked) {
- this.blankRowArray[i].Books.push(selectedBooks);
- this.blankRowArray[i].SelectedBooks[x].IsChecked = true;
- } else {
- this.blankRowArray[i].Books = this.blankRowArray[i].Books.filter(obj => obj !== selectedBooks);
- this.blankRowArray[i].SelectedBooks[x].IsChecked = false;
- }
- }
In the above code, I have used some parameters, which I will explain one by one.
- list :It Stores the selected value from the checkbox.
- i :It stores the current blank row index value.
- x :It stores the current checkbox index value.
- isChecked :It stores the boolean value of the checkbox (for checked 'true' and for unchecked 'false' ).
If Checked, then it will push into an array and assign SelectedBooks.Ischecked as true. It can check or uncheck all checkboxes:
-
- onSubjectChecked(list: any, i, x, isChecked: boolean) {
- let selectedSubject = list.value;
- if (this.blankRowArray[i].Subject.length < 2) {
- if (isChecked) {
- if (this.blankRowArray[i].Subject.length == 0) {
- this.tempData = [];
- }
- if (this.tempData.length < 2) {
- this.tempData.push(x);
- }
- if (this.tempData.length == 2) {
- let saveSub = this.tempData.join(',');
- this.savedSubjects[i] = saveSub;
- console.log(this.savedSubjects[i]);
- }
- this.blankRowArray[i].Subject.push(selectedSubject);
- this.blankRowArray[i].SelectedSubject[x].IsChecked = true;
- this.Subject[x].IsChecked = true;
- } else {
- this.tempData.filter(obj => obj !== x);
- this.blankRowArray[i].Subject = this.blankRowArray[i].Subject.filter(obj => obj !== selectedSubject)
- this.blankRowArray[i].SelectedSubject[x].IsChecked = false;
- }
- } else {
- if (isChecked) {
- this.blankRowArray[i].Subject[0] = this.blankRowArray[i].Subject[1];
- this.blankRowArray[i].SelectedSubject[x].IsChecked = true;
- let saveSub0 = this.savedSubjects[i].split(',')[0] ? this.savedSubjects[i].split(',')[0] : this.tempData[0];
- let saveSub1 = this.savedSubjects[i].split(',')[1] ? this.savedSubjects[i].split(',')[1] : this.tempData[1];
- var temp = saveSub0;
- this.tempData[0] = saveSub1;
- this.tempData[1] = x;
- this.blankRowArray[i].SelectedSubject[temp].IsChecked = false;
- this.Subject[x].IsChecked = true;
- this.blankRowArray[i].Subject[1] = selectedSubject;
- this.savedSubjects[i] = this.tempData.join(',');
- } else {
- var temp = this.tempData.find(a => a == x);
- this.tempData = [];
- this.tempData[0] = temp;
- this.blankRowArray[i].Subject = this.blankRowArray[i].Subject.filter(obj => obj !== selectedSubject)
- this.blankRowArray[i].SelectedSubject[x].IsChecked = false;
- }
- }
- }
In this method, we can only check a maximum of two values. If we select a third value, then the first value replaces with the third and so on.
Step 6
Now, open home.component.html file and add the following code in this file.
Now, open home.component.css file and add the following code in this file.
- .tooltip {
- position: absolute;
- z-index: 3000;
- border: 1px solid #b7b086;
- background-color: white;
- color: #000!important;
- padding: 5px 15px 5px 10px;
- opacity: 1;
- width: 350px;
- }
Step 8
Now, open app.module.ts file and add the following code in this file.
- import { BrowserModule } from '@angular/platform-browser';
- import { NgModule } from '@angular/core';
- import { FormsModule } from '@angular/forms';
- import { AppComponent } from './app.component';
- import { RouterModule } from '@angular/router';
- import { AppRoutingModule } from './app-routing.module';
- import { HomeComponent } from './home/home.component';
- @NgModule({
- declarations: [
- AppComponent,
- HomeComponent,
- ],
- imports: [
- BrowserModule,
- RouterModule,
- FormsModule,
- AppRoutingModule
- ],
- providers: [
- ],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
Step 9
Now, run the project, by using 'npm start' or 'ng serve' command.
Click on Add blank row button and it creates a new row.
Step 10
Now, click on the subject textbox.
Step 11
Summary
In this article, I discussed how we can create dynamic rows and custom multiselect dropdowns in Angular 8 applications.
Please give your valuable feedback/comments/questions about this article. Please let me know if you liked and understood this article and how I could improve upon it.