Angular material pagination is not working with API data - angular-material

I have created an api using Web APi in .Net and data is coming from SQL Server and displayed on FrontEnd using Angular, i have used Angular material pagination but its not working. please let me know if there is wrong.I have created an api using Web APi in .Net and data is coming from SQL Server and displayed on FrontEnd using Angular, i have used Angular material pagination but its not working. please let me know if there is wrong.
comp.ts
import { Component, OnInit,ViewChild } from '#angular/core';
import { HttpClient } from '#angular/common/http';
import {GetdataService} from './getdata.service';
import { ActivatedRoute, Router } from '#angular/router';
import { MsalService } from '#azure/msal-angular';
import { Loader } from '#googlemaps/js-api-loader';
import { FormControl } from '#angular/forms';
import { styles } from './mapstyles';
import {MatPaginator} from '#angular/material/paginator';
import {MatTableDataSource} from '#angular/material/table';
import { Observable } from 'rxjs';
import { map, startWith } from 'rxjs/operators';
import { NgxSpinnerService } from "ngx-spinner";
import { MatSort } from '#angular/material/sort';
#Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit{
userdata: any;
#ViewChild(MatPaginator,{static:true}) paginator: MatPaginator;
#ViewChild(MatSort) sort: MatSort;
constructor(private service:GetdataService, private authService: MsalService, private http: HttpClient, private msalService: MsalService,private route: ActivatedRoute, private router: Router,private SpinnerService: NgxSpinnerService) {}
ngOnInit(): void {
this.userdata.paginator = this.paginator;
}
Model='';
searchMachine(){
this.http.get("https://localhost:5001/api/Machines/"+this.Model.trim()).subscribe(data =>{
console.log(data)
this.userdata=data;
this.userdata.paginator = this.paginator;
})
}
}
comp.html
<table class="styled-table" >
<thead>
<tr>
<th>Machine ID</th>
<th>Model</th>
<th>Machine Type</th>
<th>Serial</th>
<th>User ID</th>
<th>Created Date</th>
<th>Modified Data</th>
<th>Shared Machines</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let group of userdata">
<td>{{group.machineID}}</td>
<td>{{group.model}}</td>
<td>{{group.machineType}}</td>
<td>{{group.serial}}</td>
<td>{{group.userID}}</td>
<td>{{group.createdDate}}</td>
<td>{{group.modifieddate}}</td>
<td>{{group.isShared}}</td>
</tr>
</tbody>
</table>
<mat-paginator [pageSizeOptions]="[5, 10, 25, 100]" showFirstLastButtons></mat-paginator>

Related

Having issues getting #ContentChild working with Directive

I've been trying to get ContentChild with Directive working in a demo/example and I keep running into the directive not working. No errors being thrown. I've replicated the scenario on StackBlitz and I'm getting the same problem: https://stackblitz.com/edit/angular-contentchild-directive-etktcd
Why am I still getting "undefined" for the child input?
Here is the Directive:
import { Component, Directive, Input, ContentChild, OnInit, OnDestroy, forwardRef, AfterContentInit} from '#angular/core';
import { AbstractControl } from '#angular/forms';
import { FocusDirective } from '../directive/focus.directive';
#Component({
selector: 'field-validation',
template: `
<ng-content></ng-content>
`
})
export class FieldValidationComponent implements OnInit, AfterContentInit {
#ContentChild(FocusDirective) input: FocusDirective;
ngOnInit(): void {
console.log("ngOnInit::input is: ", this.input);
// this.input.focusChange.subscribe((focus) => {
// this.updateAttributes();
// });
}
ngAfterContentInit(): void {
console.log("ngAfterContentInit::input is: ", this.input);
}
}
Here is the child Component:
import { Component, Directive, Input, ContentChild, OnInit, OnDestroy,
forwardRef, AfterContentInit} from '#angular/core';
import { AbstractControl } from '#angular/forms';
import { FocusDirective } from '../directive/focus.directive';
#Component({
selector: 'field-validation',
template: `
<ng-content></ng-content>
`
})
export class FieldValidationComponent implements OnInit, AfterContentInit {
#ContentChild(FocusDirective) input: FocusDirective;
ngOnInit(): void {
console.log("ngOnInit::input is: ", this.input);
// this.input.focusChange.subscribe((focus) => {
// this.updateAttributes();
// });
}
ngAfterContentInit(): void {
console.log("ngAfterContentInit::input is: ", this.input);
}
}
Here is the HTML in the parent app:
<form [formGroup]="testForm">
<field-validation>
<input type="text" placeholder="0.00">
</field-validation>
<div>
<button type="submit">FAKE SUBMIT</button>
</div>
</form>
Please add the FocusDirective class to declaration property of AppModule, as shown below.
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { ReactiveFormsModule } from '#angular/forms';
import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
import { FieldValidationComponent } from './child-component/field-validation.component';
import { RxReactiveFormsModule } from '#rxweb/reactive-form-validators'
import { FocusDirective } from './directive/focus.directive';
#NgModule({
imports: [ BrowserModule, ReactiveFormsModule, RxReactiveFormsModule],
declarations: [ AppComponent, HelloComponent, FieldValidationComponent, FocusDirective],
bootstrap: [ AppComponent ]
})
export class AppModule { }

