Saturday, September 21, 2019

jQuery UI spinner

jQuery UI spinner widgets are used to add a up/down arrow to the input box thus allowing the user to increase and decrease the value in the input box. It facilitates the users to type a value directly, or modify an existing value by spinning with the keyboard, mouse or scrollwheel.
It has also some extended features like:
  • It facilitates you to skip values.
  • It also enables globalized formatting options (i.e. currency, decimals, thousand separators etc.)
Syntax:

You can use spinner () method in two forms:
  1. $(selector, context).spinner (options) Method  
  1. $(selector, context).spinner ("action", params) Method  

First Method

  1. $(selector, context).spinner (options)  
The spinner (options) method specifies that an HTML element and its contents should be treated and managed as spinner. Here ?options? parameter is an object that specifies the appearance and behavior of the spinner elements involved.
You can use one or more options at a time using JavaScript object. In the case of more than one option, you have to separate them using a comma as follows:
  1. $(selector, context).spinner ({option1: value1, option2: value2.....});  
Following is a list of different options that can be used with this method:
OptionDescription
cultureThis option sets the culture to use for parsing and formatting the value. By default its value is null which means the currently set culture in globalize is used.
disabledIf you set this option to true, it disables spinner. By default its value is false.
iconsThis option is used to set icons to use for buttons, matching an icon provided by the jQueryUI CSS framework. By default its value is { down: "ui-icon-triangle-1-s", up: "ui-icon-triangle-1-n" } .
incrementalThis option controls the number of steps taken when holding down a spin button. By default its value is true.
maxThis option is used to identify the maximum allowed value. By default its value is null which means there is no maximum enforced.
minThis option is used to identify the minimum allowed value. By default its value is null which means there is no minimum enforced.
numberFormatThis option specifies a format of numbers passed to globalize, if available. Most common are "n" for a decimal number and "c" for a currency value. By default its value is null.
pageThis option indicates the number of steps to take when paging via the pageup/pagedown methods. By default its value is 10.
stepThis option specifies the size of the step to take when spinning via buttons or via the stepup()/stepdown() methods. The element's step attribute is used if it exists and the option is not explicitly set.

jQuery UI spinner example 1

With no parameter to the spinner() method:
Let's take a simple example to demonstrate spinner widget functionality, passing no parameter to the spinner() method.
  1. <!doctype html>  
  2. <html lang="en">  
  3.    <head>  
  4.       <meta charset="utf-8">  
  5.       <title>jQuery UI Spinner functionality</title>  
  6.       <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  7.       <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  8.       <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  9.       <!-- CSS -->  
  10.       <style type="text/css">  
  11.          #spinner-1 input {width: 100px}  
  12.       </style>  
  13.       <!-- JavaScript -->  
  14.       <script>  
  15.          $(function() {  
  16.             $( "#spinner-1" ).spinner();  
  17.          });  
  18.       </script>  
  19.    </head>  
  20.    <body>  
  21.       <!-- HTML -->   
  22.       <div id="example">  
  23.          <input type="text" id="spinner-1" value="0" />  
  24.       </div>  
  25.    </body>  
  26. </html>  

jQuery UI spinner example 2

Use of min, max and step options:
Let's take an example to demonstrate the usage of options min, max and step in the spinner widget of jQueryUI.
  1. <!doctype html>  
  2. <html lang="en">  
  3.    <head>  
  4.       <meta charset="utf-8">  
  5.       <title>jQuery UI Spinner functionality</title>  
  6.       <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  7.       <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  8.       <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  9.       <!-- CSS -->  
  10.       <style type="text/css">  
  11.          #spinner-2,#spinner-3 input {width: 100px}  
  12.       </style>  
  13.       <!-- Javascript -->  
  14.       <script>  
  15.          $(function() {  
  16.             $( "#spinner-2" ).spinner({  
  17.                min: -50,   
  18.                max: 50  
  19.             });  
  20.             $('#spinner-3').spinner({  
  21.                step: 1000,   
  22.                min: -1000000,   
  23.                max: 1000000  
  24.             });  
  25.          });  
  26.       </script>  
  27.    </head>  
  28.    <body>  
  29.       <!-- HTML -->   
  30.       <div id="example">  
  31.          Spinner Min, Max value set:  
  32.          <input type="text" id="spinner-2" value="0" /><br><br>  
  33.          Spinner increments/decrements in step of of 1000:  
  34.          <input type="text" id="spinner-3" value="0" />  
  35.       </div>  
  36.    </body>  
  37. </html>  

