import { ChangeDetectorRef, Component, Injectable, OnInit, AfterViewInit, ViewChild, ElementRef, Renderer2 } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
// https://blog.angularindepth.com/exploring-angular-dom-abstractions-80b3ebcfc02
// https://blog.angularindepth.com/the-mechanics-of-dom-updates-in-angular-3b2970d5c03d
const URL = 'https://my-json-server.typicode.com/spham-xyz/cars/cars';
@Component({
selector: 'my-app',
templateUrl: 'app/app.template.html'
})
export class AppComponent implements OnInit, AfterViewInit{
cars: any[];
cars1: any[];
cols: any[];
colors: SelectItem[];
cities: SelectItem[];
selectedColor: string;
selectedState: string;
display: boolean = false;
carData: string = '';
colorNames = ['Orange', 'Black', 'Gray', 'Blue', 'Orange', 'Yellow'];
colors = this.colorNames.map((name) => {
return { label: name, value: name }
});
stateNames = ['Alabama', 'Alaska', 'California'];
states = this.stateNames.map((val, i, stateNames) => {
return { label: val, value: val }
});
cityNames = [
{state: 'Alabama', city: 'Birmingham'},
{state: 'Alabama', city: 'Huntsville'},
{state: 'Alabama', city: 'Montgomery'},
{state: 'Alaska', city: 'Anchorage'},
{state: 'Alaska', city: 'Juneau'},
{state: 'California', city: 'Fresno'},
{state: 'California', city: 'Perris'}
];
// https://codeburst.io/javascript-learn-to-chain-map-filter-and-reduce-acd2d0562cd4
// https://hackernoon.com/understanding-map-filter-and-reduce-in-javascript-5df1c7eee464
getCities(state):any[] {
//let cityArray = this.cityNames.filter((el) => { return el.state === state });
//this.cities = cityArray.map((el) => {
// return { label: el.city, value: el.city }
//});
this.cities = this.cityNames
.filter((el) => { return el.state === state })
.map((el) => { return { label: el.city, value: el.city } });
}
constructor(private cdref: ChangeDetectorRef, private http: HttpClient) { }
ngOnInit() {
console.log('OnInit: ' + this.selectedState);
//this.getCities(this.selectedState);
this.cols = [
{ field: 'vin', header: 'Vin' },
{ field: 'year', header: 'Year' },
{ field: 'brand', header: 'Brand' },
{ field: 'color', header: 'Color' }
];
this.cars = [
{"brand": "VW", "year": 2012, "color": "Orange", "vin": "dsad231ff"},
{"brand": "Audi", "year": 2011, "color": "Black", "vin": "gwregre345"},
{"brand": "Renault", "year": 2005, "color": "Gray", "vin": "h354htr"},
{"brand": "BMW", "year": 2003, "color": "Blue", "vin": "j6w54qgh"},
{"brand": "Mercedes", "year": 1995, "color": "Orange", "vin": "hrtwy34"},
{"brand": "Volvo", "year": 2005, "color": "Black", "vin": "jejtyj"},
{"brand": "Honda", "year": 2012, "color": "Yellow", "vin": "g43gr"},
{"brand": "Jaguar", "year": 2013, "color": "Orange", "vin": "greg34"},
{"brand": "Ford", "year": 2000, "color": "Black", "vin": "h54hw5"},
{"brand": "Fiat", "year": 2013, "color": "Red", "vin": "245t2s"},
{"brand": "VW", "year": 2012, "color": "Orange", "vin": "dsad231ff"},
{"brand": "Audi", "year": 2011, "color": "Black", "vin": "gwregre345"},
{"brand": "Renault", "year": 2005, "color": "Gray", "vin": "h354htr"},
{"brand": "BMW", "year": 2003, "color": "Blue", "vin": "j6w54qgh"},
{"brand": "Mercedes", "year": 1995, "color": "Orange", "vin": "hrtwy34"},
{"brand": "Volvo", "year": 2005, "color": "Black", "vin": "jejtyj"},
{"brand": "Honda", "year": 2012, "color": "Yellow", "vin": "g43gr"},
{"brand": "Jaguar", "year": 2013, "color": "Orange", "vin": "greg34"},
{"brand": "Ford", "year": 2000, "color": "Black", "vin": "h54hw5"},
{"brand": "Fiat", "year": 2013, "color": "Red", "vin": "245t2s"},
{"brand": "VW", "year": 2012, "color": "Orange", "vin": "dsad231ff"},
{"brand": "Audi", "year": 2011, "color": "Black", "vin": "gwregre345"},
{"brand": "Renault", "year": 2005, "color": "Gray", "vin": "h354htr"},
{"brand": "BMW", "year": 2003, "color": "Blue", "vin": "j6w54qgh"},
{"brand": "Mercedes", "year": 1995, "color": "Orange", "vin": "hrtwy34"},
{"brand": "Volvo", "year": 2005, "color": "Black", "vin": "jejtyj"},
{"brand": "Honda", "year": 2012, "color": "Yellow", "vin": "g43gr"},
{"brand": "Jaguar", "year": 2013, "color": "Orange", "vin": "greg34"},
{"brand": "Ford", "year": 2000, "color": "Black", "vin": "h54hw5"},
{"brand": "Fiat", "year": 2013, "color": "Red", "vin": "245t2s"}
];
this.http.get(URL).subscribe(
data => this.cars1 = data
);
}
ngAfterViewInit() {
console.log('AfterViewInit: ' + this.selectedState);
this.getCities(this.selectedState);
this.cdref.detectChanges();
}
selectRow(event) {
this.display = true;
this.carData = event.data.vin;
console.log(event.data.vin);
}
log(data):void {
console.log(data);
console.log('selectedColor: ' + this.selectedColor);
}
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
// Import PrimeNG modules
import { ButtonModule } from 'primeng/button';
import { DialogModule } from 'primeng/dialog';
import { DropdownModule } from 'primeng/dropdown';
import { TableModule } from 'primeng/table';
@NgModule({
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule,
BrowserAnimationsModule,
HttpClientModule,
ButtonModule,
DialogModule,
DropdownModule,
TableModule],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);
body {
padding: 2em;
font-family: Arial, Helvetica, sans-serif;
}
<!DOCTYPE html>
<html>
<head>
<title>Angular QuickStart</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<!-- PrimeNG style dependencies -->
<link rel="stylesheet" href="https://unpkg.com/primeng@5.2.4/resources/themes/omega/theme.css" />
<!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" /> -->
<link rel="stylesheet" href="https://unpkg.com/font-awesome@4.7.0/css/font-awesome.min.css" />
<link rel="stylesheet" href="https://unpkg.com/primeng@5.2.4/resources/primeng.min.css" />
<!-- 1. Load libraries -->
<!-- Polyfill for older browsers -->
<script src="https://unpkg.com/core-js/client/shim.min.js"></script>
<script src="https://unpkg.com/zone.js@0.8.23?main=browser"></script>
<script src="https://unpkg.com/reflect-metadata@0.1.12"></script>
<script src="https://unpkg.com/systemjs@0.19.47/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>
<!--
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
-->
/**
* WEB ANGULAR VERSION
* (based on systemjs.config.js in angular.io)
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(function(global) {
System.config({
// DEMO ONLY! REAL CODE SHOULD NOT TRANSPILE IN THE BROWSER
transpiler: 'ts',
typescriptOptions: {
// Copy of compiler options in standard tsconfig.json
"target": "es5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
},
meta: {
'typescript': {
"exports": "ts"
}
},
paths: {
// paths serve as alias
'npm:': 'https://unpkg.com/'
},
// map tells the System loader where to look for things
// https://stackoverflow.com/questions/45634559/systemjs-xhr-error-angular-commobundles-common-umd-js-http-not-found
map: {
// our app is within the app folder
app: 'app',
// angular bundles
'@angular/animations': 'npm:@angular/animations@5.2.9/bundles/animations.umd.js',
'@angular/animations/browser': 'npm:@angular/animations@5.2.9/bundles/animations-browser.umd.js',
'@angular/core': 'npm:@angular/core@5.2.9/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common@5.2.9/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler@5.2.9/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser@5.2.9/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic@5.2.9/bundles/platform-browser-dynamic.umd.js',
'@angular/platform-browser/animations': 'npm:@angular/platform-browser@5.2.9/bundles/platform-browser-animations.umd.js',
'@angular/common/http': 'npm:@angular/common@5.2.9/bundles/common-http.umd.js',
'@angular/router': 'npm:@angular/router@5.2.9/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms@5.2.9/bundles/forms.umd.js',
'@angular/upgrade': 'npm:@angular/upgrade@5.2.9/bundles/upgrade.umd.js',
'@angular/upgrade/static': 'npm:@angular/upgrade@5.2.9/bundles/upgrade-static.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'tslib': 'npm:tslib/tslib.js',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
'ts': 'npm:plugin-typescript@4.0.10/lib/plugin.js',
'typescript': 'npm:typescript@2.8.1/lib/typescript.js',
'primeng': 'npm:primeng@5.2.4'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
rxjs: {
defaultExtension: 'js'
},
primeng: {
defaultExtension: 'js'
}
}
});
if (!global.noBootstrap) {
bootstrap();
}
// Bootstrap the `AppModule`(skip the `app/main.ts` that normally does this)
function bootstrap() {
console.log('Auto-bootstrapping');
// Stub out `app/main.ts` so System.import('app') doesn't fail if called in the index.html
System.set(System.normalizeSync('app/main.ts'), System.newModule({}));
// bootstrap and launch the app (equivalent to standard main.ts)
Promise.all([
System.import('@angular/platform-browser-dynamic'),
getAppModule()
])
.then(function(imports) {
var platform = imports[0];
var app = imports[1];
platform.platformBrowserDynamic().bootstrapModule(app.AppModule);
})
.catch(function(err) {
console.error(err);
});
}
// Import AppModule or make the default AppModule if there isn't one
// returns a promise for the AppModule
function getAppModule() {
if (global.noAppModule) {
return makeAppModule();
}
return System.import('app/app.module').catch(makeAppModule)
}
function makeAppModule() {
console.log('No AppModule; making a bare-bones, default AppModule');
return Promise.all([
System.import('@angular/core'),
System.import('@angular/platform-browser'),
System.import('app/app.component')
])
.then(function(imports) {
var core = imports[0];
var browser = imports[1];
var appComp = imports[2].AppComponent;
var AppModule = function() {}
AppModule.annotations = [
new core.NgModule({
imports: [browser.BrowserModule],
declarations: [appComp],
bootstrap: [appComp]
})
]
return {
AppModule: AppModule
};
})
}
})(this);
<p-dialog header="Title" [modal]="true" [(visible)]="display" width="250" height="150">
{{carData}}
</p-dialog>
<p-dropdown [options]="colors" [(ngModel)]="selectedColor" [autoWidth]="false" (onChange)="log($event.value)"></p-dropdown>
<br />
<p-dropdown [options]="states" [(ngModel)]="selectedState" [autoWidth]="false" (onChange)="getCities($event.value)"></p-dropdown>
<p-dropdown [options]="cities" [autoWidth]="false"></p-dropdown>
<br />
<!--
<p-table [value]="cars" [paginator]="true" [rows]="5" [alwaysShowPaginator]="false">
<ng-template pTemplate="header">
<tr>
<th>#</th>
<th>Vin</th>
<th>Year</th>
<th>Brand</th>
<th>Color</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-rowData let-car let-rowIndex="rowIndex">
<tr>
<td [ngStyle]="{'text-align':'center'}">{{rowIndex + 1}}</td>
<td>{{car.vin}}</td>
<td>{{car.year}}</td>
<td>{{car.brand}}</td>
<td>
<p-dropdown appendTo="body" [options]="colors" [(ngModel)]="car.color" [style]="{'width':'100%'}" (onChange)="log($event.value)"></p-dropdown>
</td>
</tr>
</ng-template>
</p-table>
-->
<h4>Dynamic Columns</h4>
<p-table #dt [value]="cars1" [columns]="cols" [paginator]="true" [alwaysShowPaginator]="false" [rows]="5" [rowsPerPageOptions]="[5,10,20]" [scrollable]="true" selectionMode="single" (onRowSelect)="selectRow($event)">
<ng-template pTemplate="caption">
<div style="text-align: right">
<i class="fa fa-search" style="margin:4px 4px 0 0"></i>
<input type="text" pInputText size="50" placeholder="Global Filter" (input)="dt.filterGlobal($event.target.value, 'contains')">
</div>
</ng-template>
<ng-template pTemplate="header" let-columns>
<tr>
<th *ngFor="let col of columns" class="ui-resizable-column" [pSortableColumn]="col.field">
{{col.header}}
<p-sortIcon [field]="col.field"></p-sortIcon>
</th>
</tr>
<!--
<tr>
<th *ngFor="let col of columns">
<p-dropdown [options]="colors" [autoWidth]="false" [style]="{'width':'100%'}" appendTo="body"></p-dropdown>
</th>
</tr>
-->
</ng-template>
<ng-template pTemplate="body" let-rowData let-columns="columns">
<!-- <tr [pSelectableRow]="rowData" [pSelectableRowDblClick]="rowData" (dblclick)="onRowDblClick($event, rowData)"> -->
<tr [pSelectableRow]="rowData" [pSelectableRowDblClick]="rowData">
<td *ngFor="let col of columns" class="ui-resizable-column" [ngSwitch]="col.field">
<span *ngSwitchCase="'color'"><p-dropdown appendTo="body" [options]="colors" [(ngModel)]="rowData[col.field]" [style]="{'width':'100%','overflow':'visible'}" (onChange)="log($event.value)"></p-dropdown></span>
<span *ngSwitchDefault>{{rowData[col.field]}}</span>
</td>
</tr>
</ng-template>
</p-table>
<p-footer>
<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
<button type="button" pButton icon="fa-close" (click)="display=false" label="No"></button>
<button type="button" pButton icon="fa-check" (click)="display=false" label="Yes"></button>
</div>
</p-footer>
<p-table [value]="cars" [paginator]="true" [rows]="10" [rowsPerPageOptions]="[5,10,20]" [scrollable]="true">
<ng-template pTemplate="header">
<tr>
<th>#</th>
<th>Vin</th>
<th>Year</th>
<th>Brand</th>
<th>Color</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-rowData let-rowIndex="rowIndex">
<tr>
<td>{{rowIndex+1}}</td>
<td pEditableColumn>
<p-cellEditor>
<ng-template pTemplate="input">
<input type="text" [(ngModel)]="rowData.vin">
</ng-template>
<ng-template pTemplate="output">
{{rowData.vin}}
</ng-template>
</p-cellEditor>
</td>
<td pEditableColumn>
<p-cellEditor>
<ng-template pTemplate="input">
<input type="text" [(ngModel)]="rowData.year" required>
</ng-template>
<ng-template pTemplate="output">
{{rowData.year}}
</ng-template>
</p-cellEditor>
</td>
<td pEditableColumn>
<p-cellEditor>
<ng-template pTemplate="input">
<input type="text" [(ngModel)]="rowData.brand">
</ng-template>
<ng-template pTemplate="output">
{{rowData.brand}}
</ng-template>
</p-cellEditor>
</td>
<td pEditableColumn>
<p-cellEditor>
<ng-template pTemplate="input">
<!-- <input type="text" [(ngModel)]="rowData.color"> -->
<p-dropdown appendTo="body" [options]="colors" [(ngModel)]="rowData.color" [style]="{'width':'100%'}" (onChange)="log($event.value)"></p-dropdown>
</ng-template>
<ng-template pTemplate="output">
{{rowData.color}}
</ng-template>
</p-cellEditor>
</td>
</tr>
</ng-template>
</p-table>
# PrimeNG Issue Template
Please create a test case and attach the link of the plunkr to your github issue report.