material icon indicating copy to clipboard operation
material copied to clipboard

md-slider should have a option to update the model after the user has stopped sliding it.

Open junaidurfjd opened this issue 11 years ago • 12 comments

Right now when we slide the slider, the model value changes. But if that model value is bind to some variable on backend, then sliding it will cause a lot of server calls. I want the slider to update the server only when the user has set its value, and mouse has left.

junaidurfjd avatar Feb 09 '15 10:02 junaidurfjd

This works for discrete slider. But for linear slider there should be an option like that..

junaidurfjd avatar Feb 09 '15 10:02 junaidurfjd

+1 Seiyria bootstrap-slider is a nice example (see events).

Flertz avatar Feb 09 '15 14:02 Flertz

@Darwesh27 - I do not understand. If I use:

<md-slider 
          md-discrete 
          ng-model="rating" 
          step="1" min="1" max="5" 
          aria-label="rating" flex >
</md-slider>

Then the rating model value will be updated as the slider changes.

ThomasBurleson avatar Feb 09 '15 14:02 ThomasBurleson

@ThomasBurleson you are right: the rating model value will be updated. But if you want to update some back-end variable ONLY when the user stops to dragging, you should have some events fired from the slider.

Flertz avatar Feb 09 '15 14:02 Flertz

@Flertz, @Darwesh27 - the team made specific decisions to minimize the use of custom change events. Instead Angular Material expects developers to use standard AngularJS practices to $watch for specific model changes and respond accordingly.

Closed - No change expected.

ThomasBurleson avatar Feb 09 '15 15:02 ThomasBurleson

@Flertz, @Darwesh27 sure it's a little late however; there might be ones still encountering this case.

ngMouseUp directive perfectly solves the problem here. Bind some model to slider and do stuff with that model on mouseup event.

Hope it'll help. Thanks.

dbtek avatar Jan 19 '16 11:01 dbtek

@dbtek ngMouseUp does not work properly, when you start dragging the slider, but end up releasing mouse button out if its zone. However, if your cursor is still over the slider, you will surely be able to catch the event...

alwye avatar Jun 21 '16 05:06 alwye

@dbtek @ThomasBurleson This a very ugly implement for resolve this issue. I tried to using ng-change to resolve this issue, but I failed.

  1. ng-mouseup should be added at md-slider-container. This will expand the event to the whole slider area. When click other part inner slider container but not on slider itsself it will call the api and wast of some resource.
  2. When we want to type something into the input to modify the value, The stupid ng-mouseup won't fire.
  3. $watch is not the best practice for this issue.

qoshi avatar Jul 25 '16 05:07 qoshi

You could use lodash's _.debounce() or a similar method. It might not be perfect, but it would at least cut down on your server calls dramatically.

aj-r avatar Oct 24 '16 20:10 aj-r

@qoshi

ng-mouseup should be added at md-slider-container.

Thank you for suggestion! It helps me.

SergeyKurdyukov avatar Jun 23 '17 11:06 SergeyKurdyukov

This is still an issue as alwye and qoshi have outlined. md-discrete seems to catch the mouseup event, but comes with its own set of problems (and different UI you may not want)

My solution - add a temporary event listener

$('#button').on('mousedown', function() {
      document.body.addEventListener("mouseup", release); // add listener to begin watching for mouseup
});
var release = function() {
      myAction(); // your API call here
      document.body.removeEventListener("mouseup", release); // clean up and remove the listener
}

pyroflies avatar Dec 15 '17 23:12 pyroflies

@mk4yukkalab I would be open to a community PR that adds some custom events (and tests) to the slider. The first step would be to open a new enhancement request with the proposed approach and APIs.

Splaktar avatar Dec 09 '18 09:12 Splaktar