Skip to main content
Participating Frequently
Aug 24, 2010 10:45 AM UTC
Question

Flex 4 -> Datagrid add checkbox to a column

Hello all.

I am trying to add a checkbox to a datagrid column, so that i can use it to delete a record from the array.  My datagrid is as follows

    <mx:DataGrid x="10" y="40" width="903" dataProvider="{questionsArr}" sortableColumns="false" draggableColumns="false" editable="true">
        <mx:columns>
            <mx:DataGridColumn headerText="No." dataField="Number" width="100" editable="false"/>
            <mx:DataGridColumn headerText="Question" dataField="Question" />
            <mx:DataGridColumn headerText="Delete"  width="100" />
        </mx:columns>
    </mx:DataGrid>

The user inputs questions into the array via a textinput box.

But what i want to do is add a checkbox to the "Delete" column.  If anyone could help me with this that would be great !

    This topic is closed to new replies. Start a new post to keep the conversation going.

    11 replies

    Participant
    Aug 1, 2013 8:59 PM UTC

      <mx:DataGridColumn headerText="Date" dataField="date" width="100" editable="true"
     
    editorDataField="selected" rendererIsEditor="true">
        
    <mx:itemRenderer>
           
    <mx:Component>
              
    <mx:CheckBox selected="false">
               </mx:CheckBox>
                           
    </mx:Component>
        
    </mx:itemRenderer>
     
    </mx:DataGridColumn>

    Taken from http://stackoverflow.com/questions/1120841/flex-checkbox-in-datagrid

    Translate
    Amy Blankenship
    Legend
    Jul 25, 2011 3:41 PM UTC

    Check out this post http://www.benclinkinbeard.com/2007/11/efficient-reusable-and-centered-checkbox-renderers-for-datagrids/ .  It contains full code for exactly what you want to do.

    HTH;

    Amy

    Translate
    djh88ukwbAuthor
    Participating Frequently
    Aug 24, 2010 11:40 AM UTC

    Ok, thanks both for your answers.

    I have now gone about doing it the following way:

        <mx:DataGrid x="10" y="40" width="903" dataProvider="{questionsArr}" sortableColumns="false" draggableColumns="false" editable="true">
            <mx:columns>
                <mx:DataGridColumn headerText="No." dataField="Number" width="100" editable="false"/>
                <mx:DataGridColumn headerText="Question" dataField="Question" />
                <mx:DataGridColumn headerText="Delete"  width="100">
                        <mx:itemRenderer>
                            <fx:Component>
                                <mx:HBox horizontalAlign="center" verticalAlign="middle">
                                    <s:CheckBox/>
                                </mx:HBox>
                            </fx:Component>
                        </mx:itemRenderer>
                </mx:DataGridColumn>
            </mx:columns>
        </mx:DataGrid>

    It works fine, as i add a question, a checkbox gets added to the Delete column.  Alls great.

    But when i come to click on the checkbox, the following text gets displayed [object Object]

    Then when i click away from the checkbox the checkbox reappears.  With it either selected or unselected depending on what value it should be.

    Is there anyway so that it dosent display the [object Object] ?

    Translate
    betheflexcoder
    Participant
    Aug 24, 2010 12:23 PM UTC

    Please use the code given by the other guy for check

    box renderer and override the set data method as per your requirement. Read the link I have given all about item renderers for more information.

    Translate
    Aug 24, 2010 11:00 AM UTC

    Hi

                Use check box as an item renderer to the datagrid and then u will get check box for the delete column

                      Here i posting the sample code of  item renderer and datagrid column for ur convieniance... Name that Item renderer as RenderItem.mxml

    for out timebeing..

                <?xml version="1.0" encoding="utf-8"?>
    <mx:CheckBox xmlns:mx="http://www.adobe.com/2006/mxml" c>
        <mx:Script>
            <![CDATA[
               
                override public function set data(value:Object):void
                {
                   super.data=value;

                        // Add ur stuff here              
                }

            ]]>
        </mx:Script>
    </mx:CheckBox>

    and in datagrid u can use this item renderer for the Delete column

       <mx:DataGrid>

                   <mx:columns>

                                  <mx:DatagridColums headerText="Delete" itemRenderer="../RenderItem.mxml"/>

                   </mx:columns>

         <mx:DataGrid>

    Translate
    betheflexcoder
    Participant
    Aug 24, 2010 10:52 AM UTC

    You must add the check box to the column using an itemrenderer.  Look at this link for more info

    http://livedocs.adobe.com/flex/3/html/cellrenderer_3.htmlhttp://blog.flexexamples.com/2008/01/27/using-a-checkbox-control-as-a-list-item-renderer-in-flex/

    Translate