DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Data
  4. Working With Firebase Firestore and the Ignite UI for Angular Grid

Working With Firebase Firestore and the Ignite UI for Angular Grid

In this article, we take a look at how toi read data from Firestore and bind that data to the Ignite UI for Angular Grid.

Dhananjay Kumar user avatar by
Dhananjay Kumar
·
Sep. 23, 18 · Tutorial
Like (2)
Save
Tweet
Share
7.90K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we’ll learn to use Firebase Firestore and the Ignite UI for Angular Grid. The Firestore is Firebase NoSQL database offering so, before you start, I recommend that you learn more about both Firestore and Ignite UI for Angular.

In this article, we’ll learn to read data from Firestore and bind that data to the Ignite UI for Angular Grid. In the next article on this topic, we'll move through Create, Delete, and Update operations.

So, let’s begin by setting up Firestore, creating an Angular project to work with Ignite UI for Angular, and then fetching data from Firestore.

Setting Up Firestore

You’re going to navigate to the Firebase console. When you’re there, click on the “Add project” option.

After clicking on “Add project,” you will get a dialog window to provide project information. Enter into that to create a project as shown in the image below. Here, I have given the project name iggridemo.

Once the project is created, you need to add an app to the project. We’ll add a web project, as we’re going to use Firestore data collection inside Angular, which is a web project.

As you click on the web option, you’ll get a snippet to add to your project. Copy this snippet, since you may need it later to add into an Angular project.

Note: We will add the below setting in the Angular project's environment.prod.ts file.

Next, click on “Database” in the side menu options and create a collection. Keep in mind that Firestore is a NoSQL-based database. Here, we create collections to work with data. To create a database, go to the side menu and click on 'database,' then create the database. You’ll be asked to select your security rules. Select option, “Start in test mode,” and click on “Enable”.

After setting up security rules, click on “Add collection” to add a collection to the database.

Give a name to the collection. For this example, I gave the name here “products.”

After creating the collection, add a document. I’m adding a document in the products collection, as shown in the image below:

After adding the first document, the database should look like this:

Now, let’s add a few more documents to the collection. For that action, click on “Add document” in the products collection.

So, I’ve added five documents to the products collection. We’re going to work with the products collection now in the Ignite UI for Angular Grid. So far, we’ve only created the collection in Firebase Firestore.

Setting Up an Angular Project With Ignite UI

We have three options for setting up an anAngular project with Ignite UI for Angular.

  1. Use Ignite UI CLI to create a new Angular project configured with Ignite UI for Angular.
  2. Use Ignite UI for Angular in an existing Angular project.
  3. Use Ignite UI for Angular Toolbox Extension for Visual Studio Code.

From this point forward, I’ll assume that you already have an Angular project configured to work with Ignite UI for Angular.

Setting Up AngularFire

We’re going to use the AngularFire library to work with Firebase in an Angular project. To start, install AngularFire in the Angular project using npm.

npm install firebase @angular/fire --save

After installing AngularFire, we need to set up a Firebase environment. To do that, open environment.prod.ts and modify it, as shown below:

export const environment = {
    production: false,
    firebase: {
        apiKey: "yourkey",
        authDomain: "iggriddemo.firebaseapp.com",
        databaseURL: "https://iggriddemo.firebaseio.com",
        projectId: "iggriddemo",
        storageBucket: "iggriddemo.appspot.com",
        messagingSenderId: "1055912852453"
    }
};

If you recall from setting up Firestore, we added a web project. We need to copy that setting from there to the environment.prod.ts file. If you’re working in a development environment, you may want to add the above entry to the environment.ts file.

Next, in AppModule, import Firestore and AngularFire modules. Import in app.module.ts, as below:

import { AngularFirestoreModule } from '@angular/fire/firestore';
import { AngularFireModule } from '@angular/fire';
import { environment } from '../environments/environment';

Modify AppModule:

@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        IgxGridModule.forRoot(),
        AngularFireModule.initializeApp(environment.firebase),
        AngularFirestoreModule
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule {
}

Now, we’ve added AngularFireModule and AngularFireStoreModule in imports array.

Read Data From Firestore Collections

We’re going to read data from the Firestore collections in AppComponent. First, import AngularFirestore and Observable in AppComponent.

import { AngularFirestore } from '@angular/fire/firestore';
import { Observable } from 'rxjs';

Next, let’s fetch data in the constructor of the component:

items: Observable<any[]>;
constructor(private db: AngularFirestore) {

    this.items = db.collection('/products').valueChanges();

}

If you remember, we have created a products collection in Firestore. Putting everything together, AppComponent will look like below:

import { Component } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
import { Observable } from 'rxjs';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    title = 'Ignite UI for Angular Grid + Firestore';
    items: Observable<any[]>;
    constructor(private db: AngularFirestore) {

        this.items = db.collection('/products').valueChanges();

    }
}

On the template, let’s add Ignite UI for Angular Grid:

<h1>
    Welcome to {{ title }}!
</h1>
<igx-grid [data]="items | async" [autoGenerate]="false" height="400px">
    <igx-column field="Id" [sortable]="true" header="Id" [filterable]="true"></igx-column>
    <igx-column field="Title" [sortable]="true" header="Title" [filterable]="true"></igx-column>
    <igx-column field="Price" [sortable]="true" header="Price" [filterable]="true"></igx-column>
    <igx-column field="Quantity" [sortable]="true" header="Quantity" [filterable]="true"></igx-column>
    <igx-column field="inStock" [sortable]="true" header="Stock" [filterable]="true"></igx-column>
</igx-grid>

So, let’s talk through the code. We:

  • Added igxGrid.
  • Set data through property binding to items. Since it is an observable, we are using an async pipe.
  • Set the autoGenerate property to false because we are going to add columns manually.
  • Configured columns and bound it to the Firestore collection products columns.

On running the application you will find the Ignite UI for Angular Grid is displaying data from Firestore. For products, collection igxGrid should be rendered, as shown in the image below:

In the next post, we will see how we can perform Create, Update, and Delete operations. I hope you find this post useful, and now you should able to work with Firebase Firestore and Ignite UI for Angular Grid with ease.

AngularJS Firebase Database Data (computing)

Published at DZone with permission of Dhananjay Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Real-Time Analytics for IoT
  • Cloud Performance Engineering
  • Custom Validators in Quarkus
  • A Beginner's Guide to Infrastructure as Code

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: