7 Replies Latest reply on Jan 5, 2010 11:02 AM by rick@cat

    Set DataGridColumn editable based on row data

    rick@cat Level 1

      I searched for this but did not find a direct answer. People wanted something similar but not exactly the same.

       

      I have several columns in a standard datagrid that I would like to set editable if the data.company_id > 0. Is this possible? Here's my MXML code:

       

      <mx:DataGridColumn headerText="Part Number"
                         dataField="part_number"
                         editable="false"
                         itemRenderer="controls.setTextColor"/>

       

      I tried:

       

      <mx:DataGridColumn headerText="Part Number"
                          dataField="part_number"
                          editable="{data.company_id > 0 ? true : false;}"
                          itemRenderer="controls.setTextColor"/>

       

      But of course that would be too easy, right?

       

      Help greatly appreciated...

      Rick

        • 1. Re: Set DataGridColumn editable based on row data
          Matt Le Fevre Level 4
          But of course that would be too easy, right?

           

          aha, you're in luck

           

          Very quick and rough, but it works like a charm

           

          XML (written from php)

          <?php
          
               echo 
               "
               <thing>
                    <edit>true</edit>
                    <value>10</value>
               </thing>
               <thing>
                    <edit>false</edit>
                    <value>1000</value>
               </thing>
               <thing>
                    <edit>false</edit>
                    <value>550</value>
               </thing> 
               <thing>
                    <edit>true</edit>
                    <value>20</value>
               </thing>
               ";
          
          ?>

           

          Flex

          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="stuff.send()">

           

           

           

              <mx:HTTPService id="stuff" showBusyCursor="true" method="POST" url="http://localhost/stuff.php"/>

           

           

           

              <mx:DataGrid id="blah" x="240" y="93" dataProvider="{stuff.lastResult.thing}" editable="{blah.selectedItem.edit}">

           

                  <mx:columns>

           

                      <mx:DataGridColumn dataField="value" editable="{data.edit}"/>

           

                  </mx:columns>

           

              </mx:DataGrid>

           

           

           

           

          </mx:Application>

           

          so yeah, i'd assume you'd have to edit the actually datagrid editable properties, rather than just the datagrid column. If your function doesn't work in there then just feed it a direct boolean value like i did.

          1 person found this helpful
          • 2. Re: Set DataGridColumn editable based on row data
            rick@cat Level 1

            Grizzzzzzz,

            Thanks for the reply. No, for some odd reason it did not work for me. Now the fields on all rows are editable even though my XML data has "false" in it. I even added a column to display the "allow_edit" field I added to verify I was getting the right stuff back from the php side.

             

            I know have:

             

            <part id="30">
              <company_id>0</company_id>
              <part_number>TDTO</part_number>
              <description>DRIVE TRAIN OIL</description>
              <allow_edit>false</allow_edit>
            </part>

             

            And:

             

            <mx:DataGridColumn

                 headerText="Part Number"
                 dataField="part_number"
                 editable="{data.allow_edit}"
                 itemRenderer="controls.setTextColor"/>

             

            Did not work in flex 3.4,

            Rick

            • 3. Re: Set DataGridColumn editable based on row data
              Flex harUI Adobe Employee

              The editable property is not watched for changes.  Use ITEM_EDIT_BEGINNING event instead.

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

              Blog: http://blogs.adobe.com/aharui

              • 4. Re: Set DataGridColumn editable based on row data
                Matt Le Fevre Level 4

                rick@cat wrote:

                 

                Grizzzzzzz,

                Thanks for the reply. No, for some odd reason it did not work for me. Now the fields on all rows are editable even though my XML data has "false" in it. I even added a column to display the "allow_edit" field I added to verify I was getting the right stuff back from the php side.

                 

                I know have:

                 

                <part id="30">
                  <company_id>0</company_id>
                  <part_number>TDTO</part_number>
                  <description>DRIVE TRAIN OIL</description>
                  <allow_edit>false</allow_edit>
                </part>

                 

                And:

                 

                <mx:DataGridColumn

                     headerText="Part Number"
                     dataField="part_number"
                     editable="{data.allow_edit}"
                     itemRenderer="controls.setTextColor"/>

                 

                Did not work in flex 3.4,

                Rick

                 

                did you add it to the actual DataGrid component like i mentioned, or just the DataGridColumn?

                 

                <mx:DataGrid id="blah" x="240" y="93" dataProvider="{stuff.lastResult.thing}" editable="{blah.selectedItem.edit}">

                 

                        <mx:columns>

                 

                            <mx:DataGridColumn dataField="value" editable="{data.edit}"/>

                 

                        </mx:columns>

                    </mx:DataGrid>


                it does work, i'm sitting here playing with it

                • 5. Re: Set DataGridColumn editable based on row data
                  rick@cat Level 1

                  Grzzzz,

                  The grid itself is editable. I have one column that is always editable regardless of who the company is.

                   

                  Alex,

                  I was afraid of that... Some things are just not easy, are they?

                   

                  Rick

                  • 6. Re: Set DataGridColumn editable based on row data
                    Matt Le Fevre Level 4

                    rick@cat wrote:

                     

                    Grzzzz,

                    I have one column that is always editable regardless of who the company is.

                     

                     

                    Ah, thats a bummer.

                    • 7. Re: Set DataGridColumn editable based on row data
                      rick@cat Level 1

                      For other folks. This is what I wound up with:

                       

                      itemEditBeginning="partsEditBegin(event)"

                       

                       

                      private function partsEditBegin(event:DataGridEvent):void{
                          var isEditable:String =
                              dgParts.dataProvider.getItemAt(event.rowIndex)['allow_edit'];
                          if(event.columnIndex > 2 && isEditable == 'false'){
                               dgParts.destroyItemEditor();
                               event.preventDefault();
                          }
                      }

                       

                      So, both answers were helpful but Alex is once again correct!

                       

                      Thanks guys!

                      Rick