`#ngrx/store` - createFeatureSelector never called from wrapper component

Here is my createFeatureSelector ts file :
import { State } from "./../../state/app.state";
import { EventState, ModelEvent } from "./../models/model.event";
import { createFeatureSelector, createSelector, ActionReducerMap } from "#ngrx/store";
export interface NewState extends State {
events:EventState
}
const getCalendarFeatureState = createFeatureSelector<EventState>("reducer");
export const getEvents = createSelector(getCalendarFeatureState, state => state.events );
my events shell component ( where i call the getEvents ):
import { Component, OnInit, ChangeDetectionStrategy } from '#angular/core';
import { Store, select } from '#ngrx/store';
import { Observable } from 'rxjs';
import * as fromRoot from "./../../state";
import { CalendarActions, Load } from "./../../state/calendar.actions";
import { ModelEvent, EventState } from "./../../models/model.event";
#Component({
selector: 'calendar-shell',
templateUrl: './calendar-shell.component.html',
changeDetection:ChangeDetectionStrategy.OnPush
})
export class CalendarShellComponent implements OnInit {
events$:Observable<any>
constructor(private store:Store<fromRoot.NewState>) { }
ngOnInit():void{
this.store.dispatch(new Load());
this.events$ = this.store.pipe(select(fromRoot.getEvents)); //getting nothing!!
}
}
template :
<div *ngIf="events$ | async"> //shows nothing!!
<ul>
<li *ngFor="let item of events">{{item.title}}</li>
</ul>
</div>
Any help? thanks in advance. ( ready to provide further details if any )
I update the dom element like this:
<div *ngIf="events$ | async"> //shows nothing!!
<ul>
<li *ngFor="let item of events | async">{{item.title}}</li>
</ul>
</div>
works fine.

How do I pass a JSON array to an Angular2 template with #Input from a ASP.NET MVC View?

I'm new to Angular2 and have many questions.
I have some controllers that I wrote in AngularJS that I now want to convert to Angular2 components.
I would like to pass JSON data to the component and use in the template. In AngularJS I used ng-init to do this.
I have tried initialising the component in the ASP.NET MVC view with:
<review [reviewJson]="#Newtonsoft.Json.JsonConvert.SerializeObject(Model.CustomerReviews)"></review>
The component looks like this:
import { Component, OnInit, Input } from '#angular/core';
#Component({
selector: 'review',
template: `
<div id="reviews-wrapper" class="row">
<div class="item col-sm-4 col-xs-6" *ngFor="let review of reviewJson">
<div class="review-card">
<div class="review-image"><img [src]="review.Image"/></div>
<div class="review-name">review.Name</div>
<div class="review-text">review.Text</div>
</div>
</div>
</div>
`,
providers: []
})
export class ReviewComponent implements OnInit {
#Input() reviewJson: any;
constructor() {
}
ngOnInit() {
console.log(this.reviewJson);
}
}
The app.module file where I declare the review component looks like this:
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { HttpModule } from '#angular/http';
import { ReviewComponent } from './review.component';
#NgModule({
imports: [
BrowserModule,
HttpModule
],
declarations: [
ReviewComponent
],
bootstrap: [ReviewComponent],
providers: [
]
})
export class AppModule { }
The console.log(this.reviewJson) is showing undefined when I load the page so obviously I'm doing something completely wrong.
Any help appreciated.
You cannot directly bind the MVC models into the angular template - fetch the data as JSON using service and then bind the value to it's #Input decorator
Hope this helps - happy coding :)

custom mapping json file in angular 6 from rails api project

