4 Replies Latest reply on Jul 27, 2011 6:08 AM by j@i

    datagrid item editors

    j@i Level 1

      hi everyone,

       

      i'm a beginer,i'm datagrid item editors.

      i'm preparing a marks sheets with name,subjects as colomns.i have used textinput as item renderer to enter marks in grid.

       

      i'm trying to get sum of  sum of subject in last column soon after we finish the editing.

       

      could any one plz help me on it.

       

      <?xml version="1.0" encoding="utf-8"?>

      <s:Panel xmlns:fx="http://ns.adobe.com/mxml/2009"

      xmlns:s="library://ns.adobe.com/flex/spark"

      xmlns:mx="library://ns.adobe.com/flex/mx" verticalCenter="0" horizontalCenter="0">

      <fx:Declarations>

      <!-- Place non-visual elements (e.g., services, value objects) here -->

      </fx:Declarations>

      <fx:Script>

      <![CDATA[

      import mx.collections.ArrayCollection;

      import mx.events.DataGridEvent;

      [Bindable]

      public var studentArray:ArrayCollection = new ArrayCollection([{name:"XXXX", maths:00, science:00, english:00, total:00},

      {name:"YYYY", maths:00, science:00, english:00, total:00}, {name:"ZZZZ", maths:00, science:00, english:00, total:00},

      {name:"MMMM", maths:00, science:00, english:00, total:00}]);

       

      protected function marksSheet_itemEditEndHandler(event:DataGridEvent):void

      {

       

       

      }

       

      ]]>

      </fx:Script>

       

      <mx:DataGrid id="marksSheet" editable="true" verticalCenter="0" horizontalCenter="0"

      rowCount="4" dataProvider="{studentArray}" width="90%"

      itemEditEnd="marksSheet_itemEditEndHandler(event)">

      <mx:columns>

      <mx:DataGridColumn id="col1" dataField="name" headerText="Name" editable="false" />

      <mx:DataGridColumn id="maths" dataField="maths" headerText="Maths"

         itemEditor="mx.controls.TextInput"/>

      <mx:DataGridColumn id="science" dataField="science" headerText="Science" 

         itemEditor="mx.controls.TextInput"/>

      <mx:DataGridColumn id="english" dataField="english" headerText="English"

         itemEditor="mx.controls.TextInput"/>

      <mx:DataGridColumn id="total" dataField="total" headerText="Total" editable="true">

      <mx:itemRenderer>

      <fx:Component>

      <s:Label/>

      </fx:Component>

      </mx:itemRenderer>

      </mx:DataGridColumn>

      </mx:columns>

      </mx:DataGrid>

      </s:Panel>

      thankyou,

        • 1. Re: datagrid item editors
          Kanchan Ladwani Level 2

          Hello

           

          Following code may help you it worked for me

           

           

           

          <?xml version="1.0" encoding="utf-8"?>
          <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                         xmlns:s="library://ns.adobe.com/flex/spark"
                         xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
                  <fx:Script>
                      <![CDATA[
                          import mx.collections.ArrayCollection;
                          import mx.controls.dataGridClasses.DataGridItemRenderer;
                          import mx.controls.listClasses.ListItemRenderer;
                          import mx.events.DataGridEvent;
                          import mx.events.DragEvent;
                          import mx.events.FlexEvent;
                          [Bindable]
                          public var ar:ArrayCollection = new ArrayCollection([
                              {Type:'Normal',  Cost:0},
                              {Type:'None',  Cost:-2},
                              {Type:'Enhanced', Cost:5},
                              {Type:'Infrared',  Cost:5}
                          ]);
                         
                          private function total():void {
                             
                              calculateTotal();                                       
                          }
                         
                          private function calculateTotal():void {
                              var timer:Timer = new Timer(0, 1);
                             
                              timer.addEventListener(TimerEvent.TIMER_COMPLETE, function(event:TimerEvent):void {
                                  var total:Number = 0;
                                 
                                  for each (var row:Object in destgrid.dataProvider) {
                                      total += Number(row.Cost);
                                  }
                                  text1.text = total.toString();
                              });
                              timer.start();
                          }
                      ]]>
                  </fx:Script>   
                      <mx:VBox>   
                         
                          <mx:DataGrid id="destgrid" dataProvider="{ar}">       
                              <mx:columns>
                                  <mx:DataGridColumn dataField="Type" />
                                  <mx:DataGridColumn id="VCOST" dataField="Cost" />
                              </mx:columns>   
                          </mx:DataGrid>
                      </mx:VBox>
                 
                  <mx:Text x="115" y="304" height="40" color="#00B4FF" fontWeight="bold" fontSize="16" id="text1" fontFamily="Eurostile" width="297"/>
                 
                  <mx:Button id="b1"
                             label="calculate"
                             click="total()"
                             x="279" y="191"/>
             
          </s:Application>

           

          Thanks and Regards,

          ---------------------------------------------------------------------- -----------------------------

          Kanchan Ladwani | kladwani@infocepts.com | www.infocepts.com

          ---------------------------------------------------------------------- -----------------------------

          1 person found this helpful
          • 2. Re: datagrid item editors
            EvyatarBH Level 3

            Your example sums a specific field on all DG rows (single column value from all rows).

            What he's looking for is to create a total column that sums the values from all previous columns.

             

            Anyway, a quick Google search found me this page -

            http://stackoverflow.com/questions/3706251/creating-a-total-column-in-datagrid-flex

             

            See if that helps

            • 3. Re: datagrid item editors
              j@i Level 1

              Thankyou very much.

              • 4. Re: datagrid item editors
                j@i Level 1

                thiankyou very much.