jQuery Slider For Visualforce Pages | Techila
2862
single,single-post,postid-2862,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive
 

jQuery Slider For Visualforce Pages

JQUERY SLIDER FOR VISUALFORCE PAGES

jQuery Slider For Visualforce Pages

Talking about the Visualforce component, it has the entire component that can make a decent UI but its only limited to the word “DECENT”, you can create very rich UI using visualforce. What if a simple slider needs to be implemented in a Visualforce page? Alas! There are no such components in the standard Visualforce Library.

Well you can create one for yourself using jQuery Library. Talking about jQuery its very stable JavaScript library with lots of feature! It readily mixes with visualforce to create very rich UI.

So let’s have look on the code to create a simple slider using jQuery.

Visualforce Page

<apex:page controller=”jqSlider_Con”>

<!–Inculding the required jQuery Files and CSS—>

<apex:includeScript value=”{!URLFOR($Resource.jQuery, ‘js/jquery-1.6.2.min.js’)}”/>

<apex:includeScript value=”{!URLFOR($Resource.jQuery, ‘js/jquery-ui-1.8.16.custom.min.js’)}”/>

<apex:stylesheet value=”{!URLFOR($Resource.jQuery, ‘css/ui-lightness/jquery-ui-1.8.16.custom.css’)}”/>

<script>

$j = jQuery.noConflict();

//this function will be called as soon as page load is complete

$j(function(){

//calling the createSlider Method to create the slider in designated location

createSlider(‘slider’,’disp’,’idInputHidden’,0,0,1000);

});

/*  —————————-

*  Method to create the Slider

*  —————————-

*  PARAMETERS : “destination” = The Id of the div where the slider needs to be created

*         “dispOutput” = The Id of the div where the slider value needs to be displayed

*         “idInputHidden” = The Id of the apex inputHidden component

*         “startVal” = initial position of slider

*         “minVal”, “maxVal” = minimum and maximum value of slider

**/

function createSlider(destination,dispOutput,idInputHidden,startVal,minVal,maxVal){

$j(“#”+destination).slider({

range: false,

min: minVal,

max: maxVal,

values: [startVal],

slide: function(event, ui){

//This function executes every time slider is moved and applies the slider values

//to the input fields as well as the output below the slider

$j(“[id$=”+idInputHidden+”]”).val(ui.values[0]);

$j(“#”+dispOutput).html(‘$’ + ui.values[0]);

}

});

//write the initial value in the display div

$j(“#”+dispOutput).html(‘$’ + startVal);

}

</script>

<apex:form >

<apex:pageBlock title=”jQuery Slider”>

<!–Slider will be created Here–>

<div id=”slider”/><br/>

<!–Slider Output here–>

<div id=”disp”/>

<apex:inputHidden value=”{!sSliderField}” id=”idInputHidden”/>

<apex:pageBlockButtons >

<apex:commandButton value=”Submit” action=”{!checkSliderVal}” reRender=”idInputHidden”/>

</apex:pageBlockButtons>

</apex:pageBlock>

</apex:form>

</apex:page>

Controller Class

public class jqSlider_Con {

public String sSliderField { get; set; }

public void checkSliderVal(){

System.debug(‘###————-+’+sSliderField );

}

}

About the method “createSlider”: In this example I have created a function “createSlider”. Which takes in the id of div where the slider neds to be rendered, the id of div where the the values should be displayed, the id of the apex inputHidden(so that values can be passed to controller), the initial value of the slider and range of the slider(max and minimum value).This method can be called multiple times for creating slider in different location of page.

Passing Values to Controller: The values are passed to controller using apex inputHiddent element which assigns the value to a public variable. You can check the communication by clicking the submit button, the value of the slider will assigned to the sSliderField and can be seen in debug logs.

Courtesy: http://blogforce9.blogspot.in/2012/02/jquery-slider-for-visualforce-pages.html

“Explore – Techila Global Services, A Salesforce development company”

Author: techila

No Comments

Post A Comment