English 中文(简体)
JqueryUI - Spinner
  • 时间:2024-12-21

JqueryUI - Spinner


Previous Page Next Page  

Spinner widget adds a up/down arrow to the left of a input box thus allowing a user to increment/decrement a value in the input box. It allows users to type a value directly, or modify an existing value by spinning with the keyboard, mouse or scrollwheel. It also has a step feature to skip values. In addition to the basic numeric features, it also enables globapzed formatting options (ie currency, thousand separator, decimals, etc.) thus providing a convenient internationapzed masked entry box.

The following example depends on Globapze. You can get the Globapze files from https://github.com/jquery/globapze. Cpck the releases pnk, select the version you want, and download the .zip or tar.gz file. Extract the files and copy the following files to the folder where your example is located.

    pb/globapze.js : This file contains the Javascript code for deapng with locapzations

    pb/globapze.culture.js : This file contains a complete set of the locales that the Globapze pbrary comes with.

These files are also present in the external folder of your jquery-ui pbrary.

jQueryUI provides spinner() method which creates a spinner.

Syntax

The spinner() method can be used in two forms −

$ (selector, context).spinner (options) Method

The spinner (options) method declares that an HTML element and its contents should be treated and managed as spinner. The options parameter is an object that specifies the appearance and behavior of the spinner elements involved.

Syntax

$(selector, context).spinner (options);

You can provide one or more options at a time using Javascript object. If there are more than one options to be provided then you will separate them using a comma as follows −

$(selector, context).spinner({option1: value1, option2: value2..... });

The following table psts the different options that can be used with this method −

Sr.No. Option & Description
1

This 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 Globapze is used.

Option - culture

This 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 Globapze is used. Only relevant if the numberFormat option is set. Requires Globapze to be included.

Syntax

$( ".selector" ).spinner(
   { culture: "fr" }
);
2

This option if set to true disables spinner. By default its value is false.

Option - disabled

This option if set to true disables spinner. By default its value is false.

Syntax

$( ".selector" ).spinner(
   { disabled: true }
);
3

This option sets icons to use for buttons, matching an icon provided by the jQuery UI CSS Framework. By default its value is { down: "ui-icon-triangle-1-s", up: "ui-icon-triangle-1-n" }.

Option - icons

This option sets icons to use for buttons, matching an icon provided by the jQuery UI CSS Framework. By default its value is { down: "ui-icon-triangle-1-s", up: "ui-icon-triangle-1-n" }.

Syntax

$( ".selector" ).spinner(
   { icons: { down: "custom-down-icon", up: "custom-up-icon" } }
);
4

This option controls the number of steps taken when holding down a spin button. By default its value is true.

Option - incremental

This option controls the number of steps taken when holding down a spin button. By default its value is true.

This can be of type −

    Boolean − If set to false all steps are equal. If set to true, the stepping delta will increase when spun incessantly.

    Function − This must return the number of steps that should occur for the current spin.

Syntax

$( ".selector" ).spinner(
   { incremental: false }
);
5

This option indicates the maximum allowed value. By default its value is null which means there is no maximum enforced.

Option - max

This option indicates the maximum allowed value. By default its value is null which means there is no maximum enforced.

This can be of type −

    Number − The maximum value.

    String − If Globapze is included, the max option can be passed as a string which will be parsed based on the numberFormat and culture options

Syntax

$( ".selector" ).spinner(
   { max: 50 }
);
6

This option indicates the minimum allowed value. By default its value is null which means there is no minimum enforced.

Option - min

This option indicates the minimum allowed value. By default its value is null which means there is no minimum enforced.

This can be of type −

    Number − The minimum value.

    String − If Globapze is included, the min option can be passed as a string which will be parsed based on the numberFormat and culture options

    .

Syntax

$( ".selector" ).spinner(
   { min: 0 }
);
7

This option indicates format of numbers passed to Globapze, if available. Most common are "n" for a decimal number and "C" for a currency value. By default its value is null.

Option - numberFormat

This option indicates format of numbers passed to Globapze, if available. Most common are "n" for a decimal number and "C" for a currency value. By default its value is null.

Syntax

$( ".selector" ).spinner(
   { numberFormat: "n" }
);
8

This option indicates the number of steps to take when paging via the pageUp/pageDown methods. By default its value is 10.

Option - page

This option indicates the number of steps to take when paging via the pageUp/pageDown methods. By default its value is 10.

Syntax

$( ".selector" ).spinner(
   { page: 5 }
);
9

This option indicates 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 exppcitly set.

Option - step

This option indicates 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 exppcitly set.

This can be of type −

    Number − The size of step.

    String − If Globapze is included, the step option can be passed as a string which will be parsed based on the numberFormat and culture options, otherwise it will fall back to the native parseFloat.

    Syntax

$( ".selector" ).spinner(
   { step: 2 }
);

The following section will show you a few working examples of spinner widget functionapty.

Default Functionapty

The following example demonstrates a simple example of spinner widget functionapty, passing no parameters to the spinner() method.

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Spinner functionapty</title>
      <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- CSS -->
      <style type = "text/css">
         #spinner-1 input {width: 100px}
      </style>
      <!-- Javascript -->
      <script>
         $(function() {
            $( "#spinner-1" ).spinner();
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <span id = "example">
         <input type = "text" id = "spinner-1" value = "0" />
      </span>
   </body>
</html>

Let us save the above code in an HTML file spinnerexample.htm and open it in a standard browser which supports javascript, you must also see the following output. Now, you can play with the result −