Categories

Tuesday, July 23, 2024
#919814419350 therichposts@gmail.com
AngularAngular 16Bootstrap 5

Implementing Search Functionality with Data Binding in Angular 16

Implementing Search Functionality with Data Binding in Angular 16

Hello friends, welcome back to my blog. Today this blog post will tell you, Implementing Search Functionality with Data Binding in Angular 16.

Guys in this demo we will implement custom search functionality to our ecommerce site shop page. After this demo I will do advance search functionality.


Working Demo

Implementing Search Functionality with Data Binding in Angular 16
Implementing Search Functionality with Data Binding in Angular 16

Angular 16 and Bootstrap 5 came and if you are new then you must check below two links:

  1. Angular16 Basic Tutorials
  2. Bootstrap 5

Friends now I proceed onwards and here is the working code snippet and please use carefully this to avoid the mistakes:

1. Firstly friends we need fresh angular 16 setup and for this we need to run below commands but if you already have angular 16 setup then you can avoid below commands. Secondly we should also have latest node version installed on our system:

npm install -g @angular/cli 

ng new angulardemo//Create new Angular Project

cd angulardemo // Go inside the Angular Project Folder

2. Now friends we need to run below commands into our project terminal to  Bootstrap 5  modules into our angular application:

npm i bootstrap 

npm i @popperjs/core

3. Now friends we just need to add below code into angulardemo/src/app/app.component.html file to get final out on the web browser:

<div class="container p-5">
    <div class="p-1 mb-2 bg-primary text-white text-center fs-3">Therichpost</div>
    <div class="input-group">
      <span class="input-group-text bg-primary text-white">Search Product</span>
      <input type="text" class="form-control" [(ngModel)]="searchText">
    </div>
    
      <div class="mt-2" *ngFor="let product of products">
        <div class="product-container" *ngIf="searchText==='' || product.product_title.toLowerCase().includes(searchText)">
          <div class="card mb-2 me-4">
            <img [src]="product.product_image" class="card-img-top" alt="...">
            <div class="card-body">
              <p class="card-text text-center">{{ product.product_title }}</p>
              <p class="card-text text-center">{{ product.product_price }}</p>
            </div>
          </div>
        </div>
       
     
    </div>
  </div>

4. Now friends we just need to add below code into angulardemo/angular.json file:

...

 "styles": [
              "src/styles.css",
              "node_modules/bootstrap/dist/css/bootstrap.min.css"
            ],
            "scripts": [
              "node_modules/bootstrap/dist/js/bootstrap.min.js"
            ]

...

5. Now friends we just need to add below code into angulardemo/src/app/app.component.ts file:

import { Component } from '@angular/core';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  products = [
    {
        "id": "8",
        "product_title": "HeadPhone",
        "product_price": "$178.00",
        "product_image": "https://therichpost.com/testjsonapi/wp-content/themes/testjsonapi/assets/images/items/8.jpg",
        "product_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
        "created_at": "2021-02-01 12:42:08",
        "updated_at": "2021-02-01 12:42:08"
    },
    {
        "id": "7",
        "product_title": "Digital Watch",
        "product_price": "$56.00",
        "product_image": "https://therichpost.com/testjsonapi/wp-content/themes/testjsonapi/assets/images/items/7.jpg",
        "product_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
        "created_at": "2021-02-01 12:42:08",
        "updated_at": "2021-02-01 12:42:08"
    },
    {
        "id": "6",
        "product_title": "Sofa",
        "product_price": "$280.00",
        "product_image": "https://therichpost.com/testjsonapi/wp-content/themes/testjsonapi/assets/images/items/6.jpg",
        "product_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
        "created_at": "2021-02-01 12:41:08",
        "updated_at": "2021-02-01 12:41:08"
    },
    {
        "id": "5",
        "product_title": "Apple Laptop",
        "product_price": "$179.00",
        "product_image": "https://therichpost.com/testjsonapi/wp-content/themes/testjsonapi/assets/images/items/5.jpg",
        "product_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
        "created_at": "2021-02-01 12:41:08",
        "updated_at": "2021-02-01 12:41:08"
    },
    {
        "id": "4",
        "product_title": "Bag",
        "product_price": "$179.00",
        "product_image": "https://therichpost.com/testjsonapi/wp-content/themes/testjsonapi/assets/images/items/4.jpg",
        "product_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
        "created_at": "2021-02-01 12:39:54",
        "updated_at": "2021-02-01 12:39:54"
    },
    {
        "id": "3",
        "product_title": "Jean Shorts",
        "product_price": "$56.00",
        "product_image": "https://therichpost.com/testjsonapi/wp-content/themes/testjsonapi/assets/images/items/3.jpg",
        "product_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
        "created_at": "2021-02-01 12:39:54",
        "updated_at": "2021-02-01 12:39:54"
    },
    {
        "id": "2",
        "product_title": "Winter Jacket",
        "product_price": "$280.00",
        "product_image": "https://therichpost.com/testjsonapi/wp-content/themes/testjsonapi/assets/images/items/2.jpg",
        "product_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
        "created_at": "2021-02-01 12:38:47",
        "updated_at": "2021-02-01 12:38:47"
    },
    {
        "id": "1",
        "product_title": "T-shirt",
        "product_price": "$179.00",
        "product_image": "https://therichpost.com/testjsonapi/wp-content/themes/testjsonapi/assets/images/items/1.jpg",
        "product_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
        "created_at": "2021-02-01 12:38:47",
        "updated_at": "2021-02-01 12:38:47"
    }
];
  searchText:string = "";
}

Friends in the end must run ng serve command into your terminal to run the angular 16 project.

Now we are done friends. If you have any kind of query, suggestion and new requirement then feel free to comment below.

Note: Friends, In this post, I just tell the basic setup and things, you can change the code according to your requirements.

I will appreciate that if you will tell your views for this post. Nothing matters if your views will be good or bad because with your views, I will make my next posts more good and helpful.

Jassa

Thanks

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.