Flashing issues when utilizing the Jquery ui slider within an Angular 2 component

I recently incorporated a jquery-ui slider plugin into an angular 2 component and it's been working well overall, but I have encountered an annoying issue. Whenever the slider is used, there is a flickering effect on the screen. Interestingly, when I tried using the standard HTML5 slider instead, there was no flicker and the transition was smooth. However, the HTML5 slider only supports one handle whereas the jquery-ui slider supports two handles. How can I eliminate this flickering effect? I need help in identifying the root cause of this problem. I have created a detailed example here with additional comments.

export class RangeSlider implements OnInit {

@Input() value: any;
@Output() sliderChanged= new EventEmitter();

elementRef: ElementRef;
constructor(elementRef: ElementRef) {
    this.elementRef = elementRef;
}
ngOnInit() {
    let that = this;
    let gradeLabels = ["K", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11","12"];
    jQuery(this.elementRef.nativeElement).find('.rangeSlider').slider({
        range: true,
       min: 0,
       max: gradeLabels.length-1,
        values: [0, 12],
        slide: function (event, ui) {
          let min = ui.values[0]
         let max = ui.values[1];
          min = (min == 0) ? "K" : min;
          let grade = (min == max) ? min : min + '-' + max;
          that.sliderChanged.emit(grade);
       }
    }).slider("pips", {
       rest: "label",
       labels:gradeLabels
    });
}

Answer №1

It seems like the issue lies in how you are handling the reference to activitiesArray within your sliderChanged() function. By replacing this reference each time the function is called, the async pipe ends up unsubscribing from the old data and subscribing to the new one, resulting in a brief period where no data is displayed. This effect would be more noticeable if you were working with HTTP requests.

To address this problem, I have made some modifications to your code in this [plunk](https://plnkr.co/edit/LCw9Agn30ECdR1SuvoEN?p=preview) to prevent repetitive setting of activitiesArray and ensure a more reactive approach. It would be ideal to observe the output directly from the jQuery slider instead of triggering an event callback that pushes data to a subject.

Here are the changes made to src/app.component.ts:

htmlSlider = new Control();
jqSlider$ = new Subject();

constructor(private _activityService: ActivityService) {
    let searchTerm$ = this.term.valueChanges
      .debounceTime(400)
      .distinctUntilChanged()
      .startWith('');
      
    let activityList$ = this._activityService.getActivityList();
    
    let searchedActivities$ = activityList$.combineLatest(searchTerm$, this.searchFilter);
    let jqSliderFiltered$ = searchedActivities$.combineLatest(this.jqSlider$.startWith(undefined), this.sliderSearch);
    
    let htmlSlider$ = this.htmlSlider.valueChanges.startWith(undefined);
    let htmlSliderFiltered$ = jqSliderFiltered$.combineLatest(htmlSlider$, this.gradePipe);
    
    this.activitiesArray = htmlSliderFiltered$;
}

searchFilter(activityList: Activity[], term: string): Activity[] {
    return activityList.filter(item => item.description.toLowerCase().includes(term.toLowerCase()));
}

sliderSearch(activityList: Activity[], term: string): Activity[] {
    if(!term) return activityList;
    return activityList.filter(item => item.grade == term);
}

gradePipe(activityList, minGrade) {
  if (!minGrade) return activityList;
  
  return activityList.filter(activity => parseInt(activity.grade.split("-")[0]) >= +minGrade);
}

sliderChanged(grade: any) {
    this.jqSlider$.next(grade);
}

Changes made to activitymain.html:

<input type="range" min="0" max="12" [ngFormControl]="htmlSlider" />

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

Ensuring the validity of an HTML form by including onClick functionalities for additional form elements

I am working on a form that I put together using various pieces of code that I found online. My knowledge of HTML and JavaScript is very basic. The form includes a button that, when clicked, will add another set of the same form fields. Initially, I added ...

Easily accessible button and span located on the right side

One issue I'm facing is that the button and span are not aligned on the same line. Is there a way to resolve this? <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/c ...

Invert the motion within a photo carousel

Is there anyone who can assist me with creating a unique photo slider using HTML, CSS, and JS? Currently, it includes various elements such as navigation arrows, dots, and an autoplay function. The timer is reset whenever the arrows or dots are clicked. Ev ...

Executing a component method from a service class in Angular

When trying to call a component method from a service class, an error is encountered: 'ERROR TypeError: Cannot read property 'test' of undefined'. Although similar issues have been researched, the explanations mostly focus on component- ...

NG02100: InvalidPipeArgument: 'Cannot parse "Wed Jun 29 2022 10:19:00 GM" as a date' when using the DatePipe in Angular

It's frustrating not knowing where in my code I'm triggering this error, but every time I click on an item, this error pops up: https://i.stack.imgur.com/Gmu8S.png I suspect this error indicates a need to convert a string to a date, though I ca ...

Place a small image within a list item to ensure it matches the height of the list element

I need help aligning images to the right within list elements. The images are squares with equal height and width, and I want them to fit snugly within the height of the list element on the right side. My HTML code is quite simple for now (see example bel ...

Ensuring the Current Page is Valid Before Progressing to the Next Step in Angular-Archwizard

Currently, I am working with Angular-7 and using angular-archwizard to create a multi-step form. The issue I am facing is that the form allows users to proceed to the next step without filling in all the required fields. My goal is to restrict users from m ...

Is there a way to alter the date format for input elements within formGroups using Angular 7?

When the input is of type 'Date', the date format is dd/MM/yyyy. I need to convert the date format from MM/dd/yyyy to dd/MM/yyyy (Turkish Format and Turkish Calendar). Below is the code snippet. <form [formGroup]="opportunityForm" (ngSubmit ...

Using jQuery, selectively reveal and conceal multiple tbody groups by first concealing them, then revealing them based on

My goal is to initially display only the first tbody on page load, followed by showing the remaining tbody sections based on a selection in a dropdown using jQuery. Please see below for a snippet of the code. //custom JS to add $("#choice").change(func ...

Attempting to establish a login system using MongoDB

I am currently working on a function to verify user login details entered in a form and compare them with data stored in MongoDB, such as email and password. However, the function seems to be malfunctioning. The expected behavior is that when a user ente ...

Updating Github pages requires clearing the cache first

As I work on my first website using GitHub pages, I've noticed that it can be frustrating to constantly clear the cache or open an incognito window whenever I add something new. I'm thinking about incorporating Jekyll into my workflow so I can t ...

I wonder which Material Design repository is the source of this library

I am attempting to replicate the CSS style used in this particular Material Design-inspired web application: https://i.stack.imgur.com/utalx.png After exploring Materialize CSS and MUI, I have not been able to pinpoint the exact framework responsible for ...

In what way can I fill out my search fields using the criteria included in the URL?

In the process of developing an asp.net web application, I have implemented a code snippet to construct a section for search fields. This code builds URL parameters based on user input: <script type="text/javascript> $(document).ready(function(){ ...

Transform SVG with a Click

Can someone assist me with rotating the pink area on click and moving it to a new angle from its current position? I am looking for a way to save the current position of the pink area and then, when clicked, move it smoothly to a new position. Your help ...

Tips for eliminating nested switchMaps with early returns

In my project, I have implemented 3 different endpoints that return upcoming, current, and past events. The requirement is to display only the event that is the farthest in the future without making unnecessary calls to all endpoints at once. To achieve th ...

Error encountered while testing karma: subscription function is not recognized

I encountered an issue with my karma unit test failing with the following error message. "this.gridApi.getScaleWidth().subscribe is not a function" GridApi.ts export class GridApi { private scaleWidthSubject = new BehaviorSubject<{value: number}& ...

Strange occurrences within the realm of javascript animations

The slider works smoothly up until slide 20 and then it suddenly starts cycling through all the slides again before landing on the correct one. Any insights into why this is happening would be greatly appreciated. This issue is occurring with the wp-coda- ...

Notify user before exiting the page if there is an unsaved form using TypeScript

I am working on a script that handles unsaved text inputs. Here is the code for the script: export class Unsave { public static unsave_check(): void { let unsaved = false; $(":input").change(function(){ unsaved = true; ...

Styling the background of a navigation menu specifically for mobile devices using CSS

Currently, my focus is on developing the website I am using Elementskit nav builder and I aim to change the background color of the navigation menu on mobile and tablet devices to black The existing code snippet is as follows: margin-top: 6px; } @med ...

Should the potential benefits of implementing Responsive Design in IE8 be taken into account in 2013?

It seems that there are still questions lingering about how to make responsive design compatible with outdated browsers like IE8 or even the dreaded IE7. Personally, I question the need for implementing responsive design specifically for IE8, considering ...