Filter by pills/buttons instead of using a select - angularWhen are you supposed to use escape instead of encodeURI / encodeURIComponent?How can I know which radio button is selected via jQuery?How can I select an element with multiple classes in jQuery?Get selected value in dropdown list using JavaScriptGet selected text from a drop-down list (select box) using jQuerylist comprehension vs. lambda + filterWhat is the difference between angular-route and angular-ui-router?How to use this pipe in angular 5 dynamically?Angular 5 - using filter in select tag with pipeAngular [scrollTop] causing unwanted behaviour
Resources to study quantum algorithms and quantum complexity
Does the PS4 still *beep* when coming out of Rest mode?
Keeping data in old Database (SQL 2008) identical to new Database (SQL 2016)?
In OOP, 'protected' keyword is not required?
Looking for a restaurant in Vienna with a name similar to "An die 2 Linsen" that served huge Schnitzel
Cherem ceremony: candles?
SQL Server: Multiple Availability Groups, one cluster, how do I keep the primary cluster node as primary?
Harmonic sums and elementary number theory
Could a technologically advanced society exist as a feudal monarchy?
Where to start with a child learning chess?
Can socialism and capitalism coexist in the same country?
Does an action-reaction pair always contain the same kind of force?
How to move directory into a directory with the same name?
Is Yoda made using CGI in the original Star Wars or is it practical effects?
Is there one website that contains all relevant DevOps news and gets updated on a daily basis?
Temporary queue to prevent duplication
Schemes/ Mechanisms that could provide one time decryption?
What's the difference between freedom and ''the freedom'' here?
How large should a hole be for a bolt to go through?
Do any countries have a procedure that allows a constituent part of that country to become independent unilaterally?
How to Taper a coil?
How true are Trump’s claims about NATO spending?
Is the "p" in "spin" really a "b"?
Possible bug in NMaximize function?
Filter by pills/buttons instead of using a select - angular
When are you supposed to use escape instead of encodeURI / encodeURIComponent?How can I know which radio button is selected via jQuery?How can I select an element with multiple classes in jQuery?Get selected value in dropdown list using JavaScriptGet selected text from a drop-down list (select box) using jQuerylist comprehension vs. lambda + filterWhat is the difference between angular-route and angular-ui-router?How to use this pipe in angular 5 dynamically?Angular 5 - using filter in select tag with pipeAngular [scrollTop] causing unwanted behaviour
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
I'm building with Angular and have used a filter pipe to filter on the selected option from my dropdown on the *ngFor loop. The content then filters accordingly. I want to swap the select options to buttons or pills. So when a button is clicked the filter takes place - the button will act as a on/off switch so you can have more than one option filtered.
Here is my stackblitz example - https://stackblitz.com/edit/timeline-angular-7-tyle1f
<div class="form-group row">
<div class="col-sm">
<select class="form-control" name="locationFilter" id="locationFilter" [(ngModel)]="filteredLocation">
<option value="All">All</option>
<option *ngFor="let entry of timeLine | filterUnique" value="entry.location">entry.location
</option>
</select>
</div>
//Button filters below
<div ngDefaultControl [(ngModel)]="filteredLocation" name="locationFilter" id="locationFilter">
<button class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">entry.location</button>
</div>
</div>
I'm not sure how to get the buttons to work the same way as no filtering takes place.... any help appreciated!?
javascript
add a comment
|
I'm building with Angular and have used a filter pipe to filter on the selected option from my dropdown on the *ngFor loop. The content then filters accordingly. I want to swap the select options to buttons or pills. So when a button is clicked the filter takes place - the button will act as a on/off switch so you can have more than one option filtered.
Here is my stackblitz example - https://stackblitz.com/edit/timeline-angular-7-tyle1f
<div class="form-group row">
<div class="col-sm">
<select class="form-control" name="locationFilter" id="locationFilter" [(ngModel)]="filteredLocation">
<option value="All">All</option>
<option *ngFor="let entry of timeLine | filterUnique" value="entry.location">entry.location
</option>
</select>
</div>
//Button filters below
<div ngDefaultControl [(ngModel)]="filteredLocation" name="locationFilter" id="locationFilter">
<button class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">entry.location</button>
</div>
</div>
I'm not sure how to get the buttons to work the same way as no filtering takes place.... any help appreciated!?
javascript
add a comment
|
I'm building with Angular and have used a filter pipe to filter on the selected option from my dropdown on the *ngFor loop. The content then filters accordingly. I want to swap the select options to buttons or pills. So when a button is clicked the filter takes place - the button will act as a on/off switch so you can have more than one option filtered.
Here is my stackblitz example - https://stackblitz.com/edit/timeline-angular-7-tyle1f
<div class="form-group row">
<div class="col-sm">
<select class="form-control" name="locationFilter" id="locationFilter" [(ngModel)]="filteredLocation">
<option value="All">All</option>
<option *ngFor="let entry of timeLine | filterUnique" value="entry.location">entry.location
</option>
</select>
</div>
//Button filters below
<div ngDefaultControl [(ngModel)]="filteredLocation" name="locationFilter" id="locationFilter">
<button class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">entry.location</button>
</div>
</div>
I'm not sure how to get the buttons to work the same way as no filtering takes place.... any help appreciated!?
javascript
I'm building with Angular and have used a filter pipe to filter on the selected option from my dropdown on the *ngFor loop. The content then filters accordingly. I want to swap the select options to buttons or pills. So when a button is clicked the filter takes place - the button will act as a on/off switch so you can have more than one option filtered.
Here is my stackblitz example - https://stackblitz.com/edit/timeline-angular-7-tyle1f
<div class="form-group row">
<div class="col-sm">
<select class="form-control" name="locationFilter" id="locationFilter" [(ngModel)]="filteredLocation">
<option value="All">All</option>
<option *ngFor="let entry of timeLine | filterUnique" value="entry.location">entry.location
</option>
</select>
</div>
//Button filters below
<div ngDefaultControl [(ngModel)]="filteredLocation" name="locationFilter" id="locationFilter">
<button class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">entry.location</button>
</div>
</div>
I'm not sure how to get the buttons to work the same way as no filtering takes place.... any help appreciated!?
javascript
javascript
asked Sep 26 at 8:33
Tom RudgeTom Rudge
2,6513 gold badges30 silver badges60 bronze badges
2,6513 gold badges30 silver badges60 bronze badges
add a comment
|
add a comment
|
4 Answers
4
active
oldest
votes
If you want to filter on multiple options your filter pipe will have to be adjusted to accomadate an array of strings to filter on.
So in your FilterPipe the transform function gets changed as follows:
transform(value: string[], filterStrings: string[], propName: string): any
if (value.length === 0
After that you need to add some code to the timeline.component.ts file:
An array to hold all the active filter options:
filteredLocations: string[] = [];
A function to toggle an option on or off:
toggle(location)
let indexLocation = this.filteredLocations.indexOf(location);
if (indexLocation >= 0)
this.filteredLocations = this.filteredLocations.filter((i) => i !== location);
else
this.filteredLocations.push(location);
Now change the template (timeline.component.html):
Remove the selection box.
Add a click handler to your button:
<button (click)="toggle(entry.location)" class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">entry.location</button>
Lastly, the filter should accept the new filtered locations: (I just changed filteredLocation to filteredLocations)
*ngFor="let entry of timeLine | filter:filteredLocations:'location'"
add a comment
|
Bind a click event (click)="filteredLocation = entry.location" to the button
Try like this:
<div ngDefaultControl [(ngModel)]="filteredLocation" name="locationFilter" id="locationFilter">
<button (click)="filteredLocation = entry.location" class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">entry.location</button>
</div>
Working Demo
add a comment
|
The crux of this is just assign the value of the button/switch to your filteredLocation variable like below
<button (click)="filteredLocation = entry.location" class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">entry.location</button>
Updated Demo
Hope this helps :)
Thanks. Any ideas how I can change the filter/pipe to take more than one filter at a time?
– Tom Rudge
Sep 26 at 9:26
You can apply more than one filters/pipe easily just pipe them with|and they will be applied in the sequence of application. As in output of first pipe will be input for the second one.
– Manish
Sep 27 at 4:05
add a comment
|
Easier to just have a click handler:
<div>
<button class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique" (click)="filter(entry)">entry.location</button>
</div>
filter(entry)
this.filteredLocation = entry.location;
Demo: https://stackblitz.com/edit/timeline-angular-7-z2e6zh?file=src/app/timeline/timeline.component.ts
add a comment
|
Your Answer
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/4.0/"u003ecc by-sa 4.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%2f58112691%2ffilter-by-pills-buttons-instead-of-using-a-select-angular%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to filter on multiple options your filter pipe will have to be adjusted to accomadate an array of strings to filter on.
So in your FilterPipe the transform function gets changed as follows:
transform(value: string[], filterStrings: string[], propName: string): any
if (value.length === 0
After that you need to add some code to the timeline.component.ts file:
An array to hold all the active filter options:
filteredLocations: string[] = [];
A function to toggle an option on or off:
toggle(location)
let indexLocation = this.filteredLocations.indexOf(location);
if (indexLocation >= 0)
this.filteredLocations = this.filteredLocations.filter((i) => i !== location);
else
this.filteredLocations.push(location);
Now change the template (timeline.component.html):
Remove the selection box.
Add a click handler to your button:
<button (click)="toggle(entry.location)" class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">entry.location</button>
Lastly, the filter should accept the new filtered locations: (I just changed filteredLocation to filteredLocations)
*ngFor="let entry of timeLine | filter:filteredLocations:'location'"
add a comment
|
If you want to filter on multiple options your filter pipe will have to be adjusted to accomadate an array of strings to filter on.
So in your FilterPipe the transform function gets changed as follows:
transform(value: string[], filterStrings: string[], propName: string): any
if (value.length === 0
After that you need to add some code to the timeline.component.ts file:
An array to hold all the active filter options:
filteredLocations: string[] = [];
A function to toggle an option on or off:
toggle(location)
let indexLocation = this.filteredLocations.indexOf(location);
if (indexLocation >= 0)
this.filteredLocations = this.filteredLocations.filter((i) => i !== location);
else
this.filteredLocations.push(location);
Now change the template (timeline.component.html):
Remove the selection box.
Add a click handler to your button:
<button (click)="toggle(entry.location)" class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">entry.location</button>
Lastly, the filter should accept the new filtered locations: (I just changed filteredLocation to filteredLocations)
*ngFor="let entry of timeLine | filter:filteredLocations:'location'"
add a comment
|
If you want to filter on multiple options your filter pipe will have to be adjusted to accomadate an array of strings to filter on.
So in your FilterPipe the transform function gets changed as follows:
transform(value: string[], filterStrings: string[], propName: string): any
if (value.length === 0
After that you need to add some code to the timeline.component.ts file:
An array to hold all the active filter options:
filteredLocations: string[] = [];
A function to toggle an option on or off:
toggle(location)
let indexLocation = this.filteredLocations.indexOf(location);
if (indexLocation >= 0)
this.filteredLocations = this.filteredLocations.filter((i) => i !== location);
else
this.filteredLocations.push(location);
Now change the template (timeline.component.html):
Remove the selection box.
Add a click handler to your button:
<button (click)="toggle(entry.location)" class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">entry.location</button>
Lastly, the filter should accept the new filtered locations: (I just changed filteredLocation to filteredLocations)
*ngFor="let entry of timeLine | filter:filteredLocations:'location'"
If you want to filter on multiple options your filter pipe will have to be adjusted to accomadate an array of strings to filter on.
So in your FilterPipe the transform function gets changed as follows:
transform(value: string[], filterStrings: string[], propName: string): any
if (value.length === 0
After that you need to add some code to the timeline.component.ts file:
An array to hold all the active filter options:
filteredLocations: string[] = [];
A function to toggle an option on or off:
toggle(location)
let indexLocation = this.filteredLocations.indexOf(location);
if (indexLocation >= 0)
this.filteredLocations = this.filteredLocations.filter((i) => i !== location);
else
this.filteredLocations.push(location);
Now change the template (timeline.component.html):
Remove the selection box.
Add a click handler to your button:
<button (click)="toggle(entry.location)" class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">entry.location</button>
Lastly, the filter should accept the new filtered locations: (I just changed filteredLocation to filteredLocations)
*ngFor="let entry of timeLine | filter:filteredLocations:'location'"
answered Sep 26 at 9:17
Giovani VercauterenGiovani Vercauteren
1,56510 silver badges20 bronze badges
1,56510 silver badges20 bronze badges
add a comment
|
add a comment
|
Bind a click event (click)="filteredLocation = entry.location" to the button
Try like this:
<div ngDefaultControl [(ngModel)]="filteredLocation" name="locationFilter" id="locationFilter">
<button (click)="filteredLocation = entry.location" class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">entry.location</button>
</div>
Working Demo
add a comment
|
Bind a click event (click)="filteredLocation = entry.location" to the button
Try like this:
<div ngDefaultControl [(ngModel)]="filteredLocation" name="locationFilter" id="locationFilter">
<button (click)="filteredLocation = entry.location" class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">entry.location</button>
</div>
Working Demo
add a comment
|
Bind a click event (click)="filteredLocation = entry.location" to the button
Try like this:
<div ngDefaultControl [(ngModel)]="filteredLocation" name="locationFilter" id="locationFilter">
<button (click)="filteredLocation = entry.location" class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">entry.location</button>
</div>
Working Demo
Bind a click event (click)="filteredLocation = entry.location" to the button
Try like this:
<div ngDefaultControl [(ngModel)]="filteredLocation" name="locationFilter" id="locationFilter">
<button (click)="filteredLocation = entry.location" class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">entry.location</button>
</div>
Working Demo
answered Sep 26 at 8:46
Adrita SharmaAdrita Sharma
11.5k6 gold badges24 silver badges47 bronze badges
11.5k6 gold badges24 silver badges47 bronze badges
add a comment
|
add a comment
|
The crux of this is just assign the value of the button/switch to your filteredLocation variable like below
<button (click)="filteredLocation = entry.location" class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">entry.location</button>
Updated Demo
Hope this helps :)
Thanks. Any ideas how I can change the filter/pipe to take more than one filter at a time?
– Tom Rudge
Sep 26 at 9:26
You can apply more than one filters/pipe easily just pipe them with|and they will be applied in the sequence of application. As in output of first pipe will be input for the second one.
– Manish
Sep 27 at 4:05
add a comment
|
The crux of this is just assign the value of the button/switch to your filteredLocation variable like below
<button (click)="filteredLocation = entry.location" class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">entry.location</button>
Updated Demo
Hope this helps :)
Thanks. Any ideas how I can change the filter/pipe to take more than one filter at a time?
– Tom Rudge
Sep 26 at 9:26
You can apply more than one filters/pipe easily just pipe them with|and they will be applied in the sequence of application. As in output of first pipe will be input for the second one.
– Manish
Sep 27 at 4:05
add a comment
|
The crux of this is just assign the value of the button/switch to your filteredLocation variable like below
<button (click)="filteredLocation = entry.location" class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">entry.location</button>
Updated Demo
Hope this helps :)
The crux of this is just assign the value of the button/switch to your filteredLocation variable like below
<button (click)="filteredLocation = entry.location" class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique">entry.location</button>
Updated Demo
Hope this helps :)
answered Sep 26 at 8:46
ManishManish
3,2773 gold badges18 silver badges34 bronze badges
3,2773 gold badges18 silver badges34 bronze badges
Thanks. Any ideas how I can change the filter/pipe to take more than one filter at a time?
– Tom Rudge
Sep 26 at 9:26
You can apply more than one filters/pipe easily just pipe them with|and they will be applied in the sequence of application. As in output of first pipe will be input for the second one.
– Manish
Sep 27 at 4:05
add a comment
|
Thanks. Any ideas how I can change the filter/pipe to take more than one filter at a time?
– Tom Rudge
Sep 26 at 9:26
You can apply more than one filters/pipe easily just pipe them with|and they will be applied in the sequence of application. As in output of first pipe will be input for the second one.
– Manish
Sep 27 at 4:05
Thanks. Any ideas how I can change the filter/pipe to take more than one filter at a time?
– Tom Rudge
Sep 26 at 9:26
Thanks. Any ideas how I can change the filter/pipe to take more than one filter at a time?
– Tom Rudge
Sep 26 at 9:26
You can apply more than one filters/pipe easily just pipe them with
| and they will be applied in the sequence of application. As in output of first pipe will be input for the second one.– Manish
Sep 27 at 4:05
You can apply more than one filters/pipe easily just pipe them with
| and they will be applied in the sequence of application. As in output of first pipe will be input for the second one.– Manish
Sep 27 at 4:05
add a comment
|
Easier to just have a click handler:
<div>
<button class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique" (click)="filter(entry)">entry.location</button>
</div>
filter(entry)
this.filteredLocation = entry.location;
Demo: https://stackblitz.com/edit/timeline-angular-7-z2e6zh?file=src/app/timeline/timeline.component.ts
add a comment
|
Easier to just have a click handler:
<div>
<button class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique" (click)="filter(entry)">entry.location</button>
</div>
filter(entry)
this.filteredLocation = entry.location;
Demo: https://stackblitz.com/edit/timeline-angular-7-z2e6zh?file=src/app/timeline/timeline.component.ts
add a comment
|
Easier to just have a click handler:
<div>
<button class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique" (click)="filter(entry)">entry.location</button>
</div>
filter(entry)
this.filteredLocation = entry.location;
Demo: https://stackblitz.com/edit/timeline-angular-7-z2e6zh?file=src/app/timeline/timeline.component.ts
Easier to just have a click handler:
<div>
<button class="btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique" (click)="filter(entry)">entry.location</button>
</div>
filter(entry)
this.filteredLocation = entry.location;
Demo: https://stackblitz.com/edit/timeline-angular-7-z2e6zh?file=src/app/timeline/timeline.component.ts
answered Sep 26 at 8:41
ChrillewoodzChrillewoodz
17.7k16 gold badges57 silver badges117 bronze badges
17.7k16 gold badges57 silver badges117 bronze badges
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.
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%2f58112691%2ffilter-by-pills-buttons-instead-of-using-a-select-angular%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