Skip to content
  • iImagine
  • Register
  • Log In

Web Development School

Learning made easy.

  • Books
    • Beginning Web Development with ASP.Net Core & Client-Side Technologies
      • TOC
      • Part 1
        • Chapter 1: Static HTML – Designing the landing page
      • Part 2
        • Chapter 2: ASP.Net Core – Let’s talk Dynamic
        • Chapter 3: Introduction to ASP.Net Core MVC
          [ASP.Net Core v9]
      • Part 4
        • Chapter 7: Using Server Side & Client Side technologies together
          [ASP.Net Core v7 & Angular 15]
  • Environment Setup
    • Installing Angular
    • Installing Visual Studio 2022
    • Installing SQL Server 2022 Express
    • Installing Postman
    • Installing Git for Windows
  • Blog
  • iImagine WebSolutions
  • Events
  • Learning Videos
  • Toggle search form

MatSidenav: Create a Search Sidebar

Now that we are able to page and sort our vehicle data (our mocked vehicle data anyway for now), let’s set up a sidebar where we can add some UI to search vehicles by category. And we will add in other search features as we go. Since we have been making pretty heavy use of Angular Material in this chapter, you might be asking yourself if there is an Angular Material component we can use for a Search Sidebar. As a matter of fact there is. We will be using the AM Sidenav

Note: The pathway I am using for development in this module is:
C:\Development\FredsCars\FullStack\Module19.
Table Of Contents
  1. Import the MatSidenavModule
  2. Update the Vehicles component HTML
    • Using MatSideNav
    • Tweak MatSidenav with CSS
  3. What's Next

Import the MatSidenavModule

As you probably guessed by now, the first thing we need to do is import the required AM modules. Open angular-material.module.ts and make the following modifications.

FredsCars/src/app/angular-material.module.ts

import { NgModule } from '@angular/core';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatIconModule } from '@angular/material/icon';
import { MatButtonModule } from '@angular/material/button';
import { MatTableModule } from '@angular/material/table';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { MatPaginatorModule } from '@angular/material/paginator';
import { MatSortModule } from '@angular/material/sort';
import { MatSidenavModule } from '@angular/material/sidenav';

const angularMatModules = [
  MatToolbarModule,
  MatIconModule,
  MatButtonModule,
  MatTableModule,
  MatProgressSpinnerModule,
  MatPaginatorModule,
  MatSortModule,
  MatSidenavModule

]

@NgModule({
  imports: [
    angularMatModules
  ],
  exports: [
    angularMatModules
  ]
})
export class AngularMaterialModule { }

In the code above we imported the single AM module needed for SideNav; MatSidenavModule.

Update the Vehicles component HTML

Open the vehicles.component.html file and modify the code with the contents below.

FredsCars/src/app/vehicles/vehicles.component.html

<mat-sidenav-container>
  <mat-sidenav mode="side" #sidenav>
    <p><b>Search Panel</b></p>
  </mat-sidenav>

  <mat-sidenav-content>
    <p *ngIf="!vehicles">
      <mat-spinner style="margin: 0 auto;"></mat-spinner>
    </p>

    <div>
      <button mat-icon-button
              color="primary"
              (click)="sidenav.toggle()">
        <mat-icon>
          search
        </mat-icon>
      </button>
    </div>
    <table mat-table [dataSource]="vehicles"
           *ngIf="vehicles" matSort>

      <ng-container matColumnDef="vehicleType">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>Category</th>
        <td mat-cell *matCellDef="let item">
          <b>{{ item.vehicleType }}</b>
        </td>
      </ng-container>

      <ng-container matColumnDef="id">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>ID</th>
        <td mat-cell *matCellDef="let item">
          {{ item.id }}
        </td>
      </ng-container>

      <ng-container matColumnDef="status">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>Status</th>
        <td mat-cell *matCellDef="let item">
          {{ item.status }}
        </td>
      </ng-container>

      <ng-container matColumnDef="year">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>Year</th>
        <td mat-cell *matCellDef="let item">
          {{ item.year }}
        </td>
      </ng-container>

      <ng-container matColumnDef="make">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>Make</th>
        <td mat-cell *matCellDef="let item">
          {{ item.make }}
        </td>
      </ng-container>

      <ng-container matColumnDef="model">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>Model</th>
        <td mat-cell *matCellDef="let item">
          {{ item.model }}
        </td>
      </ng-container>

      <ng-container matColumnDef="color">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>Color</th>
        <td mat-cell *matCellDef="let item">
          {{ item.color }}
        </td>
      </ng-container>

      <ng-container matColumnDef="price">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>Price</th>
        <td mat-cell *matCellDef="let item">
          {{ item.price | currency:"USD" }}
        </td>
      </ng-container>

      <!-- No VIN on vehicles page.  We will show this on the details page -->
      <!-- Header Template -->
      <tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
      <!-- Row Template -->
      <tr mat-row *matRowDef="let row; columns: columnsToDisplay"></tr>
    </table>

    <mat-paginator [pageSize]="5" [pageSizeOptions]="[3, 5, 10]">
    </mat-paginator>

  </mat-sidenav-content>
</mat-sidenav-container>

If you run the application and go to the Vehicles page you should see something similar to the following screenshot.

As you can see in the above screenshot, we have added a search icon. If you click on the search icon a sidebar will slide open on the left hand side of the browser as in the following screenshot.

Pretty spiffy right? Well, let’s take a look at the modifications we made in the html above in vehicles.component.html. It really didn’t take much. We just made basic use of MatSidebar in the AM component library.

Using MatSideNav

To use MatSideNav we need to use three components.

  • <mat-sidenav-container>: acts as a structural container for the content and sidenav.
  • <mat-sidenav-content>: represents the main content.
  • <mat-sidenav>: represents the side content.

And that is what we did in vehicles.component.html above.

First we wrapped the existing content in a <mat-sidenav-content> element nested in a <mat-sidenav-container> element.

<mat-sidenav-container>
    <mat-sidenav-content>
        <!-- spinner -->
        <!-- vehicles table -->
    <mat-sidenav-content>
</mat-sidenav-container>

Then we add a <mat-sidenav> element inside the <mat-sidenav-container> above <mat-sidenav-content> which will hold our sidenav search panel. Also notice we give the <mat-sidenav> element a variable name using #sidenav. This way we will be able to access the sidebar by the variable name sidenav to toggle it open and closed.

<mat-sidenav-container>
    <mat-sidenav mode="side" #sidenav>
        <p><b>Search Panel</b></p>
    </mat-sidenav>

    <mat-sidenav-content>
        <!-- spinner -->
        <!-- vehicles table -->
    <mat-sidenav-content>
</mat-sidenav-container>

Next, we add a <div> element with a MatIconButton using the search glass icon in a MatIcon element right before the vehicles table inside the MatSidenav content area. We add a click event to the MatIconButton with the expression sidenav.toggle(). This accesses the sidebar through the variable name we gave it and toggles it open and closed using the MatSideNav.toggle() method.

<mat-sidenav-container>
    <mat-sidenav mode="side" #sidenav>
        <p><b>Search Panel</b></p>
    </mat-sidenav>

    <mat-sidenav-content>
        <!-- spinner -->

        <div>
            <button mat-icon-button
                  color="primary"
                  (click)="sidenav.toggle()">
            <mat-icon>
              search
            </mat-icon>
          </button>
        </div>
        <!-- vehicles table -->
    <mat-sidenav-content>
</mat-sidenav-container>

Tweak MatSidenav with CSS

Let’s make one last tweak with CSS before we move on.

When we click the search icon and open the sidenav, the text “Search Panel” is crammed up to the right edge of the search panel as you can see in the screenshot below.

Let’s fix that real quick. Open up vehicles.component.scss and make the modicication below in bold blue font.

FredsCars/src/app/vehicles/vehicles.component.scss

/*** existing content ***/
mat-sidenav {
  padding-right:10px;
}

Now there should be a little more space between the search panel content and the main content.

What’s Next

In this module we learned how to use the Angular Material Sidenav component and set up a sidebar area for a search panel we can toggle open and closed. In the next module we will add in some UI to select vehicle categories to search by using MatCheckbox.

< Prev
Next >

Leave a ReplyCancel reply

