Ion Range Slider with HH:mm format From & To not working

风格不统一 提交于 2021-01-29 05:23:00

问题


I am trying to use Ion Range Slider in my project to select working hours. Ex: (08:00-19:00).

 $('#ion').ionRangeSlider({
    grid: false,
    type: 'double',
    min: moment("0000", "hhmm"),
    max: moment("2359", "hhmm"),
    from: moment("0800", "hhmm"),
    to: moment("1900", "hhmm"),
    force_edges: true,
    drag_interval: true,
    step: 3600000,
    min_interval: 3600000,
    prettify: function (num) {
        return moment(num).format('HH:mm');
    }
});

When i want to set default working hours using from & to, it doesn't work. Slider works, though, but values are not changing.

What am i missing?


回答1:


Because you are using hours in milliseconds you need to change the value of moment you are using from an object to its milliseconds value:

min: moment("0000", "hhmm"),   // this is an object and not a number...
max: moment("2359", "hhmm"),
from: moment("0800", "hhmm"),
to: moment("1900", "hhmm"),

Change to (see docs for details):

min: moment("0000", "hhmm").valueOf(),
max: moment("2359", "hhmm").valueOf(),
from: moment("0800", "hhmm").valueOf(),
to: moment("1900", "hhmm").valueOf(),

$('#ion').ionRangeSlider({
    grid: false,
    type: 'double',
    min: moment("0000", "hhmm").valueOf(),
    max: moment("2359", "hhmm").valueOf(),
    from: moment("0800", "hhmm").valueOf(),
    to: moment("1900", "hhmm").valueOf(),
    force_edges: true,
    drag_interval: true,
    step: 3600000,
    min_interval: 3600000,
    prettify: function (num) {
        return moment(num).format('HH:mm');
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
<link rel="stylesheet" href="https://rawgit.com/IonDen/ion.rangeSlider/master/css/ion.rangeSlider.css">
<link rel="stylesheet" href="https://rawgit.com/IonDen/ion.rangeSlider/master/css/ion.rangeSlider.skinModern.css">
<script src="https://rawgit.com/IonDen/ion.rangeSlider/master/js/ion.rangeSlider.min.js"></script>


<input type="text" id="ion" name="example_name" value="" />


来源:https://stackoverflow.com/questions/52062005/ion-range-slider-with-hhmm-format-from-to-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!