I am new babie in Angular (Currently using 14.2.10)
Need to make REST call and populate the data in UI. But I can see only header is populated without any data.
I feel some minor issue (Not able to figure out). In Developer console i can see data is populating. But not in UI.
Please find my html and TS file.
app.component.html
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!--- Note that these columns can be defined in any order.
The actual rendered columns are set as a property on the row definition" -->
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
<!-- name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let Employees"> {{Employees.name}} </td>
</ng-container>
<!-- position Column -->
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef> Position </th>
<td mat-cell *matCellDef="let Employees"> {{Employees.position}} </td>
</ng-container>
<!-- office Column -->
<ng-container matColumnDef="office">
<th mat-header-cell *matHeaderCellDef> office </th>
<td mat-cell *matCellDef="let Employees"> {{Employees.office}} </td>
</ng-container>
<!-- salary Column -->
<ng-container matColumnDef="salary">
<th mat-header-cell *matHeaderCellDef> salary </th>
<td mat-cell *matCellDef="let Employees"> {{Employees.salary}} </td>
</ng-container>
</table>
app.component.ts
import { Component } from '#angular/core';
import {HttpClient} from '#angular/common/http';
import { MatTableDataSource, MatTableModule } from '#angular/material/table';
import { DataSource } from '#angular/cdk/table';
export interface Employees {
name: string;
position: string;
office:string ;
salary: number;
}
#Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Sample-App';
li:any;
lis=[];
datasource: any;
displayedColumns: string[] = ['name', 'position', 'office', 'salary'];
constructor(private http : HttpClient){
}
ngOnInit() {
this.http.get('http://www.mocky.io/v2/5ea172973100002d001eeada')
.subscribe(Response => {
console.log(Response)
this.li=Response;
this.lis=this.li.list;
this.datasource = new MatTableDataSource(this.lis);
console.log(this.datasource)
});
}
}
Output :
In Developer mode i can see
Please correct me where i am going wrong.
Your code is fine. See here: Stackblitz
It looks like you need to fix your dependencies. You're using Angular 14.2.10, but Material 13.0 and Material CDK 13.3.9.
EDIT
I updated the Stackblitz, before I was getting the error:
Mixed Content: The page at '<URL>' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint '<URL>'. This request has been blocked; the content must be served over HTTPS.
The URL you provided wasn't secure, so I changed from:
http://www.mocky.io/v2/5ea172973100002d001eeada
to
https://www.mocky.io/v2/5ea172973100002d001eeada
The Stackblitz works fine with the rest of the code you provided.
Related
I am using Angular 14.2.7. My requirement here is need to make REST call and populate data in UI. Once data is available need to filter out based on column. I can defined filter column in ngOnInit method. I can populate data. When i use filter without it is giving me correct result.
I mean it filter out the records from all columns.
When i use filterPredicate (to search in specific column) is not able to search the record. In browser console i can see below error
main.ts:11 ERROR TypeError: Cannot set properties of undefined (setting 'filterPredicate')
at AppComponent.ngOnInit (app.component.ts:30:36)
at callHook (core.mjs:2498:22)
at callHooks (core.mjs:2467:17)
at executeInitAndCheckHooks (core.mjs:2418:9)
at refreshView (core.mjs:12026:21)
at detectChangesInternal (core.mjs:13229:9)
at RootViewRef.detectChanges (core.mjs:13741:9)
at ApplicationRef.tick (core.mjs:27377:22)
at ApplicationRef._loadComponent (core.mjs:27415:14)
at ApplicationRef.bootstrap (core.mjs:27352:14)
After reading this error i can understand some my REST call is still running and tried to this.dataSource.filterPredicate.
What changes i need to do achive this.?
Please find my code.
import { HttpClient } from '#angular/common/http';
import { MatTableDataSource, MatTableModule } from '#angular/material/table';
import { DataSource } from '#angular/cdk/table';
import { Component } from '#angular/core';
import {MatInputModule} from '#angular/material/input';
#Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Sample-App';
li: any;
lis = [];
dataSource: any;
displayedColumns: string[] = ['name', 'position', 'office', 'salary'];
public data = {list: [{name:'Billy Lee',position:'Web Developer',office:'Detroit',salary:'$50000'},{name:'John Doe',position:'Manager',office:'Troy',salary:'$90000'},{name:'James Baxter',position:'IT Support',office:'Detroit',salary:'$30000'}]};
constructor(private http: HttpClient) { }
ngOnInit() {
this.getData().subscribe((data: any) => {
this.dataSource = new MatTableDataSource(data.list);
console.log("API", data);
});
this.dataSource.filterPredicate = function (dataSource : any, filter : string) {
return dataSource.name.toLocaleLowerCase() == filter.toLocaleLowerCase();
}
}
getData() {
return this.http.get('http://www.mocky.io/v2/5ea172973100002d001eeada');
}
applyFilter(event: Event) {
const filterValue = (event.target as HTMLInputElement).value;
this.dataSource.filter = filterValue.trim().toLowerCase();
}
}
HTML
<mat-form-field appearance="standard">
<mat-label>Filter</mat-label>
<input (keyup)="applyFilter($event)" matInput placeholder="Search columns" #input>
</mat-form-field>
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!--- Note that these columns can be defined in any order.
The actual rendered columns are set as a property on the row definition" -->
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
<!-- name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let Employees"> {{Employees.name}} </td>
</ng-container>
<!-- position Column -->
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef> Position </th>
<td mat-cell *matCellDef="let Employees"> {{Employees.position}} </td>
</ng-container>
<!-- office Column -->
<ng-container matColumnDef="office">
<th mat-header-cell *matHeaderCellDef> office </th>
<td mat-cell *matCellDef="let Employees"> {{Employees.office}} </td>
</ng-container>
<!-- salary Column -->
<ng-container matColumnDef="salary">
<th mat-header-cell *matHeaderCellDef> salary </th>
<td mat-cell *matCellDef="let Employees"> {{Employees.salary}} </td>
</ng-container>
</table>
My Bokeh version is 2.3.1. I'm able to use the CustomJSHover to specify some javascript to execute on hover, with the input being a field (from a column data source). However, I'm trying to map different CustomJSHover's to the same field, and specify them differently in the HTML. An example snippet of what I have is
customjs = CustomJSHover(code="""///do some javascript here on the value var.""")
hover_points = HoverTool(tooltips="""
<div $x{custom} id=$index style="font-size:12px; font-family:arial; color:white; background:black; padding:10px;">
<div>
<span style="font-weight:bold;">Name:</span>
<span>#input_x{custom}</span>
</div>
""",
formatters={
'#input_x' :customjs
},
point_policy='snap_to_data')
This works great, but I want to have multiple custom formaters on input_x. If I try something like this
c1 = CustomJSHover(code="console.log(1);")
c2 = CustomJSHover(code="console.log(2);")
hover_highlight_line = HoverTool(tooltips="""
<div style="font-size:12px; font-family:arial; color:white; background:black; padding:10px;">
<table>
<tr>
<th style="text-align: center;">1</th>
<th style="text-align: center;">2</th>
</tr>
<tr>
<td style="padding:5px;">#input_x{c2}</td>
<td style="padding:5px;">#input_x{c1}</td>
</tr>
</table>
</div>
""",
formatters= {
'#input_x':c1,
'#input_x':c2
},
I only get the console log for c2. Is this possible to achieve? Thanks.
After sleeping on it, I finally found out how to do this.
Within the bracket, you can map a prefix to a formatter. This is in the docs, but the doc's don't tell you how to use it. Within the CustomJSCode argument, you can access this prefix (or if you want just specify the name) with the format variable.
c1 = CustomJSHover(code="""
if(format == 'agg'){ return //do something with agg}
else if(format == 'mean'){return //do something to get the mean}
""")
hover_highlight_line = HoverTool(tooltips="""
<div style="font-size:12px; font-family:arial; color:white; background:black; padding:10px;">
<table>
<tr>
<th style="text-align: center;">1</th>
<th style="text-align: center;">2</th>
</tr>
<tr>
<td style="padding:5px;">#input_x{agg}</td>
<td style="padding:5px;">#input_x{mean}</td>
</tr>
</table>
</div>
""",
formatters= {
'#input_x':c1,
},
If there is a better way to do this, please post an answer. Thanks.
My goal is to use react front end to setup a stripe checkout using node. I mapped the apiId using the key prop to iterate over all the items in my list. But there is an integration warning inside the console and I'm not sure why. I've added the js.stripe script src into my html file so I don't understand why an API call is not being made.
function checkout() {
stripe.redirectToCheckout({
items: items.map(item => ({
quantity: item.quantity,
price: item.apiId
})),
successUrl: "https://www.website.com/success",
cancelUrl: "https://www.website.com/canceled",
})
}
return (
<div>
<table>
<thead>
<tr>
<th>Name</th>
<th>Image</th>
<th>Quanity</th>
<th>Price</th>
</tr>
</thead>
<tbody>
{items.map((item) => (
<tr key={item.apiId}>
<td>{item.name}</td>
<td>
<img
src={`/images/${item.apiId}.jpg`}
alt={item.name}
width={180}
/>
</td>
<td>{item.quantity}</td>
<td>{formatPrice(item.price)}</td>
</tr>
))}
<tr>
<td style={{ textAlign: "right" }} colSpan={3}>
Total:
</td>
<td>{formatPrice(totalPrice(items))}</td>
</tr>
<tr>
<td style={{ textAlign: "right" }} colSpan={4}>
<button onClick={checkout}>Complete checkout</button>
</td>
</tr>
</tbody>
</table>
</div>
)
}
The items field only accepts an array of SKU/Plans and a quantity. If you're using Prices you want to use lineItems: https://stripe.com/docs/js/checkout/redirect_to_checkout#stripe_checkout_redirect_to_checkout-options-lineItems
Im not sure what my issue is here i see the arrow and i can log the sorting to the console and see that its seeing the field it should be sorting but when i click the table arrow to sort by username it doesnt sort it my code will be below any help pointing out what I may be doing wrong would be a huge help thanks everyone.
player-list.component.html
<mat-form-field>
<input
matInput
(keyup)="applyFilter($event.target.value)"
placeholder="Filter"
/>
</mat-form-field>
<table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8">
<!-- Position Column -->
<ng-container matColumnDef="Username">
<th mat-header-cell *matHeaderCellDef mat-sort-header>username</th>
<td mat-cell *matCellDef="let user">{{ user.UserName }}</td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="Region">
<th mat-header-cell *matHeaderCellDef>Region</th>
<td mat-cell *matCellDef="let user">{{ user.Region }}</td>
</ng-container>
<ng-container matColumnDef="Earnings">
<th mat-header-cell *matHeaderCellDef>Earnings</th>
<td mat-cell *matCellDef="let user">{{ user.Earnings }}</td>
</ng-container>
<!-- Weight Column -->
<ng-container matColumnDef="Wins">
<th mat-header-cell *matHeaderCellDef>Wins</th>
<td mat-cell *matCellDef="let user">{{ user.Wins }}</td>
</ng-container>
<!-- Symbol Column -->
<ng-container matColumnDef="Loses">
<th mat-header-cell *matHeaderCellDef>Loses</th>
<td mat-cell *matCellDef="let user">{{ user.Loses }}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
</table>
player-list.component.ts
import { UserService } from "./../../../services/API/userAPI";
import { User } from "./../../../services/API/user.model";
import { Component, OnInit, ViewChild } from "#angular/core";
import { MatPaginator, MatSort, Sort } from "#angular/material";
import { MatTableDataSource } from "#angular/material/table";
#Component({
selector: "app-player-list",
styleUrls: ["player-list.component.css"],
templateUrl: "player-list.component.html"
})
export class PlayerListComponent implements OnInit {
public userArray: User[];
dataSource: MatTableDataSource<any>;
displayedColumns: string[] = [
"Username",
"Region",
"Earnings",
"Wins",
"Loses"
];
constructor(public userService: UserService) {}
#ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
#ViewChild(MatSort, { static: true }) sort: MatSort;
applyFilter(filterValue: string) {
this.dataSource.filter = filterValue.trim().toLowerCase();
}
ngOnInit() {
this.userService.getAllUsers().subscribe(res => {
this.dataSource = new MatTableDataSource(res);
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
console.log(this.dataSource.sort);
});
}
}
Fix my issues if you look at this code here
<ng-container matColumnDef="Username">
<th mat-header-cell *matHeaderCellDef mat-sort-header="UserName">Username</th>
<td mat-cell *matCellDef="let user">{{ user.UserName }}</td>
</ng-container>
you will see mat-sort-header="UserName" i had that set to Username before not realizing that it needed to match the user.UserName
I'm working on adding an export/downloading feature to a Laravel 5 application. I found the Laravel-Excel library (http://www.maatwebsite.nl/laravel-excel/docs), which seems perfect -- ideally, I will be able to take a Laravel View or html and make a downloadable Excel file.
So far, I've gotten the creation and storage to work. I can create an Excel file, and it's okay (it's the right data and View), and on the server in storage/exports/ I see "Report.xls" - which is right save path. The issue I'm having is I cannot seem to download the file through the browser after creating it.
The Laravel-Excel library has ->download('xls') and ->export('xls') methods, but these only seems to return the raw content, which is visible in the developer's console:
On the right, you can see the file was created and stored as "Report.xls", and presumably the raw content is the right content - but I don't know why it's just spitting raw content into the console. I can open the Report.xls file in Excel, so I know it's not corrupted.
I also tried to use the Laravel 5 Response::download() function and set headers for the download, but it also spit out raw content in the console instead of downloading the file:
The code I'm using is an AJAX call that hits a Controller method, and the controller method just triggers a Service method called "downloadReportFile()":
public function downloadReportFile($requestData, $fileType) {
$configJSON = \Report::loadConfigFile($requestData['reportName']);
$today = Carbon::today()->format('Y-m-d');
$FileViewdata = array(
'config' => $configJSON,
'html' => $requestData['report_html']
);
\Excel::create("Report", function($excel) use ($FileViewdata) {
$excel->setTitle($FileViewdata['config']['title']);
$excel->setDescription($FileViewdata['config']['description']);
$excel->sheet("page 1", function($sheet) {
$sheet->loadView("reports.sample");
});
})->store('xls', storage_path('exports')); // ->export('xls');
$report_excelFilepath = storage_path('exports') . "/Report.xls";
return response()->download($report_excelFilepath, "Report.xls", [
'Content-Type' => 'application/vnd.ms-excel',
'Content-Disposition' => "attachment; filename='Report.xls'"
]);
}
The View being made into the Excel file is a simple table - the same table as in the screenshots. Here's the template/HTML of the View:
<div class="container full-width-container">
<link href="http://192.168.33.10/css/reports.css" rel="stylesheet">
<div id="results" class="row">
<div class="col-xs-12">
<h2 class="report-title">Active Products </h2>
<br>
<div class="row">
<div class="col-xs-12">
<div class="table-responsive report-component" name="table" template="table">
<table id="report-table" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<td class="report-table-th">ENGINE</td>
<td class="report-table-th">PRODUCT COUNT</td>
<td class="report-table-th">PRODUCT PRICE</td>
</tr>
</thead>
<tbody>
<tr class="results-cell">
<td class="report-table-cell">
<p>Meows</p>
</td>
<td class="report-table-cell">
<p>1,234</p>
</td>
<td class="report-table-cell">
<p>781230.00</p>
</td>
</tr>
</tbody>
<tbody>
<tr class="results-cell">
<td class="report-table-cell">
<p>Paws</p>
</td>
<td class="report-table-cell">
<p>10,777</p>
</td>
<td class="report-table-cell">
<p>3919823.00</p>
</td>
</tr>
</tbody>
<tbody>
<tr class="results-cell">
<td class="report-table-cell">
<p>Meow Mobile</p>
</td>
<td class="report-table-cell">
<p>177</p>
</td>
<td class="report-table-cell">
<p>334323.00</p>
</td>
</tr>
</tbody>
<tbody>
<tr class="results-cell">
<td class="report-table-cell">
<p>Tails Cloud</p>
</td>
<td class="report-table-cell">
<p>335</p>
</td>
<td class="report-table-cell">
<p>378918923.00</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
Does anyone see what I might be missing to download an XLS file?
Thanks for your time!
EDIT
After doing some more research, it seems that my original workflow for downloading -- using an AJAX call to trigger -- is part of the problem. For example Download a file from Servlet using Ajax talks about AJAX storing results in JavaScript memory, which would explain why I get content in my console and not a file.
Try to add code below before Excel::create():
ob_end_clean();
ob_start();
Example code:
ob_end_clean();
ob_start();
Excel::create($filename, function($excel) use($records, $sheetname, $data) {}
This works for me.