Chapter 1: Static HTML – Designing the landing page.

  • Static HTML – Designing the landing page.
  • Let’s get started!
  • Mock your site with HTML
  • Make CSS easy with Bootstrap
  • Mock your content
  • Introducing JavaScript
  • JavaScript Code Improvements
  • Results Data
  • Images and the HTML Image Element.
  • Revisiting Reusability for CSS and JavaScript
  • Reuse for HTML: PART 1
  • Reuse for HTML: PART 2
  • Details Page – Using a Bootstrap Component
  • Creating Links
  • Chapter One Conclusion

Chapter 2: ASP.Net Core – Let’s talk Dynamic

  • Introduction to ASP.Net Core
  • What is .Net?
  • What is ASP.Net
  • Introduction to Entity Framework Core

Chapter 3: ASP.Net MVC Core – Models, Views, and Controllers [ASP.Net Core v9]

  • Introduction to ASP.Net Core MVC
  • Create the project: ASP.Net Core MVC
  • Explore the ASP.Net Core Empty Web Project Template
  • Configure the Application for MVC
  • Create a Controller: Home Controller
  • Create a View: Index View for the Home Controller
  • Install Bootstrap using Libman
  • Create the Layout template
  • Create the Model
  • Install EF Core & Create the Database
  • Seed the Database: Loading test data
  • DI (Dependency Injection): Display a List of Vehicles
  • Repository Pattern: The Vehicles Repo
  • Unit Test 1: Home Controller Can Use Vehicle Repository
  • Unit Test 2: Vehicle Repository Can Return List
  • Add the ImagePath Migration and Thumbnail images to results
  • Pagination: Create a Custom Tag Helper
  • Sorting
  • Category Filter
  • Partial View: Break out the vehicle results
  • View Component: Create dynamic category buttons
  • Create the Details page
  • Create the Create Page

Chapter 7: Using Server Side & Client Side technologies together. [ASP.Net Core v7 & Angular v15]

  • Intro to Full Stack Development
  • Fred’s Cars – Full Stack Development
  • Prepare the environment
  • Create the Visual Studio Solution
  • Add the ASP.Net Core Web API project
  • Add the Angular Project
  • Wire it up!
  • WeatherForecast: Understanding the basics
  • Vehicles API Controller: Mock Data
  • Vehicles Angular Component: Consuming Data
  • Routing and Navigation
  • Using a Component Library: Angular Material
  • Our first Angular Material Component: MatToolbar
  • Configuring for Saas: CSS with superpowers
  • Create the Header & Footer components
  • Displaying Results with MatTable
  • Loading: Using a Progress Spinner
  • MatTable: Client-Side Paging and Sorting
  • MatSidenav: Create a Search Sidebar
  • MatCheckbox: Category Search UI
  • Adding an image to the welcome page
  • Create the database with Entity Framework Core migrations
  • MatPaginator & PageEvent: Custom Server-Side Paging
  • Unit Testing: Custom Server-Side Paging
  • Repository Pattern: VehicleRepository
  • Unit Test: Paging in the Vehicles controller
  • Server-Side Sorting
  • Unit Tests: Sorting
  • Filter (Quick Search)
  • Unit Tests: Filter feature
  • Advanced Search: Categories
  • Unit Tests: Search by category
  • Progress Spinner: Final Fix

TOC

  • What were WebForms?
  • Enter MVC
    • Understanding MVC
    • Advantages of MVC
  • ASP.Net Core MVC – A total rewrite
  • ASP.Net Core 2 MVC – Here come Razor Pages
    • Understanding Razor Pages
  • ASP.Net Core 3 – Dropping the MVC reference
    • Understanding Blazor
  • Dropping the MVC reference
  • Hello .Net 5!
  • What’s Next? – Here comes .Net 6.

Recent Posts

  • Angular Commands Cheat Sheet
  • Installing Git for Windows
  • Installing Postman
  • Installing SQL Server 2022 Express
  • Installing Visual Studio 2022

Recent Comments

No comments to show.

Archives

  • November 2023
  • October 2023
  • June 2023
  • October 2021

Categories

  • Angular
  • ASP.Net
  • Environment Setup
  • See All
  • SQL Server
  • Visual Studio
  • Web API & Rest Services

WordPress Theme Editor

Copyright © 2025 Web Development School.

Powered by PressBook Blog WordPress theme