Rambling Developer

Recent Posts


Archives


Rambling Developer

Creating A Flex Spark List of Checkbox Bound To XML DataProvider

adminadmin

For the sake of the cookbook recipe, I will assume you already have the data you are working with in XML format from whatever backend solution you choose. The simple XML I will be using is:

<fx:XML format="e4x" id="namesXML">
  <people>
    <person name="Leon"/>
    <person name="Mathilda"/>
    <person name="Stansfield"/>
    <person name="Benny"/>
  </people>
</fx:XML>

Displaying all of the people in this list with checkboxes next to their names involves three parts.

  1. Creating an ItemRenderer Component which contains a Checkbox which will display the person’s name in “namesXML” as the CheckBox label.
  2. Defining the new ItemRenderer to be the default ItemRenderer of our s:List component.
  3. Defining dataProvider of an s:List component to be the “namesXML” data shown above.

First we need to define out ItemRenderer as a new MXML Item Renderer called “CheckBoxItemRenderer”.  The source of the file will be:

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" autoDrawBackground="true">
 
<s:CheckBox label="{data.@name}"/>
 
</s:ItemRenderer>

Notice, that the only component defined within our new MXML ItemRenderer is a s:CheckBox. The label of this checkbox is bound to the @name attribute of an object called data. The data Object is autopopulated by the ItemRenderer class and reflects a single node of the dataprovider which is feeding the list of which the ItemRenderer is a part. Simply, there will be a new instance of the “CheckBoxItemRenderer” for each node in the “namesXML” and the label of the checkbox will be set to the person’s name.

Lastly, we need to define our list which will define it’s itemRenderer as “CheckBoxItemRenderer” and it’s dataProvider as “namesXML”. Since “namesXML” is XML instead of an XMLListCollection which is required to set a dataProvider, we will need to convert the XML to an XMLListCollection. The MXML defining the list is:

<s:List itemRenderer="CheckBoxItemRenderer" dataProvider="{new XMLListCollection(namesXML.children())}"/>

With that definition, you will now have a list of checkboxes with a person’s name next to each checkbox. The list of names is bound to the “namesXML” and can be updated with updates to the namesXML object feeding the dataProvider.

Live demo and source code  you can visit our one of our last website https://www.canlicasinouzmani1.com/

Comments 0
There are currently no comments.