Skip to content Skip to sidebar Skip to footer

Don't Lose Previous Position Of Rzslider After Select The Date In Angular Js?

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 +4hrs and

Solution 1:

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 };
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://rawgit.com/rzajac/angularjs-slider/master/dist/rzslider.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.14.3/ui-bootstrap-tpls.js"></script>
<script src="https://rawgit.com/rzajac/angularjs-slider/master/dist/rzslider.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<div ng-app="rzSliderDemo">
  <div ng-controller="MainCtrl" class="wrapper">
    <header>
      <h2>AngularJS Touch Slider</h2>
    </header>
    <article>
      <div class="form-group">
        <label for="choos-birth" class="control-label">choose date:</label>
        <div class="control">
          <input id="choos-birth" class="form-control" type="date" ng-model="dateBirth" style="witdh:100px;">
        </div>
      </div>
      
      <br />
      <rzslider rz-slider-model="slider.minValue" rz-slider-high="slider.maxValue" rz-slider-options="slider.options"></rzslider>
    </article>
  </div>
</div>

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


Post a Comment for "Don't Lose Previous Position Of Rzslider After Select The Date In Angular Js?"