I've been looking for a clean way to dynamically resize Kendo UI Sliders; Knockout-Kendo and Paul Irish's smart resize plugin have enabled me to arrive at a reasonably workable solution for an individual slider:
<div class="sliderWrapper">
<input class="slider col" data-bind="kendoSlider: { value: currValue, enabled: enabled, min: 0, max: 100, slide: sliderOnSlide, tickPlacement: 'none', smallStep: 1, showButtons: false, tooltip: { 'enabled': false } }, sliderTip: {}" />
</div>
var ViewModel = function (initValue) {
this.currValue = ko.observable(initValue);
this.enabled = ko.observable(true);
};
ko.bindingHandlers.sliderTip = {
init: function(element, valueAccessor) {
var dragger = $(element).closest('.k-slider').find('.k-draghandle');
dragger.empty().html('<span class="sliderTip">0%</span>');
}
};
ko.applyBindings(new ViewModel(0));
$(window).smartresize(function() {
$('.sliderBox').each(function() {
var value = $(this).val();
$(this).prev().empty().append('<input class="slider col" data-bind="kendoSlider: { value: currValue, enabled: enabled, min: 0, max: 100, slide: sliderOnSlide, tickPlacement: \'none\', smallStep: 1, showButtons: false, tooltip: { \'enabled\': false } }, sliderTip: {}" />');
ko.applyBindings(new ViewModel(value));
$(this).prev().find('.sliderTip').text(value + '%');
});
});
You can see the result by dragging the borders of the Result pane at this Fiddle: http://jsfiddle.net/MontiDesign/DuZK3/24/
I'm pleased, but I've been working with a Knockout scenario that allows for dynamically adding multiple sliders (see http://jsfiddle.net/MontiDesign/DuZK3/37/). The above solution simply wipes out all the sliders, though. I tried a variation using a custom binding handler that does not work -- and forgive me if this is just hideous, but I'm new to Knockout and still growing in JS/jQuery skills:
ko.bindingHandlers.resizeSlider = {
init: function(element, valueAccessor, allBindings) {
var slider = $(element).data('kendoSlider');
if (slider) {
$(window).smartresize(function() {
$(slider).closest('.sliderWrapper').next().each(function() {
var value = $(this).val();
$(this).prev().empty().append('<input class="slider col" data-bind="kendoSlider: { value: currValue, enabled: enabled, min: 0, max: 100, slide: sliderOnSlide, tickPlacement: \'none\', smallStep: 1, showButtons: false, tooltip: { \'enabled\': false } }, sliderTip: {}" />');
ko.applyBindings(new CreateSlider(value));
$(this).prev().find('.sliderTip').text(value + '%');
});
});
}
}
}
I figure I need to iterate through the array of dynamically created sliders and apply the resize function to each one, but I'm uncertain of how to accomplish this with Knockout.
Thanks in advance for any help you can provide!