4 Replies Latest reply: Apr 9, 2009 8:47 AM by jonpor RSS

    Advanced DataGrid and CheckBoxes.. how to enable/disable dynamically

    jonpor

      I have an advanced data grid.  I'm using a checkbox as an itemRenderer in one column.  I want to be able to enable or disable the ability to edit the check box, based on the user's role which is not known until the login process completes.

       

      If name the column and do a myColumn.enabled = false, the check box can still be check or unchecked by the user.   This appears to be some sort of 'it is already rendered' problem or something.

       

       

      <mx:AdvancedDataGrid dataProvider="{myData}"  itemClick="myDG_itemClick(event)"  id="adgMyGrid">
              <mx:columns>

                  <mx:AdvancedDataGridColumn width="10" id="adgColumn" editable="false" visible="true"  sortable="false"   dataField="myData" itemRenderer="mx.controls.CheckBox" />  
              </mx:columns>
          </mx:AdvancedDataGrid>

       

      Using the above, I would assume that the adgColumn should not be editable.  But it remains editable, no matter if I try to set it in code or in the declaration.  What am doing wrong?

        • 1. Re: Advanced DataGrid and CheckBoxes.. how to enable/disable dynamically
          -Hob Community Member

          The column's editable property specifies whether or not the grid should switch from that column's itemRenderer to the itemEditor when a cell in that column gets focus.  The problem lies in that you're using an editable control as the itemRenderer instead of the itemEditor.  If you want the checkbox look for your renderer, just disabled you can do something like this:

           

          <mx:AdvancedDataGrid dataProvider="{myData}"  itemClick="myDG_itemClick(event)"  id="adgMyGrid">
               <mx:columns>
                    <mx:AdvancedDataGridColumn width="10" id="adgColumn" editable="false" visible="true"  sortable="false"   dataField="myData" itemEditor="mx.controls.CheckBox">
                         <itemRenderer>
                              <mx:Component>
                                   <mx:CheckBox enabled="false"/>
                              </mx:Component>
                         </itemRenderer>
                    </mx:AdvancedDataGridColumn>
               </mx:columns>
          </mx:AdvancedDataGrid>
          
          • 2. Re: Advanced DataGrid and CheckBoxes.. how to enable/disable dynamically
            jonpor Community Member

            Well, thank you for that.  That does work to disable the checkbox and corrects my understanding a bit.

             

            But how do you then enable it?  I need to do it dynamically (code wise), based on user credentials.

            • 3. Re: Advanced DataGrid and CheckBoxes.. how to enable/disable dynamically
              Gregory Lafrance Community Member

              You need to have a data field that specifies whether or not user should be able to change the state of the CheckBox. So upon login, modify the ADG data provider. And reference the data provider field for the CheckBox enabled field.

              • 4. Re: Advanced DataGrid and CheckBoxes.. how to enable/disable dynamically
                jonpor Community Member

                Okay that worked.

                 

                So basically if I *get* the datagrid.. anything and everything dynamic needs to be based off the data in the dataprovider.

                 

                So to contribute back..

                 

                I added an .hasEditPermission property to the dataprovider which is set to true/false based on user permissions.

                 

                I made a new MXML component to extend the checkbox  and use it as the renderer/editor (called myCustomCheckBox for the purposes of this example)

                 

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

                and then used it in the Column in the advanceddatagrid:

                 

                  <mx:AdvancedDataGridColumn width="11"  dataField="someBooleanData"  itemRenderer="myCustomCheckBox" rendererIsEditor="true"  >   

                 

                Thank you to all for your help.