6 Replies Latest reply on Dec 14, 2009 10:35 AM by Bhulka

    How to disable datafield in datagrid?

    Bhulka

      I want to disable datafield in datagrid when it has value something like "N/A" but it should be active if it has any other values apart from "N/A". Please advise and let me know if I have not been clear with my question here and want me to elaborate more. Thanks.

        • 1. Re: How to disable datafield in datagrid?
          =VA=FyreHeart

          What do you mean by "disable"? Disable editing? Hide? Change background color?

           

          Adobe doesn't provide a simple way to loop through each row in a datagrid. What I've done is distort the labelFunction property to alter the properties of a given cell:

           

          <mx:AdvancedDataGridColumn blah=blah labelFunction="formatCell"/>

           

          protected function formatCell(item:Object, column:AdvancedDataGridColumn):String {

               column.editable=false;
               return item[column.dataField]; #//Return the existing string value as-is
          }

           

          -John

          • 2. Re: How to disable datafield in datagrid?
            Bhulka Level 1

            John,

             

            This datafield has link attached to it in datagrid which will take user to different page. What I am trying to do here is for example, if this datafield has value say N/A then link attached to it should not be displayed at all to the user.

             

            If it has any other value than that, then link attached to that datafiled should be displayed which will take user to different screen so disabling and enabling datafield in this case is disappearing and appearing of link attached to it. Please let me know if you want more info on this.

            • 3. Re: How to disable datafield in datagrid?
              =VA=FyreHeart Level 2

              I see. Do you already have an itemRenderer that displays your link properly? If so, use the labelFunction to substitute the standard itemRenderer.

               

              protected function formatCell(item:Object, column:AdvancedDataGridColumn):String {

                   if(item[column.dataField] == "N/A")

                        column.itemRenderer = new ClassFactory(DataGridItemRenderer);

                   else

                        column.itemRenderer = new ClassFactory(myCustomItemRenderer);
                   return item[column.dataField]; #//Return the existing string value as-is
              }

              • 4. Re: How to disable datafield in datagrid?
                Bhulka Level 1

                Thanks for your reply John. I have two questions for you.

                 

                One, I am using normal datagrid not advancedatagrid. In order to use labelFunction I have to use advance datagrid. can I get the same functionality using normal datagrid or I have to switch my code for advance data grid for this?

                 

                Two, this question may sound stupid to you but I have very basic undestanding of actionscript and so I didn't get "DataGridItemRenderer" and "myCustomItemRenderer" that u mentioned in your code. Where do I have to define them in my code?

                 

                Please advise and thanks a lot for all your help.

                • 5. Re: How to disable datafield in datagrid?
                  =VA=FyreHeart Level 2

                  labelFunction also exists in the DataGrid.

                   

                  DataGridItemRenderer is the default item renderer for Flex DataGrids.You should be able to use that line as-is.

                   

                  You can create custom item renderers a number of ways. The easiest is probably just creating a simple MXML file with a canvas and text field. Use a small ActionScript function to override the default setter, like so (This is the itemrenderer I use for URLs in a datagrid cell):

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" height="100%" width="100%" horizontalScrollPolicy="off">
                      <mx:Script>
                          <![CDATA[
                              import mx.controls.dataGridClasses.DataGridColumn;
                             
                              [Bindable] protected var vendorURL:String;
                              override public function set data(value:Object):void {
                                      myURL= '<a href="http://www.example.com/myScript.php?VendorName='+value['VendorName']+'">'+value['VendorName']+'</a>';
                              }
                          ]]>
                      </mx:Script>
                      <mx:Text htmlText="{vendorURL}" height="100%" styleName="hyperlink" width="100%" />
                  </mx:Canvas>

                   

                  See "Customizing Item Renderers" under Day 2 of Flex in a Week Video traning for more info: http://www.adobe.com/devnet/flex/videotraining/

                   

                  -John

                  • 6. Re: How to disable datafield in datagrid?
                    Bhulka Level 1

                    Thank you very much John.

                     

                    I caught up with some critical issues and so couldn't reply you earlier.  Thanks for helping me out.