Flex Examples
Displaying CheckBox controls using the Repeater in Flex
The following example shows how you can display a series of CheckBox controls using the Flex Repeater with an Array data provider.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/05/29/displaying-checkbox-controls-using-the-repeater-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.controls.CheckBox;
private function button_click(evt:Event):void {
var selArr:Array = [];
var idx:int;
var len:int = arr.length;
for (idx=0; idx<len; idx++) {
if (checkBox[idx].selected) {
selArr.push(checkBox[idx].label);
}
}
var title:String = selArr.length + " of " + len + " item(s) selected.";
var message:String = selArr.join("\\n");
Alert.show(message, title);
}
private function doFilter(element:*, index:int, arr:Array):Boolean {
return element.selected;
}
private function checkBox_change(evt:Event):void {
var ch:CheckBox = evt.currentTarget as CheckBox;
Alert.show(ch.getRepeaterItem().data);
}
]]>
</mx:Script>
<mx:Array id="arr">
<mx:Object label="Red" data="red" />
<mx:Object label="Orange" data="haloOrange" />
<mx:Object label="Yellow" data="yellow" />
<mx:Object label="Green" data="haloGreen" />
<mx:Object label="Blue" data="haloBlue" />
</mx:Array>
<mx:Panel id="panel" width="50%" paddingLeft="10" verticalGap="10">
<mx:Repeater id="checkBoxRepeater" dataProvider="{arr}">
<mx:CheckBox id="checkBox"
label="{checkBoxRepeater.currentItem.label}"
data="{checkBoxRepeater.currentItem.data}"
change="checkBox_change(event);" />
</mx:Repeater>
<mx:ControlBar horizontalAlign="right">
<mx:Button id="button"
label="Click me"
emphasized="true"
click="button_click(event);" />
</mx:ControlBar>
</mx:Panel>
</mx:Application>
View source is enabled in the following example.
Peter deHaan
Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
14 Responses to Displaying CheckBox controls using the Repeater in Flex
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


