7 Replies Latest reply on Aug 9, 2011 8:06 AM by Amy Blankenship

    Referencing a changed Datagrid at run time

    Scott R. Hamilton Level 1

      I have a view with seven DataGrids. The user can mark any displayed row in any of the DataGrids and should be able to delete the row.

       

      Since I don't know in advance which DataGrid will be selected I need a technique to determine which row in which Grid was selected by the user. I have tried using events to determine which DataGrid was selected and the Event class data structure contains the name of the control but I cannot use this as a reference to then manipulate the data provider. It appears to be just a string value containing the name of the control.

       

      Any ideas on how I can determine at run time which row in which DataGrid was selected by the user?

        • 1. Re: Referencing a changed Datagrid at run time
          Amy Blankenship Level 4

          What does your event listener code on the datagrids look like?

          • 2. Re: Referencing a changed Datagrid at run time
            Scott R. Hamilton Level 1

            Hi Amy,

             

            up to now I've been trying to use a drag to a garbage can image for removal of a row. In the dragAndDrop handler code I have:

             

                       sComponentName = String(event.dragInitiator);

             

            which gives me the name of the control as a string but not a reference to the actual control so I can get at the data provider which is what I really need.

             

            I don't want to use this model to manipulate rows in the grid selected because it is limited to just deletes. I want the user to select a row and then choose one of many operations using buttons - Delete, Add After, Add Before, Edit etc.

            • 3. Re: Referencing a changed Datagrid at run time
              Amy Blankenship Level 4

              What I'm looking for is a line that will look something like:

               

              someObject.addEventListener(....)

               

              or

               

              click="someHandler(event)"

               

              (may not be click--in your case it looks like probably not).

               

              I was trying to give you a very precise solution that you could just use without needing to understand why it works or dig through extra information (not to mention saving myself some typing). But without that information, here is what I can tell you:

               

              100% of Event subclasses have 2 properties:

              target

              currentTarget

               

              If you are listening directly to the dataGrid, then you will find a reference to it in currentTarget.  If you are NOT listening directly to dataGrid, target may or may not contain a reference to the dataGrid (it could also contain a reference, for instance, to a label in an itemRenderer).  If you are not listening directly to dataGrid, you will need to loop on the parent of the target until you find a dataGrid or run out of parents.

               

              HTH;

               

              Amy

              1 person found this helpful
              • 4. Re: Referencing a changed Datagrid at run time
                Scott R. Hamilton Level 1

                Hi Amy,

                 

                this is beginning to to get me on the road to a viable solution.

                 

                If I were to add an event listener for each datagrid would it be ok to use the same event listener for each?

                Could I then reference the dataprovider and row through say

                 

                currentTarget.dataProvider or currentTarget.dataProvider.seletectedIndex or currentTarget.dataProvider.getItemAt(currentTarget.dataProvider.seletectedIndex)

                 

                or something along theres lines?

                 

                 

                Probably the best way would be for the user to first indicate what she wants to do by pressing a button, e.g. [Delete] and the after selecting a row in one of the datagrids the eventHandler would delete the marked row.

                 

                Thanks,

                 

                Scott

                • 5. Re: Referencing a changed Datagrid at run time
                  Amy Blankenship Level 4

                  OK, let's back up a bit.  I was in a bit of a rush this morning, and didn't have time to check the reference.

                   

                  If you don't do String(event.dragInitiator) but instead do

                   

                  var dg:DataGrid = event.dragInitiator as DataGrid

                   

                  you should get a reference to the DataGrid that is perfectly valid.

                   

                  next, you can get a reference to the item that was dragged (you shouldn't care about the row per se) by doing this:

                   

                  var ds:DragSource = event.dragSource;

                  var item:YourItemType = ds.dataForFormat('items') as YourType;

                   

                  then, to delete:

                   

                  var lcv:ListCollectionView = dg.dataProvider as ListCollectionView;

                   

                  lcv.removeItemAt(lcv.getItemIndex(item));

                   

                  You can also probably get a reference to the datagrid by using event.currentTarget if your dragInitiator was showing a reference to the datagrid.

                   

                  HTH;

                   

                  Amy

                  1 person found this helpful
                  • 6. Re: Referencing a changed Datagrid at run time
                    Scott R. Hamilton Level 1

                    Thanks Amy,

                     

                    your post is opening a lot of windows...

                     

                    So if I go the eventListener route and wait for the user to push a button (action) and mark a row in dg1 to dg7 then I can get

                    at the dataGrid/dataProvider in the listener, right? Should be very similar to the drag EventHandler in your example.

                     

                    This sound like just what I need.

                     

                     

                     

                    Scott

                    • 7. Re: Referencing a changed Datagrid at run time
                      Amy Blankenship Level 4

                      The button's event listener is not likely to have a reference to the Datagrid in it, so you will probably need to have logic in the listener for each button that identifies the correct DG.

                       

                      HTH;

                       

                      Amy