I'm new to Angular and I'm having a problem with map data from my Rails app to the Angular app. I know I need to do some customisation to the code, but I don't really know how.
I want to mention that I'm using Rails 5.2 and Active Model Serializer for the json request.
In my rails app I have Course,Segment,Quiz,Video models and the relations are:
Course had many Segments
Segments can be Quiz or Video and I use STI so the quizzes and videos
are in segments table
This is the json file I get from the rails app and I want to map:
[
{
"id":1,
"title":"Introduction",
"author":"Dana Inbar",
"segments":[
{
"id":1,
"name":"Intro01",
"data":"http://www.video.com/1/1"
},
{
"id":2,
"name":"Intro02",
"data":"http://www.video.com/1/2"
},
{
"id":3,
"name":"Intro03",
"data":"[
{
"question 1" : "___",
"answers" : {
"1" : "____",
"2" : "____",
"3" : "____"
},
"correct_answer" : "2"
},
]"
}
]
},
{
"id":2,
"title":"Master the ELN",
"author":"Dana Inbar",
"segments":[
]
},
{
"id":3,
"title":"Master the Inventory",
"author":"Dana Inbar",
"segments":[
]
}
]
This are my angular files:
app.module
import { BrowserModule } from '#angular/platform-browser';
import { NgModule } from '#angular/core';
import { HttpClientModule } from '#angular/common/http';
import { RouterModule, Routes } from '#angular/router';
import { FormsModule } from '#angular/forms';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CourseModule } from './courses/course.module';
import { CourseService } from './courses/course.service';
#NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
AppRoutingModule,
CourseModule,
FormsModule
],
providers: [CourseService],
bootstrap: [AppComponent]
})
export class AppModule { }
courses/course
export interface IQuiz {}
export interface IVideo {}
export interface ISegment {
segment_id: string;
name: string;
type: string;
data: string;
}
export interface ICourse {
course_id: number;
title: string;
autor: string;
segments: {
[segment_id: string]: ISegment,
[name: string]: ISegment,
[type: string]: ISegment,
[data: string]: ISegment
}
}
courses/course.service
import { Injectable } from '#angular/core';
import { HttpClient } from '#angular/common/http';
import { Observable, throwError } from 'rxjs';
import { map, tap, catchError } from 'rxjs/operators';
import { ICourse } from './course';
// Inject Data from Rails app to Angular app
#Injectable()
export class CourseService{
private courseUrl = 'http://localhost:3000/courses.json';
constructor(private http: HttpClient) { }
// Get Courses from Rails API App
getCourses(): Observable<ICourse[]> {
return this.http.get<ICourse[]>(this.courseUrl)
.map((res: Response) => res.json()data)
.catch(this.handleError);
}
// Handle Any Kind of Errors
private handleError(error: Response) {
console.error(error);
return Observable.throw(error.json().error || 'Server Error');
}
}
courses/course.module
import { NgModule } from '#angular/core';
import { CommonModule } from '#angular/common';
import { RouterModule, Routes } from '#angular/router';
import { MatSidenavModule } from '#angular/material/sidenav';
import { BrowserAnimationsModule } from '#angular/platform-browser/animations';
import { CourseListComponent } from './course-list/course-list.component';
import { CourseDetailComponent } from './course-detail/course-detail.component';
import { CourseService } from './course.service';
import { CoursePlayComponent } from './course-play/course-play.component';
const coursesRoutes: Routes = [
{ path: 'courses', component: CourseListComponent },
{ path: 'courses/:id', component: CourseDetailComponent },
{ path: 'courses/:id/1', component: CoursePlayComponent }
]
#NgModule({
imports: [
CommonModule,
MatSidenavModule,
BrowserAnimationsModule,
RouterModule.forChild(
coursesRoutes
)
],
declarations: [
CourseListComponent,
CourseDetailComponent,
CoursePlayComponent
],
providers: [
CourseService
]
})
export class CourseModule { }
courses/course-list/course-list.component
import { Component, OnInit } from '#angular/core';
import { ActivatedRoute, Router } from '#angular/router';
import { ICourse } from '../course';
import { CourseService } from '../course.service';
#Component({
selector: 'lg-course-list',
templateUrl: './course-list.component.html',
styleUrls: ['./course-list.component.sass']
})
export class CourseListComponent implements OnInit {
pageTitle = "LabGuru Academy";
courses: ICourse[] =[];
errorMessage: string;
constructor(private courseService: CourseService) { }
ngOnInit() {
this.courseService.getCourses()
.subscribe(
courses => this.courses = courses,
errorMessage => this.errorMessage = <any>error
);
}
}
courses/course-list/course-list.html
<div class="row mt-3 no-gutters">
<div class="col-lg-8">
<img src="./assets/images/lg-purple.png" class="d-inline-block align-top" alt="">
</div>
</div>
<div class="jumbotron">
<div class="container text-center">
<h1>{{pageTitle}}</h1>
</div>
</div>
<div class="container-fluid bg-3 text-center">
<div class="row justify-content-lg-center">
<div class="col-sm-3" *ngFor="let course of courses | async">
<div class="card">
<div class="card-body">
<h3 class="card-title">{{course.title}}</h3>
<p class="card-text"><small class="text-muted">6 Lessons</small></p>
</div>
</div>
</div>
</div>
</div>
<br><br>
courses/course-list/course-list.sass
$pos: 70px
$color: #5c0099
$bg-col: #FFFFFF
$hig: 15rem
$wid: 15rem
$left: 5%
$top: 30%
$right: 7%
.jumbotron
background-color: transparent
color: $color
.col-sm-3
position: relative
min-height: 1px
padding-left: $left
outline-offset: none
.card
border-color: $color
background-color: $bg-col
width: $wid
height: $hig
.card-title
position: absolute
text-align: center
padding: inherit
.card-text
position: absolute
bottom: $left
right: $right

