DEV Community

Hitesh Chauhan
Hitesh Chauhan

Posted on

Building a Feature-Rich Admin Dashboard with Angular and Bootstrap 5

Admin dashboards are essential for managing and monitoring data, analyzing trends, and gaining actionable insights in any application. Combining the power of Angular with the modern, flexible design system of Bootstrap 5 enables developers to create scalable, responsive, and visually appealing admin dashboards efficiently.

In this comprehensive guide, we’ll explore why Angular and Bootstrap 5 are a great match, set up a project, and walk through building a functional admin dashboard.


Why Use Angular with Bootstrap 5?

The Power of Angular

Angular is a robust TypeScript-based framework that excels at creating dynamic single-page applications (SPAs). It offers a suite of tools that make it an ideal choice for admin dashboards:

  • Component-Based Architecture: Reusable and modular components simplify development.
  • Two-Way Data Binding: Ensures synchronization between the UI and logic.
  • Dependency Injection: Provides a streamlined way to manage services and APIs.
  • Routing and Navigation: Simplifies page management for multi-page dashboards.
  • Reactive Programming with RxJS: Enables powerful, asynchronous data streams.

Why Bootstrap 5?

Bootstrap 5 is one of the most popular front-end frameworks, offering a rich set of pre-designed components and utilities. It brings several advantages:

  • Responsive Design: Built with mobile-first principles to ensure layouts adapt seamlessly to all devices.
  • Flexbox and Grid System: Powerful tools for creating complex layouts.
  • Customizable Themes: Easily tweak colors, typography, and spacing to match your branding.
  • Modern Features: Removal of jQuery dependency in Bootstrap 5 streamlines integration with Angular.

Benefits of Combining Angular and Bootstrap 5

  1. Rapid Development: Angular’s CLI and Bootstrap’s pre-designed components speed up the development process.
  2. Consistency: Bootstrap’s design system ensures consistent styling across the application.
  3. Scalability: Angular’s modular approach and Bootstrap’s reusable components make it easy to scale.
  4. Cross-Browser Compatibility: Both Angular and Bootstrap 5 are optimized for modern browsers.

Setting Up the Project

Step 1: Install Angular

Start by creating a new Angular application using the Angular CLI.

npm install -g @angular/cli
ng new admin-dashboard
cd admin-dashboard
Enter fullscreen mode Exit fullscreen mode

Step 2: Add Bootstrap 5

Install Bootstrap 5 via npm:

npm install bootstrap
Enter fullscreen mode Exit fullscreen mode

Next, update the angular.json file to include Bootstrap’s CSS and JavaScript files:

"styles": [
  "node_modules/bootstrap/dist/css/bootstrap.min.css",
  "src/styles.css"
],
"scripts": [
  "node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"
]
Enter fullscreen mode Exit fullscreen mode

Step 3: Verify Installation

Run the application to confirm Bootstrap is successfully integrated:

ng serve
Enter fullscreen mode Exit fullscreen mode

You can test Bootstrap by adding a simple button in src/app/app.component.html:

<button class="btn btn-primary">Test Button</button>
Enter fullscreen mode Exit fullscreen mode

Building the Admin Dashboard

Dashboard Layout

A typical admin dashboard includes:

  1. Sidebar Navigation
  2. Top Navbar
  3. Main Content Area
  4. Footer

We’ll build each component step by step.


Step 1: Create the Sidebar Navigation

The sidebar serves as the primary navigation for the dashboard.

HTML Template (sidebar.component.html):

<nav class="d-flex flex-column vh-100 bg-dark text-white p-3">
  <h3>Admin Panel</h3>
  <ul class="nav flex-column mt-3">
    <li class="nav-item">
      <a class="nav-link text-white" href="#">Dashboard</a>
    </li>
    <li class="nav-item">
      <a class="nav-link text-white" href="#">Users</a>
    </li>
    <li class="nav-item">
      <a class="nav-link text-white" href="#">Settings</a>
    </li>
  </ul>
</nav>
Enter fullscreen mode Exit fullscreen mode

