Skip to main content

jQuery noUIslider Pip Label Customization

Recently, I was tasked with creating a slider element for a user to select their credit rating, of which the user can select from among: 'Poor', 'Fair', 'Good', 'Excellent' and 'Not Sure'. 

Several problems presented themselves here:

  • A drop-down box would be better, but that's not what the requirements specified.
  • I already have several numeric sliders on my page and I want them all to match.
  • I selected the jQuery noUi Slider library to create the numeric sliders.
  • noUi Slider does not support string values - only numeric values.
  • The "pips" (slider scale/labels) also does not support string values.
Since the solution involved shifting my mindset, I wanted to document it here for future reference and maybe to help someone facing the same requirements.

Here's my solution:
  • Since there is really no numeric correlation to credit score (especially considering one of the options is "Not Sure"), I will just use an index of 1 through 5.
  • The index will correspond to one of the five ratings (via an array) and will have to match an array in the script which process the form.
  • The pip labels will need to be manually overridden to hide the numeric values.
Given that solution, here is the code I came up with:



// Get a handle to the rating slider var rating_slider = document.getElementById('rating_slider');

// Range for the slider
var rating_range = {
'min': [1],
'max': [5]
};

// Initialize the slider
noUiSlider.create(rating_slider, {
range: rating_range,
start: 3,
step: 1,
connect: 'lower',
pips: {
mode: 'range',
density: 25,
stepped: true
}
});

// Alter the pip labels (and modify/add the middle one)
$('#rating_slider > div.noUi-pips.noUi-pips-horizontal > div:nth-child(2)').text('Not Sure');
$('#rating_slider > div.noUi-pips.noUi-pips-horizontal > div:nth-child(4)') .removeClass('noUi-marker-normal') .addClass('noUi-marker-large') .append('<div class="noUi-value noUi-value-horizontal noUi-value-large" style="left: 50.00000%">Fair</div>');
$('#rating_slider > div.noUi-pips.noUi-pips-horizontal > div:nth-child(7)').text('Excellent');

// Slider event handlers
rating_slider.noUiSlider.on('slide', function (values, handle) {
// Array of score names
var rating_names = ['Not Sure', 'Poor', 'Fair', 'Good', 'Excellent'];

// Get rounded value
var value = Math.round(values[handle]);

// Get score name to show in tool-tip
var text = rating_names[value - 1];

// Set hidden field to value
$('#rating_id').val(value);

// Show score name in tool-tip
$('#rating-tip').text(text).removeClass('hidden');
});

// Function to hide the tool-tip again after a timeout
rating_slider.noUiSlider.on('change', function (values, handle) {
setTimeout(function () {
$('#rating-tip').addClass('hidden');
}, 500);
});

Note: the above code requires jQuery, noUiSlider and Bootstrap 3.

Comments

Popular posts from this blog

Regarding Technology (a departure from WebDev)

I'm departing from WebDev for a minute to blog about technology. I'm as guilty of over-indulging in technology as anyone, especially given that my career demands a technology overload. Today at noon is the solar eclipse and that prompted me to think about the past and the future. I started to think about where the world stands and a song came up on my playlist that seemed perfect for my mood. 27 years ago, Bad Religion wrote a song about the current generation of kids. It would be prophetic if it weren't for the fact that the trend had already begun back then. In any event, it seems now is a good time to look back at the lyrics: 'Cause I'm a twenty-first century digital boy I don't know how to live but I got a lot of toys My daddy's a lazy middle-class intellectual My mommy's on Valium, so ineffectual What child these days does not have multiple devices to play with? Gaming systems, television, computers and tablets are in nearly every home. Kid...

Salesforce Apex: Replacing and/or Removing Apex Classes

Working with code within the constraints of Salesforce's workflow can be a challenge.  First , you have the restriction on changing anything in production.  Second , you have the requirement for 75% or greater unit test coverage.  Third , you have the fact that certain metadata cannot be pushed to production through a changeset (which means the only way to sync record IDs is by re-deploying the development environment from production). Fourth - and this is what I'd like to blog about today - you cannot delete classes from production through the Salesforce UI .  The only way to delete a class from production is to download the class (and its associated metadata file) into the Eclipse IDE (which you have previously set up with the Apex add-on), change the "status" flag to "Deleted", then deploy the changed metadata file to production.  And (here's the kicker)...sometimes it doesn't work because...reasons. So let's start with the workflow of a f...