General search pipe Angular6
up vote
3
down vote
favorite
How can I make a general search into a table using pipes in Angular? I have this working snippet, but the value of i
is only 0
, and I don't know why (and because of that, it will only search for name
column only, if I type cc
it won't find anything).
How can I modify the code in order to obtain what I want? Thank you for your time!
filter.pipe.ts
transform(value: any, input: string) {
if (!value) {
return ;
}
if (!input) {
return value;
}
var valuesArr =
if (input) {
input = input.toLowerCase();
return value.filter(function (el: any) {
valuesArr = Object.keys(el).map(key => el[key]);
for (var i in valuesArr) {
return valuesArr != null && valuesArr[i] != null && valuesArr[i] != undefined && valuesArr[i].toLowerCase().indexOf(input) > -1;
}
})
}
return value;
}
app.html
<input type="text" [(ngModel)]="toSearch" (click)="onClick(toSearch)">
<table>
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Group</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of users | filter : toSearch">
<td>{{ user.name }}</td>
<td>{{ user.surname }}</td>
<td>{{ user.group }}</td>
<tr>
</tbody>
</table>
app.ts
toSearch
users = [
{name: "john", surname: "john1", group: null},
{name: "mike", surname: "", group: 'bb'},
{name: "anne", surname: "anne1", group: ''},
{name: "dan", surname: "dan1", group: 'cc'},
{name: "ben", surname: "", group: 'a'},
]
onClick(toSearch) {
this.toSearch = toSearch
}
javascript angular filter
add a comment |
up vote
3
down vote
favorite
How can I make a general search into a table using pipes in Angular? I have this working snippet, but the value of i
is only 0
, and I don't know why (and because of that, it will only search for name
column only, if I type cc
it won't find anything).
How can I modify the code in order to obtain what I want? Thank you for your time!
filter.pipe.ts
transform(value: any, input: string) {
if (!value) {
return ;
}
if (!input) {
return value;
}
var valuesArr =
if (input) {
input = input.toLowerCase();
return value.filter(function (el: any) {
valuesArr = Object.keys(el).map(key => el[key]);
for (var i in valuesArr) {
return valuesArr != null && valuesArr[i] != null && valuesArr[i] != undefined && valuesArr[i].toLowerCase().indexOf(input) > -1;
}
})
}
return value;
}
app.html
<input type="text" [(ngModel)]="toSearch" (click)="onClick(toSearch)">
<table>
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Group</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of users | filter : toSearch">
<td>{{ user.name }}</td>
<td>{{ user.surname }}</td>
<td>{{ user.group }}</td>
<tr>
</tbody>
</table>
app.ts
toSearch
users = [
{name: "john", surname: "john1", group: null},
{name: "mike", surname: "", group: 'bb'},
{name: "anne", surname: "anne1", group: ''},
{name: "dan", surname: "dan1", group: 'cc'},
{name: "ben", surname: "", group: 'a'},
]
onClick(toSearch) {
this.toSearch = toSearch
}
javascript angular filter
1
While the right answers can be found below. Thought i'd offer a few small changes you could possibly make to clean up the code. First would be to removeif (input) {
statement, at this point in the code its guaranteed to be truthy as you just checked!input
. Secondly you can changeObject.keys(el).map(key => el[key]);
toObject.values(el)
.
– Michael Lynch
Nov 19 at 14:02
Thank you for your tips, I changed my code as you said! :-D
– Tenzolinho
Nov 19 at 14:10
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
How can I make a general search into a table using pipes in Angular? I have this working snippet, but the value of i
is only 0
, and I don't know why (and because of that, it will only search for name
column only, if I type cc
it won't find anything).
How can I modify the code in order to obtain what I want? Thank you for your time!
filter.pipe.ts
transform(value: any, input: string) {
if (!value) {
return ;
}
if (!input) {
return value;
}
var valuesArr =
if (input) {
input = input.toLowerCase();
return value.filter(function (el: any) {
valuesArr = Object.keys(el).map(key => el[key]);
for (var i in valuesArr) {
return valuesArr != null && valuesArr[i] != null && valuesArr[i] != undefined && valuesArr[i].toLowerCase().indexOf(input) > -1;
}
})
}
return value;
}
app.html
<input type="text" [(ngModel)]="toSearch" (click)="onClick(toSearch)">
<table>
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Group</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of users | filter : toSearch">
<td>{{ user.name }}</td>
<td>{{ user.surname }}</td>
<td>{{ user.group }}</td>
<tr>
</tbody>
</table>
app.ts
toSearch
users = [
{name: "john", surname: "john1", group: null},
{name: "mike", surname: "", group: 'bb'},
{name: "anne", surname: "anne1", group: ''},
{name: "dan", surname: "dan1", group: 'cc'},
{name: "ben", surname: "", group: 'a'},
]
onClick(toSearch) {
this.toSearch = toSearch
}
javascript angular filter
How can I make a general search into a table using pipes in Angular? I have this working snippet, but the value of i
is only 0
, and I don't know why (and because of that, it will only search for name
column only, if I type cc
it won't find anything).
How can I modify the code in order to obtain what I want? Thank you for your time!
filter.pipe.ts
transform(value: any, input: string) {
if (!value) {
return ;
}
if (!input) {
return value;
}
var valuesArr =
if (input) {
input = input.toLowerCase();
return value.filter(function (el: any) {
valuesArr = Object.keys(el).map(key => el[key]);
for (var i in valuesArr) {
return valuesArr != null && valuesArr[i] != null && valuesArr[i] != undefined && valuesArr[i].toLowerCase().indexOf(input) > -1;
}
})
}
return value;
}
app.html
<input type="text" [(ngModel)]="toSearch" (click)="onClick(toSearch)">
<table>
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Group</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of users | filter : toSearch">
<td>{{ user.name }}</td>
<td>{{ user.surname }}</td>
<td>{{ user.group }}</td>
<tr>
</tbody>
</table>
app.ts
toSearch
users = [
{name: "john", surname: "john1", group: null},
{name: "mike", surname: "", group: 'bb'},
{name: "anne", surname: "anne1", group: ''},
{name: "dan", surname: "dan1", group: 'cc'},
{name: "ben", surname: "", group: 'a'},
]
onClick(toSearch) {
this.toSearch = toSearch
}
javascript angular filter
javascript angular filter
edited Nov 19 at 13:29
asked Nov 19 at 13:16
Tenzolinho
22110
22110
1
While the right answers can be found below. Thought i'd offer a few small changes you could possibly make to clean up the code. First would be to removeif (input) {
statement, at this point in the code its guaranteed to be truthy as you just checked!input
. Secondly you can changeObject.keys(el).map(key => el[key]);
toObject.values(el)
.
– Michael Lynch
Nov 19 at 14:02
Thank you for your tips, I changed my code as you said! :-D
– Tenzolinho
Nov 19 at 14:10
add a comment |
1
While the right answers can be found below. Thought i'd offer a few small changes you could possibly make to clean up the code. First would be to removeif (input) {
statement, at this point in the code its guaranteed to be truthy as you just checked!input
. Secondly you can changeObject.keys(el).map(key => el[key]);
toObject.values(el)
.
– Michael Lynch
Nov 19 at 14:02
Thank you for your tips, I changed my code as you said! :-D
– Tenzolinho
Nov 19 at 14:10
1
1
While the right answers can be found below. Thought i'd offer a few small changes you could possibly make to clean up the code. First would be to remove
if (input) {
statement, at this point in the code its guaranteed to be truthy as you just checked !input
. Secondly you can change Object.keys(el).map(key => el[key]);
to Object.values(el)
.– Michael Lynch
Nov 19 at 14:02
While the right answers can be found below. Thought i'd offer a few small changes you could possibly make to clean up the code. First would be to remove
if (input) {
statement, at this point in the code its guaranteed to be truthy as you just checked !input
. Secondly you can change Object.keys(el).map(key => el[key]);
to Object.values(el)
.– Michael Lynch
Nov 19 at 14:02
Thank you for your tips, I changed my code as you said! :-D
– Tenzolinho
Nov 19 at 14:10
Thank you for your tips, I changed my code as you said! :-D
– Tenzolinho
Nov 19 at 14:10
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
Simple just check when your match is true only then send it through
import { Pipe, PipeTransform, Injectable } from '@angular/core';
@Pipe({
name: 'filter',
})
@Injectable()
export class CheckBoxPipe implements PipeTransform {
transform(value: any, input: string) {
if (!value) {
return ;
}
if (!input) {
return value;
}
var values =
if (input) {
input = input.toLowerCase();
return value.filter(function (el: any) {
values = Object.keys(el).map(key => el[key]);
let result = false;
for (var i in values) {
result = values != null && values[i] != null && values[i] != undefined && values[i].toLowerCase().indexOf(input) > -1;
if(result){
return true
}
}
return false;
})
}
return value;
}
}
Aah, I see what I did wrong, thank you very much!
– Tenzolinho
Nov 19 at 13:32
add a comment |
up vote
1
down vote
Using ES6 operators I suggest also :
transform(value: any, input: string) {
if (!value) {
return ;
}
if (!input) {
return value;
}
var valuesArr =
if (input) {
console.log(input)
input = input.toLowerCase();
return value.filter( (el: any) => {
valuesArr = Object.keys(el).map(key => el[key]);
return valuesArr.some((elm)=> {
if (JSON.stringify(elm).includes(input)) {
return elm;
}
return null;
})
})
}
return value;
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Simple just check when your match is true only then send it through
import { Pipe, PipeTransform, Injectable } from '@angular/core';
@Pipe({
name: 'filter',
})
@Injectable()
export class CheckBoxPipe implements PipeTransform {
transform(value: any, input: string) {
if (!value) {
return ;
}
if (!input) {
return value;
}
var values =
if (input) {
input = input.toLowerCase();
return value.filter(function (el: any) {
values = Object.keys(el).map(key => el[key]);
let result = false;
for (var i in values) {
result = values != null && values[i] != null && values[i] != undefined && values[i].toLowerCase().indexOf(input) > -1;
if(result){
return true
}
}
return false;
})
}
return value;
}
}
Aah, I see what I did wrong, thank you very much!
– Tenzolinho
Nov 19 at 13:32
add a comment |
up vote
4
down vote
accepted
Simple just check when your match is true only then send it through
import { Pipe, PipeTransform, Injectable } from '@angular/core';
@Pipe({
name: 'filter',
})
@Injectable()
export class CheckBoxPipe implements PipeTransform {
transform(value: any, input: string) {
if (!value) {
return ;
}
if (!input) {
return value;
}
var values =
if (input) {
input = input.toLowerCase();
return value.filter(function (el: any) {
values = Object.keys(el).map(key => el[key]);
let result = false;
for (var i in values) {
result = values != null && values[i] != null && values[i] != undefined && values[i].toLowerCase().indexOf(input) > -1;
if(result){
return true
}
}
return false;
})
}
return value;
}
}
Aah, I see what I did wrong, thank you very much!
– Tenzolinho
Nov 19 at 13:32
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Simple just check when your match is true only then send it through
import { Pipe, PipeTransform, Injectable } from '@angular/core';
@Pipe({
name: 'filter',
})
@Injectable()
export class CheckBoxPipe implements PipeTransform {
transform(value: any, input: string) {
if (!value) {
return ;
}
if (!input) {
return value;
}
var values =
if (input) {
input = input.toLowerCase();
return value.filter(function (el: any) {
values = Object.keys(el).map(key => el[key]);
let result = false;
for (var i in values) {
result = values != null && values[i] != null && values[i] != undefined && values[i].toLowerCase().indexOf(input) > -1;
if(result){
return true
}
}
return false;
})
}
return value;
}
}
Simple just check when your match is true only then send it through
import { Pipe, PipeTransform, Injectable } from '@angular/core';
@Pipe({
name: 'filter',
})
@Injectable()
export class CheckBoxPipe implements PipeTransform {
transform(value: any, input: string) {
if (!value) {
return ;
}
if (!input) {
return value;
}
var values =
if (input) {
input = input.toLowerCase();
return value.filter(function (el: any) {
values = Object.keys(el).map(key => el[key]);
let result = false;
for (var i in values) {
result = values != null && values[i] != null && values[i] != undefined && values[i].toLowerCase().indexOf(input) > -1;
if(result){
return true
}
}
return false;
})
}
return value;
}
}
answered Nov 19 at 13:30
joyBlanks
3,64711035
3,64711035
Aah, I see what I did wrong, thank you very much!
– Tenzolinho
Nov 19 at 13:32
add a comment |
Aah, I see what I did wrong, thank you very much!
– Tenzolinho
Nov 19 at 13:32
Aah, I see what I did wrong, thank you very much!
– Tenzolinho
Nov 19 at 13:32
Aah, I see what I did wrong, thank you very much!
– Tenzolinho
Nov 19 at 13:32
add a comment |
up vote
1
down vote
Using ES6 operators I suggest also :
transform(value: any, input: string) {
if (!value) {
return ;
}
if (!input) {
return value;
}
var valuesArr =
if (input) {
console.log(input)
input = input.toLowerCase();
return value.filter( (el: any) => {
valuesArr = Object.keys(el).map(key => el[key]);
return valuesArr.some((elm)=> {
if (JSON.stringify(elm).includes(input)) {
return elm;
}
return null;
})
})
}
return value;
}
add a comment |
up vote
1
down vote
Using ES6 operators I suggest also :
transform(value: any, input: string) {
if (!value) {
return ;
}
if (!input) {
return value;
}
var valuesArr =
if (input) {
console.log(input)
input = input.toLowerCase();
return value.filter( (el: any) => {
valuesArr = Object.keys(el).map(key => el[key]);
return valuesArr.some((elm)=> {
if (JSON.stringify(elm).includes(input)) {
return elm;
}
return null;
})
})
}
return value;
}
add a comment |
up vote
1
down vote
up vote
1
down vote
Using ES6 operators I suggest also :
transform(value: any, input: string) {
if (!value) {
return ;
}
if (!input) {
return value;
}
var valuesArr =
if (input) {
console.log(input)
input = input.toLowerCase();
return value.filter( (el: any) => {
valuesArr = Object.keys(el).map(key => el[key]);
return valuesArr.some((elm)=> {
if (JSON.stringify(elm).includes(input)) {
return elm;
}
return null;
})
})
}
return value;
}
Using ES6 operators I suggest also :
transform(value: any, input: string) {
if (!value) {
return ;
}
if (!input) {
return value;
}
var valuesArr =
if (input) {
console.log(input)
input = input.toLowerCase();
return value.filter( (el: any) => {
valuesArr = Object.keys(el).map(key => el[key]);
return valuesArr.some((elm)=> {
if (JSON.stringify(elm).includes(input)) {
return elm;
}
return null;
})
})
}
return value;
}
answered Nov 19 at 13:43
selem mn
4,40541938
4,40541938
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53375475%2fgeneral-search-pipe-angular6%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
1
While the right answers can be found below. Thought i'd offer a few small changes you could possibly make to clean up the code. First would be to remove
if (input) {
statement, at this point in the code its guaranteed to be truthy as you just checked!input
. Secondly you can changeObject.keys(el).map(key => el[key]);
toObject.values(el)
.– Michael Lynch
Nov 19 at 14:02
Thank you for your tips, I changed my code as you said! :-D
– Tenzolinho
Nov 19 at 14:10