oh common! no demo app today? gettin lazy now huh? ;P
steve,
SWF posted.
Peter
good!
clean!
How can add checkbox in a Datagrid…? and print the content of the row for a selected checkbox.
Awesome example, thanks for the help!
How can we select and delect all checkboxes in repeater by clicking on a button
Taqi,
Something like this:
<?xml version="1.0" encoding="utf-8"?> <mx:Application name="Repeater_CheckBox_test" xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white"> <mx:Script> <![CDATA[ import mx.controls.CheckBox; private function button_click(evt:Event):void { var idx:int; var len:int = arr.length; for (idx=0; idx<len; idx++) { // invert: //checkBox[idx].selected = !checkBox[idx].selected; // toggle: checkBox[idx].selected = button.selected; } } ]]> </mx:Script> <mx:Array id="arr"> <mx:Object label="Red" data="red" /> <mx:Object label="Orange" data="haloOrange" /> <mx:Object label="Yellow" data="yellow" /> <mx:Object label="Green" data="haloGreen" /> <mx:Object label="Blue" data="haloBlue" /> </mx:Array> <mx:Panel id="panel" width="50%" paddingLeft="10" verticalGap="10"> <mx:Repeater id="checkBoxRepeater" dataProvider="{arr}"> <mx:CheckBox id="checkBox" label="{checkBoxRepeater.currentItem.label}" data="{checkBoxRepeater.currentItem.data}" /> </mx:Repeater> <mx:ControlBar horizontalAlign="right"> <mx:Button id="button" label="Toggle all checkboxes" toggle="true" emphasized="true" click="button_click(event);" /> </mx:ControlBar> </mx:Panel> </mx:Application>You can comment/uncomment some of the ActionScript logic based on if you want to toggle or invert the selected items.
Peter
Hello friend, first let me say that the post is very good and I’m here again to ask you one more help. I’m trying to use mx: Repeater for a repetition of a Canvas, the problem is that I’m doing it and seeking the data in a database with php and I do not know how. Can you help me? Below the code to better understand:
Flex in the file:
<mx:Repeater id=”myRepeater” dataProvider=”{dataCont.lastResult.xmlrecCont.xmlRepeater}” >
<mx:Canvas width=”80%” height=”80″ backgroundColor=”#E3E3E3″ left=”10″ top=”85″ themeColor=”#4BB6B7″ borderColor=”#BFBEBE” cornerRadius=”5″ borderStyle=”solid” borderThickness=”1″>
<mx:Label text=”RI:” left=”5″ top=”5″ width=”28″ height=”20″ fontWeight=”bold”/>
<mx:Label text=”Tipo:” left=”5″ top=”30″ width=”35″ height=”20″ fontWeight=”bold”/>
<mx:Label text=”Razão Social:” left=”75″ top=”5″ width=”80″ height=”20″ fontWeight=”bold”/>
<mx:Label text=”Data:” width=”40″ height=”20″ right=”138″ top=”30″ fontWeight=”bold”/>
<mx:Label text=”Número Contrato” width=”110″ height=”20″ right=”68″ top=”55″ fontWeight=”bold”/>
<mx:Label text=”Situação:” width=”60″ height=”20″ top=”55″ fontWeight=”bold” left=”5″/>
<mx:Label width=”30″ height=”20″ top=”5″ left=”41″ text=”{dataCont.lastResult.xmlrecCont.xmlRI}” textAlign=”left”/>
<mx:Label width=”80″ height=”20″ top=”30″ left=”41″ text=”{dataCont.lastResult.xmlrecCont.xmlTpo}”/>
<mx:Label width=”80″ height=”20″ top=”55″ left=”75″ text=”{dataCont.lastResult.xmlrecCont.xmlSit}”/>
<mx:Label width=”70″ height=”20″ top=”55″ right=”5″ text=”{dataCont.lastResult.xmlrecCont.xmlId_cont}”/>
<mx:Label height=”20″ top=”5″ text=”{dataCont.lastResult.xmlrecCont.xmlRazso_no}” textAlign=”left” left=”160″ right=”5″/>
<mx:Label width=”135″ height=”20″ right=”5″ top=”30″ text=”{dataCont.lastResult.xmlrecCont.xmlDate}”/>
</mx:Canvas>
</mx:Repeater>
In the PHP file:
<?php
require_once(“conexao.php”);
$id_rep = “1″;
$mysql_select = “SELECT * FROM projcont WHERE id_rep = ‘$id_rep’”;
$mysql_query = mysql_query($mysql_select);
$mysql_fetch = mysql_fetch_array($mysql_query);
$cto = “<xmlrecCont>”;
if($mysql_query) {
$cto .= “<xmlRepeater>”;
$cto .= $mysql_fetch = mysql_fetch_array($mysql_query);
$cto .= “</xmlRepeater>”;
$cto .= “<xmlRI>”;
$cto .= $mysql_fetch['ID_cad'];
$cto .= “</xmlRI>”;
$cto .= “<xmlRazso_no>”;
$cto .= $mysql_fetch['razso_no'];
$cto .= “</xmlRazso_no>”;
$cto .= “<xmlTpo>”;
$cto .= $mysql_fetch['tpo'];
$cto .= “</xmlTpo>”;
$cto .= “<xmlData>”;
$cto .= $mysql_fetch['data'];
$cto .= “</xmlData>”;
$cto .= “<xmlSit>”;
$cto .= $mysql_fetch['sit'];
$cto .= “</xmlSit>”;
$cto .= “<xmlId_cont>”;
$cto .= $mysql_fetch['id_cont'];
$cto .= “</xmlId_cont>”;
} else {
$cto .= “Error mysql_query”;
}
$cto .= “</xmlrecCont>”;
echo($cto);
?>
Thank you already!
@Peter deHaan:
Thanks! your comment is very helpful! Exactly what i needed!
Thanks again!!
great work peter.
Thank alot Peter!
Your example realy hepl me to settle my troubles.
Okay Now.. !!! How to deal with Equally spaced Horizontal Layout for Checkboxes mentioned above.. I need something like this:
Red Orange Yellow Green
Blue Black Violet Pink
.. Consider the above as the layout of the checkboxes
Okay I think I’ve found a clue here..
http://blog.flexexamples.com/2009/03/10/using-the-repeater-class-in-flex-gumbo/
.. still the above doesn’t solve my question..
if I have 8 different labels for the checkboxes, and I am using a single repeater, then how would I divide those 8 components as first 4 and then next 4.. as mentioned in my above comment
What is the purpose of the doFilter function in this example?