How to use routing of angular2 and .net mvc Routing

I am doing authentication and authorization of pages on the server side. on index pages of each controller. But inside each index page I want to use angular 2 hence I want to use angular 2 routing.
I have tried like
app.component.ts
import { Component } from '#angular/core';
#Component({
selector: 'my-app',
template:''
})
export class AppComponent {
}
app.module.ts
import {NgModule} from '#angular/core';
import {BrowserModule} from '#angular/platform-browser';
import { AppComponent } from './app.component';
import { RouterModule } from '#angular/router'
import { TestSuiteComponent } from './testsuite/testsuite.component'
//import {InputTextModule, CalendarModule, DataTable} from 'primeng/primeng';
import { DataTableModule, SharedModule } from 'primeng/primeng';
#NgModule({
imports: [BrowserModule, DataTableModule, SharedModule, RouterModule.forRoot([
{
path: 'TestSuiteEditor/Index',
component: TestSuiteComponent
},
{
path: 'Home/Index',
component: TestSuiteComponent
}
])],
declarations: [AppComponent,TestSuiteComponent],
bootstrap: [ AppComponent]
})
export class AppModule { }
on testsuite.component.ts page
import { Directive, Component, OnInit } from '#angular/core';
import {DataTableModule, SharedModule} from 'primeng/primeng';
import { TestSuite } from './testsuite';
#Component({
// moduleId: module.id,
selector: 'testsuite-header',
template: `
<div class="ui-widget-header ui-helper-clearfix" style="padding:4px 10px;border-bottom: 0 none">
<i class="fa fa-search" style="float:left;margin:4px 4px 0 0"></i>
<input #gb type="text" pInputText size="50" style="float:left" placeholder="Global Filter">
</div>
<div class="ui-datatable ui-widget">
<div class="ui-datatable-tablewrapper">
<p-dataTable [value]="testSuites" [rows]="5" [paginator]="true" [globalFilter]="gb" [editable]="true">
<p-column field="testSuiteId" header="TestSuites (startsWith)" [style]="{'width':'10%'}" [filter]="true" [editable]="true"></p-column>
<p-column field="projectId" header="ProjectId (contains)" [style]="{'width':'10%'}" [filter]="true" filterMatchMode="contains" [editable]="true"></p-column>
<p-column field="name" header="Name (startsWith)" [style]="{'width':'30%'}" [filter]="true" [editable]="true"></p-column>
<p-column field="description" header="Description (endsWith)" [style]="{'width':'40%'}" [filter]="true" filterMatchMode="endsWith" [editable]="true"></p-column>
<p-column field="isActive" header="IsActive (endsWith)" [style]="{'width':'10%'}" [filter]="true" filterMatchMode="endsWith" [editable]="true"></p-column>
</p-dataTable>
</div>
</div> `,
// providers: [TestSuiteService]
})
export class TestSuiteComponent{}
Home/Index.cshtml
<testsuite-header>Loading....</testsuite-header>
but it throwing an error of
Cannot find primary outlet to load 'TestSuiteComponent'
You have forgotten to add router-outlet in your application. You can define router-outlet in AppComponent like this-
<router-outlet></router-outlet>
So your AppComponent will looks like this-
import { Component } from '#angular/core';
#Component({
selector: 'my-app',
template:`
<!-- Routed views go here -->
<router-outlet></router-outlet>
`
})
export class AppComponent {
}
I have found the solution, thanks for your help
I have created the path similar to my MVC routing and added in routing.ts.
Since angular 2routing have feature of loading the active url so it automatically loads the component which is assign to that URL.

Resources