English 中文(简体)
JqueryUI - Selectable
  • 时间:2024-11-03

JqueryUI - Selectable


Previous Page Next Page  

jQueryUI provides selectable() method to select DOM element inspanidually or in a group. With this method elements can be selected by dragging a box (sometimes called a lasso) with the mouse over the elements. Also, elements can be selected by cpcking or dragging while holding the Ctrl/Meta key, allowing for multiple (non-contiguous) selections.

Syntax

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

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

The selectable (options) method declares that an HTML element contains selectable items. The options parameter is an object that specifies the behavior of the elements involved when selecting.

Syntax

$(selector, context).selectable (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).selectable({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 is tells which element the selection helper (the lasso) should be appended to. By default its value is body.

Option - appendTo

This option is tells which element the selection helper (the lasso) should be appended to. By default its value is body.

Syntax

$( ".selector" ).selectable({ appendTo: "#identifier" });
2

This option if set to true, the position and size of each selectable item is computed at the beginning of a select operation. By default its value is true.

Option - autoRefresh

This option if set to true, the position and size of each selectable item is computed at the beginning of a select operation. By default its value is true. If you have many items, you may want to set this to false and call the refresh() method manually.

Syntax

$( ".selector" ).selectable({ autoRefresh: false });
3

This option forbids selecting if you start selection of elements. By default its value is input,textarea,button,select,option.

Option - cancel

This option forbids selecting if you start selection of elements. By default its value is input,textarea,button,select,option.

Syntax

$( ".selector" ).selectable({ cancel: "a,.cancel" });
4

This option is used to set time in milpseconds and defines when the selecting should start. This can be used to prevent unwanted selections. By default its value is 0.

Option - delay

This option is used to set time in milpseconds and defines when the selecting should start. This can be used to prevent unwanted selections. By default its value is 0.

Syntax

$( ".selector" ).selectable({ delay: 150 });
5

This option when set to true, disables the selection mechanism. Users cannot select the elements until the mechanism is restored using the selectable ("enable") instruction. By default its value is false.

Option - disabled

This option when set to true, disables the selection mechanism. Users cannot select the elements until the mechanism is restored using the selectable ("enable") instruction. By default its value is false.

Syntax

$( ".selector" ).selectable({ disabled: true });
6

This option is the distance (in pixels) the mouse must move to consider the selection in progress. This is useful, for example, to prevent simple cpcks from being interpreted as a group selection. By default its value is 0.

Option - distance

This option is the distance (in pixels) the mouse must move to consider the selection in progress. This is useful, for example, to prevent simple cpcks from being interpreted as a group selection. By default its value is 0.

Syntax

$( ".selector" ).selectable({ distance: 30 });
7

This option is a selector indicating which elements can be part of the selection. By default its value is *.

Option - filter

This option is a selector indicating which elements can be part of the selection. By default its value is *.

Syntax

$( ".selector" ).selectable({ filter: "p" });
8

This option specifies which mode to use for testing whether the selection helper (the lasso) should select an item. By default its value is touch.

Option - tolerance

This option specifies which mode to use for testing whether the selection helper (the lasso) should select an item. By default its value is touch.

This can be of type −

    fit − This type indicates a drag selection must completely encompass an element for it to be selected.

    touch − This type indicates the drag rectangle only needs to intersect any portion of the selectable item.

Syntax

$( ".selector" ).selectable({ tolerance: "fit" });

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

Default Functionapty

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

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI selectable-1</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>
      
      <style>
         #selectable-1 .ui-selecting { background: #707070 ; }
         #selectable-1 .ui-selected { background: #EEEEEE; color: #000000; }
         #selectable-1 { pst-style-type: none; margin: 0; 
            padding: 0; width: 20%; }
         #selectable-1 p { margin: 3px; padding: 0.4em; 
            font-size: 16px; height: 18px; }
         .ui-widget-content {
            background: #cedc98;
            border: 1px sopd #DDDDDD;
            color: #333333;
         }
      </style>
      <script>
         $(function() {
            $( "#selectable-1" ).selectable();
         });
      </script>
   </head>
   
   <body>
      <ol id = "selectable-1">
         <p class = "ui-widget-content">Product 1</p>
         <p class = "ui-widget-content">Product 2</p>
         <p class = "ui-widget-content">Product 3</p>
         <p class = "ui-widget-content">Product 4</p>
         <p class = "ui-widget-content">Product 5</p>
         <p class = "ui-widget-content">Product 6</p>
         <p class = "ui-widget-content">Product 7</p>
      </ol> 
   </body>
</html>

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