9 Replies Latest reply on Mar 28, 2007 8:20 PM by dnystwn

    add row + dataGrid

    ljonny18 Level 1
      hi

      I have an "editable" dataGrid within my Flex 2 application....
      I want to allow the user to edit the data within the dataGrid and then save there changes

      I additionally want to allow the user to add a new row to the dataGrid - i.e. using an "Add Row" button which when selected adds a new row to the dataGrid in which they can continue to edit.... is this possible???

      Any suggestions on how to go about doing this (best practice)

      Thanks,
      Jon.
        • 1. Re: add row + dataGrid
          jtan
          The best thing to do is to add an empty item through the dataProvider. Here is an example:

          <?xml version="1.0"?>
          <!-- DataGrid control example. -->
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">
          <mx:Script>
          <![CDATA[
          import mx.collections.*;
          private function addStuff(): void
          {
          var newItem:XML = <employee><name></name><phone></phone><email></email><active></active></employee>;
          IList(dg.dataProvider).addItem(newItem);
          }
          ]]>
          </mx:Script>

          <mx:XMLList id="employees">
          <employee>
          <name>Christina Coenraets</name>
          <phone>555-219-2270</phone>
          <email>ccoenraets@fictitious.com</email>
          <active>true</active>
          </employee>
          <employee>
          <name>Joanne Wall</name>
          <phone>555-219-2012</phone>
          <email>jwall@fictitious.com</email>
          <active>true</active>
          </employee>
          <employee>
          <name>Maurice Smith</name>
          <phone>555-219-2012</phone>
          <email>maurice@fictitious.com</email>
          <active>false</active>
          </employee>
          <employee>
          <name>Mary Jones</name>
          <phone>555-219-2000</phone>
          <email>mjones@fictitious.com</email>
          <active>true</active>
          </employee>
          </mx:XMLList>

          <mx:DataGrid id="dg" width="100%" height="100%" dataProvider="{employees}" editable="true">
          <mx:columns>
          <mx:DataGridColumn dataField="name" headerText="Name"/>
          <mx:DataGridColumn dataField="phone" headerText="Phone"/>
          <mx:DataGridColumn dataField="email" headerText="Email"/>
          </mx:columns>
          </mx:DataGrid>

          <mx:Button label="add item" click="addStuff()" />

          </mx:Application>

          HTH,
          Joan Lafferty
          Flex SDK QA
          • 2. Re: add row + dataGrid
            Poums
            Hi,

            I've another question. I try to allow an user to add a row and edit it.
            No problem to add the row. But i can't find how to set focus on the first new cell.
            Can you help me ?
            Thanks a lot,
            Denis
            • 3. Re: add row + dataGrid
              __Voodoo__Child__
              If the new row is at the end of the dataGrid, just set the selectedIndex to the last item in the row. If your datagrid, myDatagrid, has a dataprovider of myDataProvider you can do this by...

              myDatagrid.selectIndex = myDataProvider.length - 1;
              • 4. Re: add row + dataGrid
                ljonny18 Level 1
                cheers guys - Poums, that was going to be my next question also ;)

                Jon.
                • 5. Re: add row + dataGrid
                  Poums Level 1
                  Jon,
                  I've found a way to set focus on the first cell of the new row :

                  //MaketingDatas is the datagrid's dataprovider.
                  var newData:Object = new Object();
                  var cellFocus:Object = new Object();
                  newData.mktg_user = theUserID;
                  newData.mktg_date = new Date();
                  newData.mktg_amount = null;
                  MarketingDatas.addItem(newData);

                  // Set focus on the 2nd column of the new row (1st column not visible)
                  cellFocus.columnIndex = 1;
                  cellFocus.rowIndex = MarketingDatas.length-1;
                  MarketingInvestmentDataGridID.editedItemPosition = cellFocus;


                  Hope this help you,
                  Now, i try to use a datefield to enter mktg_date values ...

                  See you soon !
                  Denis.
                  • 6. Re: add row + dataGrid
                    ljonny18 Level 1
                    thats great thaks Denis! - I have adapted it to fit my application, and it works a treat :)

                    I dont suppose you, or anyone else has any idea how to add an additional "total" ROW (not column) that is always the bottom row of the dataGrid that sums up the total amount of the rest of the columns etc......?

                    any suggestions?

                    Thanks,
                    Jon.
                    • 7. Re: add row + dataGrid
                      Macys_Mike
                      Denis,
                      I tried using the code that you show in your post...and I was unable to get it to work. Are there any settings that I might be missing?
                      Thanks!
                      - Mike
                      • 8. Re: add row + dataGrid
                        Macys_Mike Level 1
                        Denis,
                        Scratch that...I got it working!
                        Thanks,
                        - Mike
                        • 9. Re: add row + dataGrid
                          dnystwn
                          quote:

                          Originally posted by: ljonny18
                          I dont suppose you, or anyone else has any idea how to add an additional "total" ROW (not column) that is always the bottom row of the dataGrid that sums up the total amount of the rest of the columns etc......?

                          any suggestions?

                          Thanks,
                          Jon.


                          Does anyone have any solution for this?

                          thanks,

                          Danny