To display selected value from autocomplete on the table
I am using autocomplete
and table
component on the same page/component
.Here on selecting particular player
from the list of autocomplete component i want display that selected player details(for ex NAME
and AGE
) on the table like this :
I got some examples to display the selected value on the input field. Unable find it for table component.
Stackblitz DEMO
angular angular-material angular6
add a comment |
I am using autocomplete
and table
component on the same page/component
.Here on selecting particular player
from the list of autocomplete component i want display that selected player details(for ex NAME
and AGE
) on the table like this :
I got some examples to display the selected value on the input field. Unable find it for table component.
Stackblitz DEMO
angular angular-material angular6
Give my answer a try to check if that's what you're looking for.
– SiddAjmera
Nov 23 '18 at 9:19
add a comment |
I am using autocomplete
and table
component on the same page/component
.Here on selecting particular player
from the list of autocomplete component i want display that selected player details(for ex NAME
and AGE
) on the table like this :
I got some examples to display the selected value on the input field. Unable find it for table component.
Stackblitz DEMO
angular angular-material angular6
I am using autocomplete
and table
component on the same page/component
.Here on selecting particular player
from the list of autocomplete component i want display that selected player details(for ex NAME
and AGE
) on the table like this :
I got some examples to display the selected value on the input field. Unable find it for table component.
Stackblitz DEMO
angular angular-material angular6
angular angular-material angular6
edited Nov 23 '18 at 10:25
PGH
asked Nov 23 '18 at 9:07
PGHPGH
453418
453418
Give my answer a try to check if that's what you're looking for.
– SiddAjmera
Nov 23 '18 at 9:19
add a comment |
Give my answer a try to check if that's what you're looking for.
– SiddAjmera
Nov 23 '18 at 9:19
Give my answer a try to check if that's what you're looking for.
– SiddAjmera
Nov 23 '18 at 9:19
Give my answer a try to check if that's what you're looking for.
– SiddAjmera
Nov 23 '18 at 9:19
add a comment |
3 Answers
3
active
oldest
votes
There are a few issues with your current implementation:
- You'll have to work with
ELEMENT_DATA
to filter the results out instead of theoptions
. - You will have to reassign to
dataSource
the fileted array of results. That's something that I've done in the_filter
method. But since it returnedstring
and I was filtering onELEMENT_DATA
I changed its return type toPeriodicElement
. - Also, you're using
options
for the AutoComplete Suggestions instead of which you can simply usefilteredOptions
and then useoption.name
in themat-option
with the string interpolation syntax({{}}
)
Give this a try:
import {Component, OnInit, ViewChild} from '@angular/core';
import {FormControl} from '@angular/forms';
import {Observable} from 'rxjs';
import {map, startWith} from 'rxjs/operators';
import {MatSort,MatPaginator, MatTableDataSource} from '@angular/material';
export interface PeriodicElement {
name: string;
age: number;
}
const ELEMENT_DATA: PeriodicElement = [
{ name: 'Sachin Tendulkar', age: 42, },
{ name: 'Virat Kohli', age: 30},
];
@Component({
selector: 'autocomplete-filter-example',
templateUrl: 'autocomplete-filter-example.html',
styleUrls: ['autocomplete-filter-example.css'],
})
export class AutocompleteFilterExample implements OnInit {
displayedColumns: string = [ 'name', 'age'];
dataSource = new MatTableDataSource(ELEMENT_DATA);
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatPaginator) paginator: MatPaginator;
myControl = new FormControl();
options: string = ['Sachin Tendulkar', 'Virat Kohli', 'Rohith Sharma'];
filteredOptions: Observable<PeriodicElement>;
ngOnInit() {
this.filteredOptions = this.myControl.valueChanges
.pipe(
startWith(''),
map(value => this._filter(value))
);
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
}
applyFilter(filterValue: string) {
this.dataSource.filter = filterValue.trim().toLowerCase();
}
private _filter(value: string): PeriodicElement {
const filterValue = value.toLowerCase();
const filteredSet = ELEMENT_DATA.filter(option => option.name.toLowerCase().includes(filterValue));
this.dataSource = new MatTableDataSource(filteredSet);
return filteredSet;
}
}
/** Copyright 2018 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 */
And in your template:
<form class="example-form">
<mat-form-field class="example-full-width">
<input type="text" placeholder="Pick Player" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option">
{{option.name}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
<!-- <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">
<!-- NAME Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header> NAME </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<!-- AGE Column -->
<ng-container matColumnDef="age">
<th mat-header-cell *matHeaderCellDef mat-sort-header> AGE </th>
<td mat-cell *matCellDef="let element"> {{element.age}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
Here's an Updated and Working StackBlitz for your ref.
Sorry for the late reply, I just hardcoded the data in table, actually the data as to be in theautocomplete
component. If select from autocomplete list(for ex Sachin). I want display sachin's details on the table.
– PGH
Nov 23 '18 at 9:22
Isn't that what's happening on the Updated stackBlitz?
– SiddAjmera
Nov 23 '18 at 9:24
In yoursstackblitz example
the hard coded data is still present,
– PGH
Nov 23 '18 at 9:25
By default the table will be empty.Only on selecting from the list(i,e autocomplete) the table row will be added.
– PGH
Nov 23 '18 at 9:26
add a comment |
Bind keydown
event on input field as (keydown)="handlekeypressEvent($event.target.value)"
,
Change the table data according to keydownevent value.
<input type="text" placeholder="Pick Player"
(keydown)="handlekeypressEvent($event.target.value)" aria-label="Number"
matInput [formControl]="myControl" [matAutocomplete]="auto">
handlekeypressEvent($event) {
console.log($event);
this.dataSource = new MatTableDataSource(ELEMENT_DATA.filter(e=>
e.name.toLowerCase().includes($event.toLowerCase())));
}
Stackblitz Demo
add a comment |
Edit:
Will this work?
Updated And Reworked!
dataSource = new MatTableDataSource();
This will make sure your table is empty initially.
Add this in ngOnInit function
this.myControl.valueChanges
.subscribe(v => {
if (!v) {
this.dataSource = new MatTableDataSource(ELEMENT_DATA);
return;
}
const newDataSource = ELEMENT_DATA.filter(ob => ob.name === v);
this.dataSource = new MatTableDataSource(newDataSource);
});
And when you change the value from dropdown, this will populate the table.
P.S.:
Let me know, if I am still not getting your goal.
Please see my question once..:)
– PGH
Nov 23 '18 at 10:21
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443548%2fto-display-selected-value-from-autocomplete-on-the-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are a few issues with your current implementation:
- You'll have to work with
ELEMENT_DATA
to filter the results out instead of theoptions
. - You will have to reassign to
dataSource
the fileted array of results. That's something that I've done in the_filter
method. But since it returnedstring
and I was filtering onELEMENT_DATA
I changed its return type toPeriodicElement
. - Also, you're using
options
for the AutoComplete Suggestions instead of which you can simply usefilteredOptions
and then useoption.name
in themat-option
with the string interpolation syntax({{}}
)
Give this a try:
import {Component, OnInit, ViewChild} from '@angular/core';
import {FormControl} from '@angular/forms';
import {Observable} from 'rxjs';
import {map, startWith} from 'rxjs/operators';
import {MatSort,MatPaginator, MatTableDataSource} from '@angular/material';
export interface PeriodicElement {
name: string;
age: number;
}
const ELEMENT_DATA: PeriodicElement = [
{ name: 'Sachin Tendulkar', age: 42, },
{ name: 'Virat Kohli', age: 30},
];
@Component({
selector: 'autocomplete-filter-example',
templateUrl: 'autocomplete-filter-example.html',
styleUrls: ['autocomplete-filter-example.css'],
})
export class AutocompleteFilterExample implements OnInit {
displayedColumns: string = [ 'name', 'age'];
dataSource = new MatTableDataSource(ELEMENT_DATA);
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatPaginator) paginator: MatPaginator;
myControl = new FormControl();
options: string = ['Sachin Tendulkar', 'Virat Kohli', 'Rohith Sharma'];
filteredOptions: Observable<PeriodicElement>;
ngOnInit() {
this.filteredOptions = this.myControl.valueChanges
.pipe(
startWith(''),
map(value => this._filter(value))
);
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
}
applyFilter(filterValue: string) {
this.dataSource.filter = filterValue.trim().toLowerCase();
}
private _filter(value: string): PeriodicElement {
const filterValue = value.toLowerCase();
const filteredSet = ELEMENT_DATA.filter(option => option.name.toLowerCase().includes(filterValue));
this.dataSource = new MatTableDataSource(filteredSet);
return filteredSet;
}
}
/** Copyright 2018 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 */
And in your template:
<form class="example-form">
<mat-form-field class="example-full-width">
<input type="text" placeholder="Pick Player" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option">
{{option.name}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
<!-- <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">
<!-- NAME Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header> NAME </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<!-- AGE Column -->
<ng-container matColumnDef="age">
<th mat-header-cell *matHeaderCellDef mat-sort-header> AGE </th>
<td mat-cell *matCellDef="let element"> {{element.age}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
Here's an Updated and Working StackBlitz for your ref.
Sorry for the late reply, I just hardcoded the data in table, actually the data as to be in theautocomplete
component. If select from autocomplete list(for ex Sachin). I want display sachin's details on the table.
– PGH
Nov 23 '18 at 9:22
Isn't that what's happening on the Updated stackBlitz?
– SiddAjmera
Nov 23 '18 at 9:24
In yoursstackblitz example
the hard coded data is still present,
– PGH
Nov 23 '18 at 9:25
By default the table will be empty.Only on selecting from the list(i,e autocomplete) the table row will be added.
– PGH
Nov 23 '18 at 9:26
add a comment |
There are a few issues with your current implementation:
- You'll have to work with
ELEMENT_DATA
to filter the results out instead of theoptions
. - You will have to reassign to
dataSource
the fileted array of results. That's something that I've done in the_filter
method. But since it returnedstring
and I was filtering onELEMENT_DATA
I changed its return type toPeriodicElement
. - Also, you're using
options
for the AutoComplete Suggestions instead of which you can simply usefilteredOptions
and then useoption.name
in themat-option
with the string interpolation syntax({{}}
)
Give this a try:
import {Component, OnInit, ViewChild} from '@angular/core';
import {FormControl} from '@angular/forms';
import {Observable} from 'rxjs';
import {map, startWith} from 'rxjs/operators';
import {MatSort,MatPaginator, MatTableDataSource} from '@angular/material';
export interface PeriodicElement {
name: string;
age: number;
}
const ELEMENT_DATA: PeriodicElement = [
{ name: 'Sachin Tendulkar', age: 42, },
{ name: 'Virat Kohli', age: 30},
];
@Component({
selector: 'autocomplete-filter-example',
templateUrl: 'autocomplete-filter-example.html',
styleUrls: ['autocomplete-filter-example.css'],
})
export class AutocompleteFilterExample implements OnInit {
displayedColumns: string = [ 'name', 'age'];
dataSource = new MatTableDataSource(ELEMENT_DATA);
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatPaginator) paginator: MatPaginator;
myControl = new FormControl();
options: string = ['Sachin Tendulkar', 'Virat Kohli', 'Rohith Sharma'];
filteredOptions: Observable<PeriodicElement>;
ngOnInit() {
this.filteredOptions = this.myControl.valueChanges
.pipe(
startWith(''),
map(value => this._filter(value))
);
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
}
applyFilter(filterValue: string) {
this.dataSource.filter = filterValue.trim().toLowerCase();
}
private _filter(value: string): PeriodicElement {
const filterValue = value.toLowerCase();
const filteredSet = ELEMENT_DATA.filter(option => option.name.toLowerCase().includes(filterValue));
this.dataSource = new MatTableDataSource(filteredSet);
return filteredSet;
}
}
/** Copyright 2018 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 */
And in your template:
<form class="example-form">
<mat-form-field class="example-full-width">
<input type="text" placeholder="Pick Player" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option">
{{option.name}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
<!-- <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">
<!-- NAME Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header> NAME </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<!-- AGE Column -->
<ng-container matColumnDef="age">
<th mat-header-cell *matHeaderCellDef mat-sort-header> AGE </th>
<td mat-cell *matCellDef="let element"> {{element.age}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
Here's an Updated and Working StackBlitz for your ref.
Sorry for the late reply, I just hardcoded the data in table, actually the data as to be in theautocomplete
component. If select from autocomplete list(for ex Sachin). I want display sachin's details on the table.
– PGH
Nov 23 '18 at 9:22
Isn't that what's happening on the Updated stackBlitz?
– SiddAjmera
Nov 23 '18 at 9:24
In yoursstackblitz example
the hard coded data is still present,
– PGH
Nov 23 '18 at 9:25
By default the table will be empty.Only on selecting from the list(i,e autocomplete) the table row will be added.
– PGH
Nov 23 '18 at 9:26
add a comment |
There are a few issues with your current implementation:
- You'll have to work with
ELEMENT_DATA
to filter the results out instead of theoptions
. - You will have to reassign to
dataSource
the fileted array of results. That's something that I've done in the_filter
method. But since it returnedstring
and I was filtering onELEMENT_DATA
I changed its return type toPeriodicElement
. - Also, you're using
options
for the AutoComplete Suggestions instead of which you can simply usefilteredOptions
and then useoption.name
in themat-option
with the string interpolation syntax({{}}
)
Give this a try:
import {Component, OnInit, ViewChild} from '@angular/core';
import {FormControl} from '@angular/forms';
import {Observable} from 'rxjs';
import {map, startWith} from 'rxjs/operators';
import {MatSort,MatPaginator, MatTableDataSource} from '@angular/material';
export interface PeriodicElement {
name: string;
age: number;
}
const ELEMENT_DATA: PeriodicElement = [
{ name: 'Sachin Tendulkar', age: 42, },
{ name: 'Virat Kohli', age: 30},
];
@Component({
selector: 'autocomplete-filter-example',
templateUrl: 'autocomplete-filter-example.html',
styleUrls: ['autocomplete-filter-example.css'],
})
export class AutocompleteFilterExample implements OnInit {
displayedColumns: string = [ 'name', 'age'];
dataSource = new MatTableDataSource(ELEMENT_DATA);
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatPaginator) paginator: MatPaginator;
myControl = new FormControl();
options: string = ['Sachin Tendulkar', 'Virat Kohli', 'Rohith Sharma'];
filteredOptions: Observable<PeriodicElement>;
ngOnInit() {
this.filteredOptions = this.myControl.valueChanges
.pipe(
startWith(''),
map(value => this._filter(value))
);
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
}
applyFilter(filterValue: string) {
this.dataSource.filter = filterValue.trim().toLowerCase();
}
private _filter(value: string): PeriodicElement {
const filterValue = value.toLowerCase();
const filteredSet = ELEMENT_DATA.filter(option => option.name.toLowerCase().includes(filterValue));
this.dataSource = new MatTableDataSource(filteredSet);
return filteredSet;
}
}
/** Copyright 2018 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 */
And in your template:
<form class="example-form">
<mat-form-field class="example-full-width">
<input type="text" placeholder="Pick Player" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option">
{{option.name}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
<!-- <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">
<!-- NAME Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header> NAME </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<!-- AGE Column -->
<ng-container matColumnDef="age">
<th mat-header-cell *matHeaderCellDef mat-sort-header> AGE </th>
<td mat-cell *matCellDef="let element"> {{element.age}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
Here's an Updated and Working StackBlitz for your ref.
There are a few issues with your current implementation:
- You'll have to work with
ELEMENT_DATA
to filter the results out instead of theoptions
. - You will have to reassign to
dataSource
the fileted array of results. That's something that I've done in the_filter
method. But since it returnedstring
and I was filtering onELEMENT_DATA
I changed its return type toPeriodicElement
. - Also, you're using
options
for the AutoComplete Suggestions instead of which you can simply usefilteredOptions
and then useoption.name
in themat-option
with the string interpolation syntax({{}}
)
Give this a try:
import {Component, OnInit, ViewChild} from '@angular/core';
import {FormControl} from '@angular/forms';
import {Observable} from 'rxjs';
import {map, startWith} from 'rxjs/operators';
import {MatSort,MatPaginator, MatTableDataSource} from '@angular/material';
export interface PeriodicElement {
name: string;
age: number;
}
const ELEMENT_DATA: PeriodicElement = [
{ name: 'Sachin Tendulkar', age: 42, },
{ name: 'Virat Kohli', age: 30},
];
@Component({
selector: 'autocomplete-filter-example',
templateUrl: 'autocomplete-filter-example.html',
styleUrls: ['autocomplete-filter-example.css'],
})
export class AutocompleteFilterExample implements OnInit {
displayedColumns: string = [ 'name', 'age'];
dataSource = new MatTableDataSource(ELEMENT_DATA);
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatPaginator) paginator: MatPaginator;
myControl = new FormControl();
options: string = ['Sachin Tendulkar', 'Virat Kohli', 'Rohith Sharma'];
filteredOptions: Observable<PeriodicElement>;
ngOnInit() {
this.filteredOptions = this.myControl.valueChanges
.pipe(
startWith(''),
map(value => this._filter(value))
);
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
}
applyFilter(filterValue: string) {
this.dataSource.filter = filterValue.trim().toLowerCase();
}
private _filter(value: string): PeriodicElement {
const filterValue = value.toLowerCase();
const filteredSet = ELEMENT_DATA.filter(option => option.name.toLowerCase().includes(filterValue));
this.dataSource = new MatTableDataSource(filteredSet);
return filteredSet;
}
}
/** Copyright 2018 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 */
And in your template:
<form class="example-form">
<mat-form-field class="example-full-width">
<input type="text" placeholder="Pick Player" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option">
{{option.name}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
<!-- <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">
<!-- NAME Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header> NAME </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<!-- AGE Column -->
<ng-container matColumnDef="age">
<th mat-header-cell *matHeaderCellDef mat-sort-header> AGE </th>
<td mat-cell *matCellDef="let element"> {{element.age}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
Here's an Updated and Working StackBlitz for your ref.
edited Nov 23 '18 at 9:19
answered Nov 23 '18 at 9:13
SiddAjmeraSiddAjmera
16k31239
16k31239
Sorry for the late reply, I just hardcoded the data in table, actually the data as to be in theautocomplete
component. If select from autocomplete list(for ex Sachin). I want display sachin's details on the table.
– PGH
Nov 23 '18 at 9:22
Isn't that what's happening on the Updated stackBlitz?
– SiddAjmera
Nov 23 '18 at 9:24
In yoursstackblitz example
the hard coded data is still present,
– PGH
Nov 23 '18 at 9:25
By default the table will be empty.Only on selecting from the list(i,e autocomplete) the table row will be added.
– PGH
Nov 23 '18 at 9:26
add a comment |
Sorry for the late reply, I just hardcoded the data in table, actually the data as to be in theautocomplete
component. If select from autocomplete list(for ex Sachin). I want display sachin's details on the table.
– PGH
Nov 23 '18 at 9:22
Isn't that what's happening on the Updated stackBlitz?
– SiddAjmera
Nov 23 '18 at 9:24
In yoursstackblitz example
the hard coded data is still present,
– PGH
Nov 23 '18 at 9:25
By default the table will be empty.Only on selecting from the list(i,e autocomplete) the table row will be added.
– PGH
Nov 23 '18 at 9:26
Sorry for the late reply, I just hardcoded the data in table, actually the data as to be in the
autocomplete
component. If select from autocomplete list(for ex Sachin). I want display sachin's details on the table.– PGH
Nov 23 '18 at 9:22
Sorry for the late reply, I just hardcoded the data in table, actually the data as to be in the
autocomplete
component. If select from autocomplete list(for ex Sachin). I want display sachin's details on the table.– PGH
Nov 23 '18 at 9:22
Isn't that what's happening on the Updated stackBlitz?
– SiddAjmera
Nov 23 '18 at 9:24
Isn't that what's happening on the Updated stackBlitz?
– SiddAjmera
Nov 23 '18 at 9:24
In yours
stackblitz example
the hard coded data is still present,– PGH
Nov 23 '18 at 9:25
In yours
stackblitz example
the hard coded data is still present,– PGH
Nov 23 '18 at 9:25
By default the table will be empty.Only on selecting from the list(i,e autocomplete) the table row will be added.
– PGH
Nov 23 '18 at 9:26
By default the table will be empty.Only on selecting from the list(i,e autocomplete) the table row will be added.
– PGH
Nov 23 '18 at 9:26
add a comment |
Bind keydown
event on input field as (keydown)="handlekeypressEvent($event.target.value)"
,
Change the table data according to keydownevent value.
<input type="text" placeholder="Pick Player"
(keydown)="handlekeypressEvent($event.target.value)" aria-label="Number"
matInput [formControl]="myControl" [matAutocomplete]="auto">
handlekeypressEvent($event) {
console.log($event);
this.dataSource = new MatTableDataSource(ELEMENT_DATA.filter(e=>
e.name.toLowerCase().includes($event.toLowerCase())));
}
Stackblitz Demo
add a comment |
Bind keydown
event on input field as (keydown)="handlekeypressEvent($event.target.value)"
,
Change the table data according to keydownevent value.
<input type="text" placeholder="Pick Player"
(keydown)="handlekeypressEvent($event.target.value)" aria-label="Number"
matInput [formControl]="myControl" [matAutocomplete]="auto">
handlekeypressEvent($event) {
console.log($event);
this.dataSource = new MatTableDataSource(ELEMENT_DATA.filter(e=>
e.name.toLowerCase().includes($event.toLowerCase())));
}
Stackblitz Demo
add a comment |
Bind keydown
event on input field as (keydown)="handlekeypressEvent($event.target.value)"
,
Change the table data according to keydownevent value.
<input type="text" placeholder="Pick Player"
(keydown)="handlekeypressEvent($event.target.value)" aria-label="Number"
matInput [formControl]="myControl" [matAutocomplete]="auto">
handlekeypressEvent($event) {
console.log($event);
this.dataSource = new MatTableDataSource(ELEMENT_DATA.filter(e=>
e.name.toLowerCase().includes($event.toLowerCase())));
}
Stackblitz Demo
Bind keydown
event on input field as (keydown)="handlekeypressEvent($event.target.value)"
,
Change the table data according to keydownevent value.
<input type="text" placeholder="Pick Player"
(keydown)="handlekeypressEvent($event.target.value)" aria-label="Number"
matInput [formControl]="myControl" [matAutocomplete]="auto">
handlekeypressEvent($event) {
console.log($event);
this.dataSource = new MatTableDataSource(ELEMENT_DATA.filter(e=>
e.name.toLowerCase().includes($event.toLowerCase())));
}
Stackblitz Demo
edited Nov 23 '18 at 10:25
answered Nov 23 '18 at 9:24
Abhishek KumarAbhishek Kumar
1,455417
1,455417
add a comment |
add a comment |
Edit:
Will this work?
Updated And Reworked!
dataSource = new MatTableDataSource();
This will make sure your table is empty initially.
Add this in ngOnInit function
this.myControl.valueChanges
.subscribe(v => {
if (!v) {
this.dataSource = new MatTableDataSource(ELEMENT_DATA);
return;
}
const newDataSource = ELEMENT_DATA.filter(ob => ob.name === v);
this.dataSource = new MatTableDataSource(newDataSource);
});
And when you change the value from dropdown, this will populate the table.
P.S.:
Let me know, if I am still not getting your goal.
Please see my question once..:)
– PGH
Nov 23 '18 at 10:21
add a comment |
Edit:
Will this work?
Updated And Reworked!
dataSource = new MatTableDataSource();
This will make sure your table is empty initially.
Add this in ngOnInit function
this.myControl.valueChanges
.subscribe(v => {
if (!v) {
this.dataSource = new MatTableDataSource(ELEMENT_DATA);
return;
}
const newDataSource = ELEMENT_DATA.filter(ob => ob.name === v);
this.dataSource = new MatTableDataSource(newDataSource);
});
And when you change the value from dropdown, this will populate the table.
P.S.:
Let me know, if I am still not getting your goal.
Please see my question once..:)
– PGH
Nov 23 '18 at 10:21
add a comment |
Edit:
Will this work?
Updated And Reworked!
dataSource = new MatTableDataSource();
This will make sure your table is empty initially.
Add this in ngOnInit function
this.myControl.valueChanges
.subscribe(v => {
if (!v) {
this.dataSource = new MatTableDataSource(ELEMENT_DATA);
return;
}
const newDataSource = ELEMENT_DATA.filter(ob => ob.name === v);
this.dataSource = new MatTableDataSource(newDataSource);
});
And when you change the value from dropdown, this will populate the table.
P.S.:
Let me know, if I am still not getting your goal.
Edit:
Will this work?
Updated And Reworked!
dataSource = new MatTableDataSource();
This will make sure your table is empty initially.
Add this in ngOnInit function
this.myControl.valueChanges
.subscribe(v => {
if (!v) {
this.dataSource = new MatTableDataSource(ELEMENT_DATA);
return;
}
const newDataSource = ELEMENT_DATA.filter(ob => ob.name === v);
this.dataSource = new MatTableDataSource(newDataSource);
});
And when you change the value from dropdown, this will populate the table.
P.S.:
Let me know, if I am still not getting your goal.
edited Nov 26 '18 at 9:32
answered Nov 23 '18 at 9:33
Akash PanigrahiAkash Panigrahi
714
714
Please see my question once..:)
– PGH
Nov 23 '18 at 10:21
add a comment |
Please see my question once..:)
– PGH
Nov 23 '18 at 10:21
Please see my question once..:)
– PGH
Nov 23 '18 at 10:21
Please see my question once..:)
– PGH
Nov 23 '18 at 10:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443548%2fto-display-selected-value-from-autocomplete-on-the-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Give my answer a try to check if that's what you're looking for.
– SiddAjmera
Nov 23 '18 at 9:19