Search code examples
sapui5jquery-ui-slider

JQuery Date Slider in SAPUI5 application


I have a custom SAPUI5 in which I want to use slider for date picker, I found example

I have included iquery-ui lib in Controller.js (as we will run app in Fiori Launchpad). And included the JS code from indicated post in onAfterRendering(), which is recommended to use instead of $(document).ready by UI5.

When I run the app - it says TypeError: $(...).slider is not a function, although I see in Network tab jquery-ui.js is being loaded.

I tried also to make it work from index.html - then there is no error in console, just the slider is absent.

Can someone please advice?

This is my code in onAfterRendering function

$('.slider-time').html(dt_from);
$('.slider-time2').html(dt_to);
var min_val = Date.parse(dt_from) / 1000;
var max_val = Date.parse(dt_to) / 1000;

function zeroPad(num, places) {
var zero = places - num.toString().length + 1;
return Array(+(zero > 0 && zero)).join("0") + num;
}

function formatDT(__dt) {
var year = __dt.getFullYear();
var month = zeroPad(__dt.getMonth() + 1, 2);
var date = zeroPad(__dt.getDate(), 2);
var hours = zeroPad(__dt.getHours(), 2);
var minutes = zeroPad(__dt.getMinutes(), 2);
var seconds = zeroPad(__dt.getSeconds(), 2);
return year + '-' + month + '-' + date + ' ' + hours + ':' + minutes + ':' + seconds;
};

$("#slider-range").slider({
range: true,
min: min_val,
max: max_val,
step: 10,
values: [min_val, max_val],
slide: function (e, ui) {

var dt_cur_from = new Date(ui.values[0] * 1000); 
$('.slider-time').html(formatDT(dt_cur_from));

var dt_cur_to = new Date(ui.values[1] * 1000);                 
$('.slider-time2').html(formatDT(dt_cur_to));

Solution

  • Thanks for your example on plunkr. I’m still debugging, but I’m also wondering: Is there a special reason, why you are not using the TimePickerSliders-control from SAPUI5 or the Slider-control, which is available in both SAPUI5 and OpenUI5 ?

    You could save some of your own coding and the design might be more Fiori-like. I modified the “content” of your Main.view.xml in the following way:

    <sap.ui.core.mvc:View controllerName="view.Main"
        xmlns="sap.m"
        xmlns:sap.ui.core.mvc="sap.ui.core.mvc"
      xmlns:html="http://www.w3.org/1999/xhtml">
        <Page title="Slider">
            <content>
    
        <Text text="TimePickerSliders" class="sapUiSmallMarginBottom"/>
        <TimePickerSliders
          id="TPS2"
          valueFormat="hh:mm:ss a"
          displayFormat="HH:mm:ss"
          change="handleChange"
          height="400px"
          support2400="true"/>
    
        <Text text="Slider with tickmarks and labels" class="sapUiSmallMarginBottom"/>
        <Slider min="0" max="60" enableTickmarks="true" value="30" width="90%" class="sapUiSmallMarginBottom">
          <ResponsiveScale tickmarksBetweenLabels="5"/>
        </Slider>
        <Text text="Minutes" class="sapUiSmallMarginBottom"/>
    
        <Slider min="0" max="24" enableTickmarks="true" class="sapUiSmallMarginBottom" width="90%">
          <ResponsiveScale tickmarksBetweenLabels="1"/>
        </Slider>
        <Text text="Hours" class="sapUiSmallMarginBottom"/>
            </content>
        </Page>
    </sap.ui.core.mvc:View>
    

    Would this be acceptable for your requirement ?