Styling (sidebar.component.css):

nav {
  width: 250px;
  position: fixed;
}
.nav-link:hover {
  background-color: #495057;
  border-radius: 5px;
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Add the Top Navbar

The top navbar provides access to user profile options, notifications, or other quick actions.

HTML Template (navbar.component.html):

<nav class="navbar navbar-expand-lg navbar-light bg-light px-4">
  <a class="navbar-brand" href="#">Admin Dashboard</a>
  <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav ms-auto">
      <li class="nav-item">
        <a class="nav-link" href="#">Profile</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Notifications</a>
      </li>
    </ul>
  </div>
</nav>
Enter fullscreen mode Exit fullscreen mode

Step 3: Main Content Area

The content area is where charts, tables, and other dashboard components are displayed.

HTML Template (dashboard.component.html):

<div class="container-fluid">
  <div class="row">
    <!-- Sidebar -->
    <div class="col-md-3">
      <app-sidebar></app-sidebar>
    </div>
    <!-- Main Content -->
    <div class="col-md-9 ms-sm-auto">
      <app-navbar></app-navbar>
      <div class="p-4">
        <h1>Welcome to the Admin Dashboard</h1>
        <div class="row">
          <!-- Card Example -->
          <div class="col-md-4">
            <div class="card text-white bg-primary mb-3">
              <div class="card-body">
                <h5 class="card-title">Users</h5>
                <p class="card-text">Manage and monitor users.</p>
              </div>
            </div>
          </div>
          <div class="col-md-4">
            <div class="card text-white bg-success mb-3">
              <div class="card-body">
                <h5 class="card-title">Sales</h5>
                <p class="card-text">Track sales performance.</p>
              </div>
            </div>
          </div>
          <div class="col-md-4">
            <div class="card text-white bg-warning mb-3">
              <div class="card-body">
                <h5 class="card-title">Reports</h5>
                <p class="card-text">Generate detailed reports.</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

Step 4: Footer

Add a footer to display information such as copyright or version details.

HTML Template (footer.component.html):

<footer class="bg-light text-center py-3">
  <p>© 2024 Admin Dashboard. All Rights Reserved.</p>
</footer>
Enter fullscreen mode Exit fullscreen mode

Adding Interactive Components

1. Charts

Integrate charts using popular libraries like Chart.js or ngx-charts.

Install Chart.js:

npm install chart.js
Enter fullscreen mode Exit fullscreen mode

Add a Sample Chart:

In the dashboard component, add:

<canvas id="myChart" width="400" height="200"></canvas>
Enter fullscreen mode Exit fullscreen mode

In the component’s TypeScript file (dashboard.component.ts):

import { Component, OnInit } from '@angular/core';
import { Chart } from 'chart.js';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {
  ngOnInit() {
    new Chart("myChart", {
      type: 'bar',
      data: {
        labels: ['January', 'February', 'March'],
        datasets: [{
          label: '# of Sales',
          data: [10, 20, 30],
          backgroundColor: ['#007bff', '#28a745', '#ffc107']
        }]
      }
    });
  }
}
Enter fullscreen mode Exit fullscreen mode

2. Data Tables

Bootstrap 5’s table classes can be used to display data effectively.

<table class="table table-striped">
  <thead>
    <tr>
      <th>#</th>
      <th>Name</th>
      <th>Email</th>
      <th>Status</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>John Doe</td>
      <td>johndoe@example.com</td>
      <td>Active</td>
    </tr>
    <tr>
      <td>2</td>
      <td>Jane Smith</td>
      <td>janesmith@example.com</td>
      <td>Inactive</td>
    </tr>
  </tbody>
</table>
Enter fullscreen mode Exit fullscreen mode

Conclusion

By combining Angular’s powerful framework with Bootstrap 5’s modern design system, you can create a feature-rich and responsive admin dashboard. Angular’s modular architecture ensures scalability, while Bootstrap’s reusable components simplify the styling process. From navigation and charts to data tables and responsive layouts, this setup is ideal for building professional dashboards for any application.

Start

Top comments (0)