Filtering JSON data with Angular 4 input range feature

Hello there, I'm currently working on a search pipe in Angular 4 to filter company team sizes from JSON data using a range input between 0 and 100. However, I'm facing an issue with the range filter as I am relatively new to TypeScript and Angular 4. Any assistance would be greatly appreciated as I've tried some basic code but seem to have trouble grasping the logic.

Here is the Range Pipe -

@Pipe({
  name: 'range'
})
export class CheckboxPipe implements PipeTransform {

  transform(value: any, args?: any): any {
    console.log('args', args);
    return args
            ? value.filter(sal => { return sal.team })
            : value;
  }

}

Below is the Range Input box -

0 <input type="range" min="0" max="100" 
        [(ngModel)]="team" name="team" /> 100

And here is the list being filtered -

<div *ngFor="let joblist of jobList | range: team">
    {{joblist.team}}
</div> 

Answer №1

Make a modification to your transform() function like this,

transform(value: any, team?: any): any {
return (team || team === 0)
        ? value.filter(sal => { return sal.team })
        : value;
}

After making the change, test if it resolves the issue at hand.

UPDATE:

Although not entirely certain, there might be an issue with ngModel not receiving the correct value due to the use of input with type set as range. Suggesting an alternative approach below:

0 <input #rangeInput type="range" min="0" max="100" [(ngModel)]="team" name="team" /> 100
<div *ngFor="let joblist of jobList | range: rangeInput?.value">
 {{joblist.team}}
</div> 

Here, I introduced a template reference variable #rangeInput to pass the value of that control to the pipe.

If the above step doesn't yield results, consider changing the team === 0 check in the return statement within the transform() to team === '0', treating the value as a string instead of a number.

Answer №2

Hey there, I successfully tackled the range filter issue and found it to be quite manageable. Let me share my solution with you -

Firstly, here is the custom Range Pipe implementation -

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'range'
})
export class RangePipe implements PipeTransform {

  transform(teamVal: any[], teamFil?: number): any {
        console.log('teamFil', teamFil);
        return teamFil
         ? teamVal.filter(person => person.team >= teamFil) 
         : teamVal;
    }

}

Next, let's look at the Range slider component -

<span>0</span>
<input type="range" min="0" max="100" [(ngModel)]="sliderValue" />
<span>100</span>

The current value of the slider in the component is stored in a property called sliderValue -

sliderValue: number = 0; 

Now, we can use this slider to filter data based on its value within the HTML template -

<ul>
    <li *ngFor="let joblists of jobList | range: sliderValue">{{joblists.team}}</li>
</ul>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to merge two observables into one observable when returning them?

I'm struggling with getting a function to properly return. There's a condition where I want it to return an Observable, and another condition where I'd like it to return the combined results of two observables. Here is an example. getSearc ...

Transferring data to .NET using Angular's POST method

I am encountering an issue when trying to send parameters to the .net side as they are coming back as undefined. The main problem I am facing is that I have two post methods with the same parameters, so I attempted to send a string along with the other one ...

"Transform the appearance of the datepicker input field with Material 15's dynamic

I am in need of assistance to change the color to white for the input date and add an underline to a datepicker element <mat-form-field class="date-criteria-select " [floatLabel]="'always'"> <mat-label class=" ...

The PrimeNG dialog component stubbornly refuses to close even when clicking outside the modal

My modal dialog component from PrimeNG is structured like this <p-dialog header="{{title}}" [(visible)]="display" [modal]="true" [dismissableMask]="true" [closeOnEscape]="true" [responsive]="true" [closable]="false" > {{content}} </p-dialog&g ...

Combining the output of two Observables through the CombineLatest method to generate a

I possess two separate collections of information: Data Model: taxControlReference [ { "providerId": "HE", "taxTables": { "STAT": [ 1 ] } }, ...

Can I assign a value from the tagModel to ngx-chips in an Angular project?

HTML code: <tag-input class="martop20 tag-adder width100 heightauto" [onAdding]="onAdding" (onAdd)="addInternalDomain($event)" type="text" Ts code: addInternalDomain(tagTex ...

leveraging the import statement in conjunction with SystemJs

Currently, I am in the process of creating a sample code using TypeScript and SystemJS for the browser. In my app.ts file: import {Person,add as test} from './testLib' In the generated app.js file (by TypeScript compiler): var testLib_1 = re ...

Issue with Angular Material date picker: Date Parsing UTC causing dates to display as one day earlier

After exploring numerous threads related to this issue and spending several days trying to find a solution, I may have stumbled upon a potential fix. However, the workaround feels too messy for my liking. Similar to other users, I am encountering an issue ...

Setting up Electron with React and TypeScript: A Comprehensive Guide

I've been developing an app using Electron, React (jsx), and Babel. However, I recently made the switch to TypeScript and I'm struggling to get everything functioning properly. The npm packages I've tried only work for either React or TypeSc ...

The value returned by Cypress.env() is always null

Within my cypress.config.ts file, the configuration looks like this: import { defineConfig } from "cypress"; export default defineConfig({ pageLoadTimeout: 360000, defaultCommandTimeout: 60000, env: { EMAIL: "<a href="/cdn-cgi/ ...

Deprecated: Ngx Progress Bar will no longer be supported due to changes in

In the scenario below, I have noticed that BrowserXhr is outdated: { provide: BrowserXhr, useClass: NgProgressBrowserXhr } But when I refer to the documentation, it directs me to the main HttpClient page without showing an alternate provider example. Wha ...

Ensure to pass the correct type to the useState function

I have a basic app structured like this import React, { useState } from 'react' import AddToList from './components/AddToList' import List from './components/List' export interface IProps{ name: string age: number url: ...

What is the process for assigning a serial number to each row in the MUI DataGrid?

Initially, the server is accessed to retrieve some data. After that, additional data is added. While the data does not contain an ID, the form must still display a serial number. const columns: GridColDef[] = [ { field: 'id' ...

Is it possible for an object's property specified in a TypeScript interface to also incorporate the interface within its own declaration?

While it may seem like an unusual request, in my specific scenario, it would be a perfect fit. I am working with an object named layer that looks something like this: const layer = { Title: 'parent title', Name: 'parent name', ...

Adjust the range slider's color depending on its value

I'm looking to customize the color of a range slider as its value increases, switching from red to green. Below is the code I've tried, but it's not quite working as intended. The goal is for the color to change based on the value of masterR ...

CSS that relies on the presence of a specific class

I'm a CSS novice and I have encountered a scenario where I need to apply a CSS class only when another class is not present. <div class="border-solid p-2 bg-white mt-1 h-fit" [ngClass]="SOME_CONDITION ? 'tile-con ...

utilize the useRef hook to display the total number of characters within a text area

Introducing the following component: import React from 'react'; export interface TexareaProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> { maxLength?: number; id: string; } export const Textarea = React.forwardRef( ( ...

Error in Angular 7: params.map function is undefined

When attempting to launch my Angular 7 application with ng serve, I suddenly encountered the following error: ERROR in params.map is not a function I am unsure of the origin of this error as ng isn't providing much information. I have attempted to ...

Tips for displaying an array and iterating through its children in Angular 7

I am currently working on extracting the parent and its children to an array in order to display them using ngFor. However, I am encountering an issue where the children are not being displayed during the ngFor. I have a service that retrieves data from a ...

In TypeScript and React, what is the appropriate type to retrieve the ID of a div when clicked?

I am facing an issue in finding the appropriate type for the onClick event that will help me retrieve the id of the clicked div. const getColor = (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => { const color = event.target.id; // ...