I'm using noUiSlider (http://refreshless.com/nouislider/) on a form which will have dozens of sliders on it. Rather than copy/pasting the code for each, I thought I could just set up an array of class names and a loop. That works; ie, it sets up working sliders. However the form also has to show the value of a slider upon update, and that's the part that I can't work out. I know how to do it with a static value but not in the loop ...
Simplified example:
JavaScript:
var steps = [
'Never',
'Occasionally',
'Frequently',
'Constant'
];
// This array will have many more
var slider_names = [
'slider',
'slider2'
];
var sliders = [];
for (var i = 0; i < slider_names.length; i++) {
sliders.push(document.getElementById(slider_names[i]));
noUiSlider.create(sliders[i], {
start: 0,
connect: 'lower',
step: 1,
range: {
'min': [ 0 ],
'max': [ 3 ]
},
pips: {
mode: 'steps',
density: 100
}
});
sliders[i].noUiSlider.on('update', function(values, handle) {
// ***** Problem line *****
document.getElementById(slider_names[i]+'-value').innerHTML = steps[parseInt(values[handle])];
// ***** Problem line *****
});
}
HTML:
<div id="slider-value"></div>
<div id="slider"></div>
<div id="slider2-value"></div>
<div id="slider2"></div> (etc...)
The problem line is highlighted above ... when using a static value (ex, 'slider2-value') it works fine, but I can't seem to figure out how to target the appropriate id when the update event triggers ... slider_names[i] obviously won't work there. I'm probably missing something obvious? Thanks!
I've also stumbled into the same issue today, I've used the following code.
Basically i'm just sending all the values of the sliders to this function which then pushes all the results into an array, then i can handle the array of data however i want to.
Hopefully this can be of some use to you also.
var sliders = document.getElementsByClassName('sliders');
for ( var i = 0; i < sliders.length; i++ ) {
noUiSlider.create(sliders[i], {
start: 50,
step: 5,
connect: "lower",
orientation: "horizontal",
range: {
'min': 0,
'max': 100
},
});
sliders[i].noUiSlider.on('slide', addValues);
}
function addValues(){
var allValues = [];
for (var i = 0; i < sliders.length; i++) {
console.log(allValues.push(sliders[i].noUiSlider.get()));
};
console.log(allValues);
}
Here is a working model that will allow you to have multiple noUiSliders and slider values on the same page.
Here is the html for 3 sliders. You can include as many as you like.
<div class="slider"></div>
<div class="value">0</div>
<div class="slider"></div>
<div class="value">0</div>
<div class="slider"></div>
<div class="value">0</div>
Here is the javascript that will create the sliders and add the correct value to the corresponding slider when the slider is moved.
$(function () {
var sliders = document.getElementsByClassName('slider');
var slideval = document.getElementsByClassName('value');
for (var i = 0; i < sliders.length; i++) {
noUiSlider.create(sliders[i], {
start: 0,
connect: [true, false],
range: {
'min': 0,
'10%': 1,
'20%': 2,
'30%': 3,
'40%': 4,
'50%': 5,
'60%': 6,
'70%': 7,
'80%': 8,
'90%': 9,
'max': 10
},
snap: true,
});
sliders[i].noUiSlider.on('slide', function () {
var allValues = [];
for (var i = 0; i < sliders.length; i++) {
// store values in array to pass through ajax...
allValues.push(sliders[i].noUiSlider.get());
// assign the slider value to the corresponding noUiSlider
slideval[i].innerHTML = sliders[i].noUiSlider.get();
};
});
}
});
This one works:
var sliders = document.getElementsByClassName('sliders');
for ( var i = 0; i < sliders.length; i++ ) {
noUiSlider.create(sliders[i], {
start: 10,
step: 1,
connect: "lower",
orientation: "horizontal",
range: {
'min': 0,
'max': 100
},
});
sliders[i].noUiSlider.on('slide', addValues);
}
function addValues(){
var allValues = [];
for (var i = 0; i < sliders.length; i++) {
allValues.push(sliders[i].noUiSlider.get());
};
for (var j = 0; j < allValues.length; j++) {
document.getElementsByClassName('slider-value')[j].innerHTML = allValues[j];
}
}
<div class="slider-value"></div>
<div class="sliders"></div>
<div class="slider-value"></div>
<div class="sliders"></div>
<div class="slider-value"></div>
<div class="sliders"></div>
<div class="slider-value"></div>
<div class="sliders"></div>
Related
I have this code:
for(i = 0; i < 1; i++){
$("body").append(questions[i]);
for(j = 0; j < prompts.length; j++){
var temp_id = "slider-" + j;
var temp_num_text_id = "slider-text-" + j;
var temptextdiv = $("<h5>50</h5>");
$("body").append(temptextdiv);
temptextdiv.attr('id', temp_num_text_id);
var tempdiv = $("<div></div>");
$("body").append(tempdiv);
tempdiv.attr('id', temp_id);
$("#" + temp_id).slider({
min: 0,
max: 100,
value: 50,
slide: function(event, ui){
$("#" + temp_num_text_id).html(ui.value);
}
});
}
}
The goal is to have one question with various prompts about the question, each being a ranking done on a jquery-ui slider. My trouble is trying to 'link' the text that displays the value of the slider with the slider itself. When I was testing this out with one slider it was as simple as the function within the declaration of the slider object shown above, but when this is done within a loop, all of the sliders only change the text value of the last slider. As I understand it from similar questions others have had, every iteration of the loop overwrites the previous declaration. How do I avoid this?
As I mentioned in the comment, all id attributes should be unique. This can be done by assigning a consecutive number to the ids.
For example: https://jsfiddle.net/Twisty/vLut8ysw/
JavaScript
var questions = [
"<div id='q1'>Question 1</div>",
"<div id='q2'>Question 2</div>",
"<div id='q3'>Question 3</div>",
"<div id='q4'>Question 4</div>",
"<div id='q5'>Question 5</div>",
];
var prompts = [
25,
50,
75,
];
var promptIds = 0;
$(function() {
$.each(questions, function(q, i) {
$(i).appendTo("body");
$.each(prompts, function(p, j) {
var temptextdiv = $("<h5>", {
id: "p-slider-text-" + promptIds
}).html(j).appendTo("body");
var tempdiv = $("<div></div>", {
id: "p-slider-" + promptIds
}).appendTo("body");
tempdiv.slider({
min: 0,
max: 100,
value: j,
slide: function(event, ui) {
temptextdiv.html(ui.value);
}
});
promptIds++;
});
});
});
This will create unique ids for each slider.
jsfiddle link : http://jsfiddle.net/u9gpsb8h/20/
i want to filter table based on jquery label slider .
something like query show tr where td1
i have two slider 1 and 2, now i want to filter table based on slider value slider 1 represents filter a and slider 2 represents filter b.
Problem is that my code works fine when for current label only
below is my js code
$(function() {
$( "#slider1" ).slider({
value: 20,
min:0,
max:20,
orientation: "horizontal",
range: "min",
animate: true,
slide: function( event, ui ) {
$('#s1').html(jQuery('#slider1').slider('value'));
// in this function we can define what happens when a user changes the sliders
var table = document.getElementById("theTable");
for (var i = 1, row; row = table.rows[i]; i++) {
//iterate through rows (we SKIP the first row: counter starts at 1!)
for (var j = 0, col; col = row.cells[j]; j++) {
//iterate through columns: if first column not in range: HIDE, else SHOW
if (j == 0) { // if first column
if ($(col).html() <= jQuery('#slider1').slider('value')) {
// if in interval
$(row).show();
} else {
$(row).hide();
}
}
}
}
}
});
$( "#slider2" ).slider({
value: 20,
min:0,
max:20,
orientation: "horizontal",
range: "min",
animate: true,
slide: function( event, ui ) {
$('#s2').html(jQuery('#slider2').slider('value'));
// in this function we can define what happens when a user changes the sliders
var table = document.getElementById("theTable");
for (var i = 1, row; row = table.rows[i]; i++) {
//iterate through rows (we SKIP the first row: counter starts at 1!)
for (var j = 0, col; col = row.cells[j]; j++) {
//iterate through columns: if first column not in range: HIDE, else SHOW
if (j == 1) { // if first column
if ($(col).html() <= jQuery('#slider2').slider('value')) {
// if in interval
$(row).show();
} else {
$(row).hide();
}
}
}
}
}
});
});
how can i sort table based on slider value i am newbie pls guide me to do this
To get the filters to work together and not compete with each other, you will want to use a specific show/hide indicator for each slider, rather than .show() and .hide(), as it is, each one will overwrite the other.
So Slider A will filter results, and Slider B will further filter those results, rather than reset.
Add this to your CSS:
.slider1Hide{
display:none;
}
.slider2Hide {
display:none;
}
And for the JavaScript:
$(function () {
$("#slider1").slider({
value: 20,
min: 0,
max: 20,
orientation: "horizontal",
range: "min",
animate: true,
slide: function (event, ui) {
$('#s1').html(jQuery('#slider1').slider('value'));
// in this function we can define what happens when a user changes the sliders
var table = document.getElementById("theTable");
for (var i = 1, row; row = table.rows[i]; i++) {
//iterate through rows (we SKIP the first row: counter starts at 1!)
for (var j = 0, col; col = row.cells[j]; j++) {
//iterate through columns: if first column not in range: HIDE, else SHOW
if (j === 0) { // if first column
if ($(col).html() <= ui.value) {
// if in interval
$(row).removeClass('slider1Hide');
} else {
$(row).addClass('slider1Hide');
}
}
}
}
}
});
$("#slider2").slider({
value: 20,
min: 0,
max: 20,
orientation: "horizontal",
range: "min",
animate: true,
slide: function (event, ui) {
$('#s2').html(jQuery('#slider2').slider('value'));
// in this function we can define what happens when a user changes the sliders
var table = document.getElementById("theTable");
for (var i = 1, row; row = table.rows[i]; i++) {
//iterate through rows (we SKIP the first row: counter starts at 1!)
for (var j = 0, col; col = row.cells[j]; j++) {
//iterate through columns: if first column not in range: HIDE, else SHOW
if (j == 1) { // if first column
if ($(col).html() <= ui.value) {
// if in interval
$(row).removeClass('slider2Hide');
} else {
$(row).addClass('slider2Hide');
}
}
}
}
}
});
});
Also as a side note, you can get the value of the slider from the ui in the slide callback by using ui.value rather than jQuery('#slider1').slider('value')
I have multiple sliders that share a common maximum number, I am trying to capture those numbers now into text inputs, but I don't know what I am doing. The JavaScript will print
the numbers into a span no issue as long as the class is "spent". But doesn't work when I've tried it with a text input. I've also made a jsfiddle for this
http://jsfiddle.net/zkyks/
What would the proper way be to bind these values to inputs? I have done this individually by calling the id but I don't know how to to implement that here.
Thanks in advance.
JS
$(
function () {
var
maxValueSlider = 100,
maxValueTotal = 100,
$sliders = $("#eq .work_slider"),
valueSliders = [],
$displaySpentTotal = $('#spent');
$displaySpendable = $('#spendable');
function arraySum(arr) {
var sum = 0,
i;
for (i in arr) sum += arr[i];
return sum;
}
$sliders.each(
function (i, slider) {
var
$slider = $(slider),
$spent = $slider.next('.spent');
valueSliders[i] = 0;
$slider.slider({
range: 'min',
value: 0,
min: 0,
max: maxValueSlider,
step: 5,
animate: true,
orientation: "horizontal",
slide: function (event, ui) {
var
sumRemainder = arraySum(valueSliders) - valueSliders[i],
adjustedValue = Math.min(maxValueTotal - sumRemainder, ui.value);
valueSliders[i] = adjustedValue;
// display the current total
$displaySpentTotal.text(sumRemainder + adjustedValue);
$displaySpendable.text(maxValueTotal - sumRemainder - adjustedValue);
// display the current value
$spent.text(adjustedValue);
// set slider to adjusted value
$slider.slider('value', adjustedValue);
// stop sliding (return false) if value actually required adjustment
return adjustedValue == ui.value;
}
});
});
});
HTML
<div class="panel">
<label class="panel_label">Sliders %:</label>
<div id="eq">
1:<div id="work_slider1" name="work_slider1" class="work_slider"></div>
<input type="text" name="work_spent1" id="work_spent1" />
2:<div id="work_slider2" name="work_slider2" class="work_slider"></div>
<input type="text" name="work_spent2" id="work_spent2" />
3:<div id="work_slider3" name="work_slider3" class="work_slider"></div>
<input type="text" name="work_spent3" id="work_spent3" />
4:<div id="work_slider4" name="work_slider4" class="work_slider"></div>
<input type="text" name="work_spent4" id="work_spent4" />
</div>
<div id="spendable">100</div>
</div>
To make minimal changes in your code, you just need to add one line just before return statement in your slide: function (event, ui) {...}:
$(this).next().val(adjustedValue); //set the value of input next to slider
And remove the <br/> between <div> and <input>.
Or keep one <br/> and use:
$(this).next().next().val(adjustedValue);
Working example: http://jsfiddle.net/k8UkE/
Did some code edititing for the first slider. See if it helps
Replaced following // $displaySpendable = $('#work_spent1')
//$displaySpentTotal.val(sumRemainder + adjustedValue);
$displaySpendable.val(maxValueTotal - sumRemainder - adjustedValue);
$(function () {
var
maxValueSlider = 100,
maxValueTotal = 100,
$sliders = $("#eq .work_slider"),
valueSliders = [],
$displaySpentTotal = $('#spent');
$displaySpendable = $('#work_spent1');
function arraySum(arr) {
var sum = 0,
i;
for (i in arr) sum += arr[i];
return sum;
}
$sliders.each(
function (i, slider) {
var
$slider = $(slider),
$spent = $slider.next('.spent');
valueSliders[i] = 0;
$slider.slider({
range: 'min',
value: 0,
min: 0,
max: maxValueSlider,
step: 5,
animate: true,
orientation: "horizontal",
slide: function (event, ui) {
var
sumRemainder = arraySum(valueSliders) - valueSliders[i],
adjustedValue = Math.min(maxValueTotal - sumRemainder, ui.value);
valueSliders[i] = adjustedValue;
// display the current total
**$displaySpentTotal.val(sumRemainder + adjustedValue);
$displaySpendable.val(maxValueTotal - sumRemainder - adjustedValue);**
// display the current value
$spent.text(adjustedValue);
// set slider to adjusted value
$slider.slider('value', adjustedValue);
// stop sliding (return false) if value actually required adjustment
return adjustedValue == ui.value;
}
});
});
});
I have been searching and trying to find a solution for this but unfortunately can't get my way round it. I have multiple jQuery UI sliders which all have their values on sliding from an array. At the end I am trying to get the value of the slider and add it up with the other sliders. Here is the code for only two sliders:
$(function () {
var labelArr = new Array(0, 200, 400, 600);
$("#slider").slider({
values: 0,
min: 0,
max: labelArr.length - 1,
slide: function (event, ui) {
mbdata = labelArr[ui.value] + $("#slider_2").slider("value") + $("#slider_3").slider("value") + $("#slider_4").slider("value") + $("#slider_5").slider("value") + $("#slider_6").slider("value");
g1.refresh(mbdata);
}
});
});
$(function () {
var labelArr = new Array(0, 300, 900, 1200);
$("#slider_2").slider({
value: 0,
min: 0,
max: labelArr.length - 1,
slide: function (event, ui) {
mbdata = labelArr[ui.value] + $("#slider_3").slider("value") + $("#slider").slider("value") + $("#slider_4").slider("value") + $("#slider_5").slider("value") + $("#slider_6").slider("value");
g1.refresh(mbdata);
}
});
});
The first slider works good, but unfortunately when I slide the second one the values are not showing the array ones but simply 1 , 2 , 3
Can someone please try giving me some help
Many thanks
I changed the whole script, but hopefully its doing now what you want it to do - just extend the sliderRangesValues Array (and add a new div with the right ID) to add more slider and values. Its now a much more flexible script:
http://jsfiddle.net/fKUAe/1/
Dummy HTML:
<div id="slider0" class="slider"></div>
<div id="slider1" class="slider"></div>
<div id="slider2" class="slider"></div>
<input type="text" id="slidervalues" value="0" />
jQuery:
var sliderRangeValues = [
[0, 100, 200, 300],
[0, 300, 600, 900],
[0, 1, 2, 3, 4, 5]
];
function getAllSliderValues(sliderCount) {
var sliderValues = 0;
for (var i = 0; i < sliderCount; i++) {
var uiValue = $('#slider' + i).slider('value');
sliderValues += sliderRangeValues[i][uiValue];
}
$('#slidervalues').val(sliderValues);
}
function initSlider(i, sliderCount) {
$('#slider' + i).slider({
values: 0,
min: 0,
max: sliderRangeValues[i].length - 1,
slide: function (event, ui) {
$('#slider' + i).slider('value', ui.value);
getAllSliderValues(sliderCount);
}
});
}
$(function () {
var sliderCount = $('.slider').length;
for (var i = 0; i < sliderCount; i++) {
initSlider(i, sliderCount);
}
});
First question from me! I have search for possible ways to do this function that i want but could not find anything that helped me out..
I´m new at javascript and working with this as base:
http://jsfiddle.net/danieltulp/gz5gN/42/
When i adjust the Price-slider, i want to se what price in numbers that i´m searching for.
ex. search from 123$ - 900$
And the same with quality.
I´m also trying to make each slider have different min-max and values, is that possible?
$(function() {
var options = {
range: true,
min: 0,
max: 250,
step: 1,
values: [0, 250],
change: function(event, ui) {
var minP = $("#price").slider("values", 0);
var maxP = $("#price").slider("values", 1);
var minQ = $("#quality").slider("values", 0);
var maxQ = $("#quality").slider("values", 1);
showProducts(minP, maxP, minQ, maxQ);
}
};
2 functions are needed. One for each slider's onChange event. After each onChange function execution min and max values for each slider should be updated based on the calculation results. showProducts() will work in its current form.
This UX feels too complicated. Have you considered representing your data filtering somehow else?
This is how i solved it!
fiddle found here: http://jsfiddle.net/5PAa7/28/
i put it in to two variables instead of two functions.
function showProductsP(minP, maxP) {
$(".product-box").filter(function() {
var price = parseInt($(this).data("price"), 10);
if(price >= minP && price <= maxP){
$(this).show();
} else {
$(this).hide();
}
});
$(function() {
var options = {
range: true,
min: 0,
max: 900,
step: 5,
values: [0, 900],
slide: function(event, ui) {
var minP = $("#slider-price").slider("values", 0);
var maxP = $("#slider-price").slider("values", 1);
var minL = $("#slider-length").slider("values", 0);
var maxL = $("#slider-length").slider("values", 1);
$(this).parent().find(".amount").html(ui.values[ 0 ] + " - " + ui.values[ 1 ]);
showProducts(minP, maxP, minL, maxL);
}
};
var options_length = {
range: true,
min: 0,
max: 60,
step: 1,
values: [0, 60],
slide: function(event, ui) {
var minP = $("#slider-price").slider("values", 0);
var maxP = $("#slider-price").slider("values", 1);
var minL = $("#slider-length").slider("values", 0);
var maxL = $("#slider-length").slider("values", 1);
$(this).parent().find(".amount").html(ui.values[ 0 ] + " - " + ui.values[ 1 ]);
showProducts(minP, maxP, minL, maxL);
}
};