23 Replies Latest reply on Mar 21, 2012 11:59 AM by Flex harUI

    Datagrid with itemRenderer

    jfb00 Level 3

      Hi,

      I have a datagrid using an itemRenderer that controls the input to be numbers.

      From an external button, i am copying values from one column to multiple columns, the copy is done but it is not refreshing the values on the renderer.

      It is a way to refresh that value?

      Thanks

       

       

      P.D:My renderer component is http://forums.adobe.com/message/3798449#3798449


        • 1. Re: Datagrid with itemRenderer
          jfb00 Level 3

          If I refresh the arrayCollection (the datagrid dataProvider) it works but it copy the same amount to other cells.

          Any ideas?

          • 2. Re: Datagrid with itemRenderer
            jfb00 Level 3

            Hi,

            With verticalScrollPosition i was able to keep my row position and cells no to act strange.

            This datagrid have 7 columns, first column have the description, second column have the number to be copy thru the rest of columns.

            My copy function copy all fine in the array collections, but if i select column 3, the value is not refresh on that column. If i select after column 4 values are not update for 3 and column 4.

            It is a way to call the itemRenderer value in my copy function so I can update the value?

             

            for(var i:int=2; i < numberOfColumns; i++) {

              myArrayCollection[rowIndex][i] = myArrayCollection(acData[rowIndex][1]);

            // refresh value of renderer

            }

             

            Thanks

            • 3. Re: Datagrid with itemRenderer
              Carol L. Frampton Level 2

              I didn't look at this too closely but I think you need to either using ArrayCollection.setItemAt or, if you continue to do what you are doing, call ArrayCollection,itemUpdated.  If you update item renderers via the back door the data grid doesn't know.  It tries to be very smart about what it updates which is critical for decent performance for large data grids.

               

              Carol

              • 4. Re: Datagrid with itemRenderer
                jfb00 Level 3

                Hi,

                Thanks for you  reply and help.

                I was able to update with itemUpdated but still the same issue.

                myArrayCollection.itemUpdated(myADG.selectedItem);

                Any other idea?

                • 5. Re: Datagrid with itemRenderer
                  Flex harUI Adobe Employee

                  The copy code doesn’t look right.  Why is there a myArrayCollection()

                   

                      myArrayCollection[rowIndex][i] = myArrayCollection(acData[rowIndex][1]);
                  

                   

                  I would think it should look like this:

                   

                     myArrayCollection[rowIndex][i] = myArrayCollection[rowIndex][1];
                  

                   

                  • 6. Re: Datagrid with itemRenderer
                    jfb00 Level 3

                    Sorry, I copy wrong here, it is fine in my code.

                    It looks like it fires first my itemEditEndHandler, any column selected prior to fire my copy button it doesn't get the new values.

                    Maybe i need to do something in my itemEditEndHandler.

                    This is what i have:

                     

                    if (event.reason == AdvancedDataGridEventReason.CANCELLED)

                                    {

                                        // Do not update cell.

                                        return;

                                    }

                                   

                                    event.itemRenderer['realValue'] = event.currentTarget.itemEditorInstance.text;

                     

                    Thanks!

                    • 7. Re: Datagrid with itemRenderer
                      Flex harUI Adobe Employee

                      So what does your code look like, including the call to itemUpdated?

                      • 8. Re: Datagrid with itemRenderer
                        jfb00 Level 3

                        Let me work in a small test case. Can I upload zip file here?

                        When i copy the code, it shows as big table.

                        Thanks

                        • 9. Re: Datagrid with itemRenderer
                          Flex harUI Adobe Employee

                          Yes, you can attach files, although I have to use the web browser to do it.  However, it is better if you cut and paste code.  You should be able to boil it down to 20 lines of code or so.  Then readers can just glance at the code instead of having to download something first.

                          • 10. Re: Datagrid with itemRenderer
                            jfb00 Level 3

                            Here is my code:

                             

                            public function ADG_itemEditEndHandler(event:AdvancedDataGridEvent):void {              
                            
                            
                            // Check the reason for the event.
                            if (event.reason == AdvancedDataGridEventReason.CANCELLED)
                            {
                               // Do not update cell.
                               return;
                            }
                            event.itemRenderer['realValue'] = event.currentTarget.itemEditorInstance.text;
                                            
                            if (isNaN(event.currentTarget.itemEditorInstance.text) || event.currentTarget.itemEditorInstance.text == "" || Number(event.currentTarget.itemEditorInstance.text) < 0 ){
                              event.currentTarget.itemEditorInstance.text = 0;
                              event.itemRenderer['realValue'] = 0;
                              }
                            }
                            
                            protected function disableEditing(event:AdvancedDataGridEvent):void {
                            if (event.columnIndex != 2){ 
                               if (event.rowIndex == 3 || event.rowIndex == 7 || event.rowIndex == 11 || event.rowIndex == 12 || event.rowIndex == 13 || event.rowIndex == 14 || event.rowIndex == 15 || event.rowIndex == 16 || event.rowIndex == 17){
                               event.preventDefault();
                               }
                               }
                            }
                            
                            protected function copy_clickHandler(event:MouseEvent):void
                            {
                              // TODO Auto-generated method stub
                              if (myGrid.selectedItem == null){
                                 Alert.show("Please select a row");
                              }else{
                                 var numberOfColumns:Number = 4;
                                 var first_year:Number = 2009
                                 var rowIndex:Number = myGrid.selectedIndex;
                                 var scrollIndex:Number = myGrid.verticalScrollPosition;
                                    
                                 if (rowIndex == 3 || rowIndex == 11 || rowIndex == 12 || rowIndex == 13 || rowIndex == 14 || rowIndex == 15 || rowIndex == 16 || rowIndex == 17){
                                    Alert.show("You cannot copy this item, it is a calculated row.");
                                 }else{
                                    for(var i:int=1; i < numberOfColumns; i++) {
                                        var colName:String = (first_year + i).toString();
                                        acData[rowIndex][first_year + i] = Number(acData[rowIndex][first_year]);
                                     }
                                    //acData.refresh();
                                   acData.itemUpdated(myGrid.selectedItem);
                                   myGrid.verticalScrollPosition = scrollIndex;
                                   Alert.show("Data Copied.");
                                   }
                                }
                            }
                            
                            
                            • 11. Re: Datagrid with itemRenderer
                              Flex harUI Adobe Employee

                              It looks like you are calling itemUpdated on the selectedItem, but modifying some other row like the top row.

                              • 12. Re: Datagrid with itemRenderer
                                jfb00 Level 3

                                Thanks for you reply and help.

                                I am copying the first year column of the selected row to the rest of the year columns, after that doing the itemUpdated.

                                The user can change the number of the first year of the row selected then move to the second year in the same row and click copy button, here is the problem that it is no copying to the any column selected in the same row.

                                What should I do?

                                • 13. Re: Datagrid with itemRenderer
                                  Flex harUI Adobe Employee

                                  I misread your code earlier.

                                   

                                  I think the issue may be that itemEditEnd is called before the new value is applied to the data object, so you are likely copying old values.  The new value should come from the itemEditorInstance.

                                  • 14. Re: Datagrid with itemRenderer
                                    jfb00 Level 3

                                    How can I do that Alex?

                                    The Copy button have to fire and do the calculations skipping itemEditEnd. Or maybe capture something inside itemEditEnd to do my calculations.

                                    Thanks

                                    • 15. Re: Datagrid with itemRenderer
                                      Flex harUI Adobe Employee

                                      The DataGrid’s itemEditEnd handler takes the column’s editorDataField and pulls that property from itemEditorInstance.  You should do something similar.

                                      • 16. Re: Datagrid with itemRenderer
                                        jfb00 Level 3

                                        Sorry, I don't get it. Can I do the same as the itemEditEnd in my copy button function?

                                        If it is, how can I get the editorDataField there?

                                        • 17. Re: Datagrid with itemRenderer
                                          Flex harUI Adobe Employee

                                          I’m confused.  Is your copy function not being called in itemEditEnd?  If not, why did you show that code?

                                          • 18. Re: Datagrid with itemRenderer
                                            jfb00 Level 3

                                            The copy button is outside of the datagrid, on top of the datagrid. I show this code because there is the problem, when I click the copy button the itemEditEnd fire because the cursor is still in one of the cells.

                                            I have a custom inputText button inside the cells as i mention in my question.

                                            Sorry for the confusion.

                                            Thanks!

                                            • 19. Re: Datagrid with itemRenderer
                                              Flex harUI Adobe Employee

                                              Try using callLater to delay the copy call until after itemEditEnd.  There won’t be an itemEditorInstance by that time, but I don’t think you will need it.

                                              • 20. Re: Datagrid with itemRenderer
                                                jfb00 Level 3

                                                I did the code bellow but not working Alex

                                                Can I include more time to my delay?

                                                 

                                                 

                                                protected function copy_clickHandler(event:MouseEvent):void
                                                {
                                                  // TODO Auto-generated method stub
                                                  if (myGrid.selectedItem == null){
                                                     Alert.show(Please select a row);
                                                  }else{
                                                     var rowIndex:Number = myGrid.selectedIndex;
                                                        
                                                     if (rowIndex == 3 || rowIndex == 11 || rowIndex == 12 || rowIndex == 13 || rowIndex == 14 || rowIndex == 15 || rowIndex == 16 || rowIndex == 17){
                                                        Alert.show(You cannot copy this item, it is a calculated row.);
                                                     }else{
                                                        callLater(copyItems);
                                                     }
                                                  }
                                                }
                                                
                                                private function copyItems():void{
                                                     var numberOfColumns:Number = 4;
                                                     var first_year:Number = 2009
                                                     var rowIndex:Number = myGrid.selectedIndex;
                                                     var scrollIndex:Number = myGrid.verticalScrollPosition;
                                                     for(var i:int=1; i < numberOfColumns; i++) {
                                                            var colName:String = (first_year + i).toString();
                                                            acData[rowIndex][first_year + i] = Number(acData[rowIndex][first_year]);
                                                         }
                                                        //acData.refresh();
                                                       acData.itemUpdated(myGrid.selectedItem);
                                                       myGrid.verticalScrollPosition = scrollIndex;
                                                       Alert.show(Data Copied.);
                                                
                                                
                                                }
                                                
                                                
                                                • 21. Re: Datagrid with itemRenderer
                                                  Flex harUI Adobe Employee

                                                  I would verify that the itemEditEnd runs before copyItems.  You can chain a couple of callLaters together if you have to, but once you get past two there is something else wrong.

                                                  • 22. Re: Datagrid with itemRenderer
                                                    jfb00 Level 3

                                                    It shows the value after i click on my cell that is not showing the copy value. I think the value is not refreshing on my itemrenderer.

                                                    Did you see my code of my item renderer?

                                                    http://forums.adobe.com/message/3798449#3798449

                                                    • 23. Re: Datagrid with itemRenderer
                                                      Flex harUI Adobe Employee

                                                      You should add diagnostics so you can know what got changed when.  It is likely a timing issue.