Second Method

  1. $(selector, context).spinner ("action", params)  
The spinner ("action", params) method is used to perform an action on spinner elements, such as enabling/disabling the spinner. The action is specified as a string in the first argument (e.g., "disable" disables the spinner).
Following is a list of different actions that can be used with this method:
ActionDescription
destroyThis action is used to destroy the spinner functionality of an element completely and the elements return to their pre-init state. This method does not accept any arguments.
disableThis action is used to disable the spinner functionality. This method does not accept any arguments.
enableThis action is used to enable the spinner functionality. This method does not accept any arguments.
option( optionName )This action gets the value currently associated with the specified optionName. Here optionName is the name of the option to get.
optionThis action gets an object containing key/value pairs representing the current spinner options hash. This method does not accept any arguments.
option(optionName, value)This action sets the value of the spinner option associated with the specified optionName.
option(options)this action sets one or more options for the spinner.
pageDown( [pages ] )This action decrements the value by the specified number of pages, as defined by the page option.
pageUp( [pages ] )This action increments the value by the specified number of pages, as defined by the page option.
stepDown( [steps ] )This action is used to decrement the value by the specified number of steps.
stepUp( [steps ] )This action increments the value by the specified number of steps.
valueThis action gets the current value as a number. The value is parsed based on the numberFormat and culture options. This method does not accept any arguments.
value(value)This action is used to set the value.
widgetThis action is used to return the spinner widget element; the one annotated with the UI-spinner class name.

jQueryUI spinner() example 3

Use of stepUp, stepDown, pageUp and pageDown:
Let's take an example to demonstrate the use of stepUp, stepDown, pageUp and pageDown method:

  1. <!doctype html>  
  2. <html lang="en">  
  3.    <head>  
  4.       <meta charset="utf-8">  
  5.       <title>jQuery UI Spinner functionality</title>  
  6.       <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  7.       <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  8.       <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  9.       <!-- CSS -->  
  10.       <style type="text/css">  
  11.          #spinner-6 input {width: 100px}  
  12.       </style>  
  13.       <!-- Javascript -->  
  14.       <script>  
  15.          $(function() {  
  16.             $("#spinner-6").spinner();  
  17.             $('button').button();  
  18.   
  19.             $('#stepUp-2').click(function () {  
  20.                $("#spinner-6").spinner("stepUp");  
  21.             });  
  22.   
  23.             $('#stepDown-2').click(function () {  
  24.                $("#spinner-6").spinner("stepDown");  
  25.             });  
  26.   
  27.             $('#pageUp-2').click(function () {  
  28.                $("#spinner-6").spinner("pageUp");  
  29.             });  
  30.   
  31.             $('#pageDown-2').click(function () {  
  32.                $("#spinner-6").spinner("pageDown");  
  33.             });  
  34.          });  
  35.       </script>  
  36.    </head>  
  37.    <body>  
  38.       <!-- HTML -->   
  39.       <input id="spinner-6" />  
  40.       <br/>  
  41.       <button id="stepUp-2">Increment</button>  
  42.       <button id="stepDown-2">Decrement</button>  
  43.       <button id="pageUp-2">Increment Page</button>  
  44.       <button id="pageDown-2">Decrement Page</button>  
  45.    </body>  
  46. </html>  

jQueryUI spinner() example 4

Use of action enable and disable:
Let's take an example to demonstrate the use of action enable and disable:
  1. <!doctype html>  
  2. <html lang="en">  
  3.    <head>  
  4.       <meta charset="utf-8">  
  5.       <title>jQuery UI Spinner functionality</title>  
  6.       <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  7.       <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  8.       <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  9.       <!-- CSS -->  
  10.       <style type="text/css">  
  11.          #spinner-7 input {width: 100px}  
  12.       </style>  
  13.       <!-- Javascript -->  
  14.       <script>  
  15.          $(function() {  
  16.             $("#spinner-7").spinner();  
  17.             $('button').button();  
  18.             $('#stepUp-3').click(function () {  
  19.                $("#spinner-7").spinner("enable");  
  20.             });  
  21.             $('#stepDown-3').click(function () {  
  22.                $("#spinner-7").spinner("disable");  
  23.             });  
  24.          });  
  25.       </script>  
  26.    </head>  
  27.    <body>  
  28.       <!-- HTML -->   
  29.       <input id="spinner-7" />  
  30.       <br/>  
  31.       <button id="stepUp-3">Enable</button>  
  32.       <button id="stepDown-3">Disable</button>  
  33.    </body>  
  34. </html>  

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...