Don't lose previous position of Rzslider after select the date in Angular Js?

后端 未结 1 1974
没有蜡笔的小新
没有蜡笔的小新 2020-12-20 03:45

I am using RZslider for the date and picker. But I am facing some problem again and agin. And also new in Rzlider and angularJs. So Slider containing 8hrs of current time +4

1条回答
  •  情话喂你
    2020-12-20 04:07

    The logic is to store the maxValue and minValue hours. Then when the date change, combine the new date with the old hours (if exist).

    var app = angular.module('rzSliderDemo', ['rzModule', 'ui.bootstrap']);
    
    app.controller('MainCtrl', function($scope, $rootScope, $timeout) {
      $scope.$watch('dateBirth', function(n, o) {
        var newDay = n || new Date();
        $scope.selectedDate = moment(newDay);
        $scope.selectedDate.hour(moment().hour());
        $scope.selectedDate.minute(0);
        $scope.init();
      });
      
      $scope.init = function() {
        var startDate, endDate, startTime, endTime;
        
        var timeData = getRange($scope.selectedDate);
        $scope.localTime = timeData.currentTime; // actually start of this hour
        
        var arr = timeData.times.map(n => {
          return {
            value: n.value
            //legend: n.value
          };
        });
        
        $timeout(function() {
          $scope.slider = {
            minValue: $scope.getValue($scope.valueTypes.MIN),
            maxValue: $scope.getValue($scope.valueTypes.MAX),
            options: {
              stepsArray: arr,
              showTicks: true,
              draggableRange: true,
              onChange: function() {
                $scope.minValueHour = moment($scope.slider.minValue).get('hour');
                $scope.maxValueHour = moment($scope.slider.maxValue).get('hour');
              }
            }
          };
        });
      }
      
      $scope.valueTypes = {
        MIN: 'min',
        MAX: 'max'
      };
      
      $scope.getValue = function(kind) {
        var localTime = $scope.localTime.clone();
        
        if ($scope[kind + 'ValueHour']) {
          localTime.set({hour: $scope[kind + 'ValueHour']});
        }
        else {
          var method = kind === 'min' ? 'subtract' : 'add';
          localTime[method](4, "hours")
        }
        
        return localTime.format('YYYY DD MMM HH:mm');
      }
      
      $scope.init();
    });
    
    function getRange(currentDate) {
      var arr = [];
      var totalHourRange = 32;
      var currentTime = currentDate || moment(); // current date and time using Moment
      
      // set current time to beginning of the hour
      currentTime.minute(0);
      
      // clone date and substract 1/2 total range to get start point
    var tmpTime = currentTime.clone();
         //tmpTime.subtract(totalHourRange / 2, 'hours');
         tmpTime.hour(0).subtract(4, 'hours');
      
      // offset is the number of minutes from the current point
      for (var i = -6 * (totalHourRange / 2); i <= 6 * (totalHourRange / 2); i++) {
        arr.push({value: tmpTime.format('YYYY DD MMM HH:mm'), offset: i * 10});
        tmpTime.add(10, 'minutes');
      }
      return { times: arr, currentTime: currentTime, totalHourRange: totalHourRange };
    }
    
    
    
    
    
    
    

    AngularJS Touch Slider


    http://jsbin.com/zenaco/edit?html,js

    0 讨论(0)
提交回复
热议问题