- jQuery UI Cookbook
- Adam Boduch
- 563字
- 2021-08-13 16:40:52
Controlling the spacing with buttonsets
The jQuery UI toolkit gives developers a container widget used for working with groups of buttons called a buttonset. You would use a buttonset for things like groups of checkboxes or groups of radio buttons—things that form a collaborative set.
The default appearance of the buttonset is of a unified whole. That is, the goal is to form a seemingly single widget out of several buttons. By default, the buttonset widget has no spacing controls for the developer. The buttons within the set, by default, are all stacked up against one another. This may not be what we're after, depending on the context of the buttonset widget in our overall user interface.
Getting ready
To better illustrate the spacing constraints we're presented with, let's build a buttonset widget and look at the result before we go about trying to resolve the issue. We'll use the following group of radio buttons as our markup:
<div> <input type="radio" id="first" name="items" /> <label for="first">Item 1</label> <input type="radio" id="second" name="items" /> <label for="second">Item 2</label> <input type="radio" id="third" name="items" /> <label for="third">Item 3</label> <input type="radio" id="fourth" name="items"/> <label for="fourth">Item 4</label> </div>
And we'll create the buttonset widget as follows:
$(function() { $( "div" ).buttonset(); });
Here is what our buttonset looks like. Notice that this widget still exhibits radio button functionality. Here the third item is selected, but will become deselected if I were to click elsewhere in the widget.

How to do it...
Now, there is nothing wrong with the default presentation of the buttonset widget. The only potential challenge we might face is if we have a spacing theme happening elsewhere in the application—the stacked against one another look of the widget just might not fit in from an aesthetic perspective. We can tackle this issue with relatively little effort by extending the widget with an option that allows us to "explode" the buttons so that they're no longer touching.
We'll implement this new exploding buttonset
capability by extending the buttonset widget and adding a new option that will enable this behavior. The HTML stays the same as previous, but here is the new JavaScript code.
(function( $, undefined ) { $.widget( "ab.buttonset", $.ui.buttonset, { options: { exploded: false }, refresh: function() { this._super("refresh"); if ( !this.options.exploded ) { return; } var buttons = this.buttons.map(function() { return $( this ).button( "widget" )[ 0 ]; }); this.element.addClass( "ui-buttonset-exploded" ); buttons.removeClass( "ui-corner-left ui-corner-right" ) .addClass( "ui-corner-all" ); } }); })( jQuery ); $(function() { $( "div" ).buttonset( { exploded: true } ); });
We'll want to include the following CSS on the page—including it via a new stylesheet is the recommended practice here:
.ui-buttonset-exploded .ui-button { margin: 1px; }

How it works...
Our extension of the buttonset widget adds the exploded
option, allowing the programmer using the widget to specify whether they would like the individual buttons separated from one another or not. We also override the refresh()
method here in order to alter the display if the exploded
option is true
.
To do this, we create a jQuery object representing all the individual buttons in the buttonset. The reason we're using map()
here is because of a work-around required with checkbox
and radio
buttons. The ui-buttonset-exploded
class adds the margin
that we're looking for between the buttons—it explodes them outward. Next, we remove the ui-corner-left
and ui-corner-right
classes from any of the buttons, and add the ui-corner-all
class to each button that gives them each their own independent borders.
- 嵌入式Linux開發技術
- 嵌入式Linux系統開發:基于Yocto Project
- Windows Phone應用程序開發
- RESS Essentials
- Joomla! 3 Template Essentials
- Cassandra 3.x High Availability(Second Edition)
- Learn CUDA Programming
- Windows Server 2008組網技術與實訓(第3版)
- OpenVZ Essentials
- 大學計算機應用基礎實踐教程(Windows 7+MS Office 2010)
- 電腦辦公(Windows 10 + Office 2016)入門與提高(超值版)
- Azure Resource Manager Templates Quick Start Guide
- 鴻蒙入門:HarmonyOS應用開發
- UNIX傳奇:歷史與回憶
- Windows8應用開發權威指南