2 Replies Latest reply on Feb 1, 2011 10:07 AM by Flex harUI

    Using a custom itemrenderer in datagrid to update value in the same row but different column/cell

    tuktuksanook

      Here's what I have so far.  I have one datagrid (dg1) with enable drag and another datagrid (dg2) with dropenabled.  Column3 (col3) of dg2 also has a custom intemrenderer that's just a hslider.

       

      When an item from dg1 is dropped on dg2, a custom popup appears that asks you to use the slider in the popup to set a stress level.  Click ok and dg2 is populated with dg1's item as well as the value you selected from the popup window.  I was also setting a sliderTemp variable that was bound to the itemrender slider to set it but that's obviously causing issues as well where all the itemrenderer sliders will change to the latest value and I don't want that.

       

      What is needed from this setup is when you click ok from the popup window, the value you choose from the slider goes into dg2 (that's working) AND the intemrenderer slider needs to be set to that value as well.  Then, if you used the intemrenderer slider you can change the numeric value in the adjacent column (col2).   I just dont know how to hook up the itemrenderer slider to correspond with that numeric value (thatds be in col2 on that row);

       

       
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600" 
                          xmlns:viewStackEffects="org.efflex.mx.viewStackEffects.*" backgroundColor="#FFFFFF" creationComplete="init(event)" 
                          xmlns:components="components.*" xmlns:local="*">
           <mx:Script>
                <![CDATA[
                     import mx.binding.utils.ChangeWatcher;
                     import mx.collections.ArrayCollection;
                     import mx.controls.Alert;
                     import mx.controls.TextInput;
                     import mx.core.DragSource;
                     import mx.core.IUIComponent;
                     import mx.events.CloseEvent;
                     import mx.events.DataGridEvent;
                     import mx.events.DragEvent;
                     import mx.events.FlexEvent;
                     import mx.events.ListEvent;
                     import mx.events.SliderEvent;
                     import mx.events.SliderEventClickTarget;
                     import mx.managers.DragManager;
                     import mx.managers.PopUpManager;
                     import mx.utils.ObjectUtil;
                
                     
                     [Bindable]private var myDP1:ArrayCollection;
                     [Bindable]private var myDP2:ArrayCollection;
                     
                     [Bindable]public var temp:String;
                     [Bindable]public var slideTemp:Number;
                     
                     private var win:Dialog;     
                     
                          
                     protected function init(event:FlexEvent):void{
                          myDP1 = new ArrayCollection([{col1:'Separation from friends and family due to deployment'},{col1:'Combat'},{col1:'Divorce'},{col1:'Marriage'},
                               {col1:'Loss of job'},{col1:'Death of a comrade'},{col1:'Retirement'},{col1:'Pregnancey'},
                               {col1:'Becoming a parent'},{col1:'Injury from an attack'},{col1:'Death of a loved one'},{col1:'Marital separation'},
                               {col1:'Unwanted sexual experience'},{col1:'Other personal injury or illness'}])
                               
                          myDP2 = new ArrayCollection()
                     }     
      
                     protected function button1_clickHandler(event:MouseEvent):void
                     {     
                          event.preventDefault();
                          if(txt.text != "")
                          {
                               Alert.yesLabel = "ok";                    
                               Alert.show("", "Enter Stress Level", 3, this,txtClickHandler);
                                                        
                          }
                     }
                     private function image_dragEnter(evt:DragEvent):void {
                          var obj:IUIComponent = IUIComponent(evt.currentTarget);
                          DragManager.acceptDragDrop(obj);
                     }
                     
                     private function image_dragDrop(evt:DragEvent):void {
                          var item:Object = dg2.selectedItem;                    
                          var idx:int = myDP2.getItemIndex(item);
                          myDP2.removeItemAt(idx);
                     }
      
      
                     protected function dg1_changeHandler(event:ListEvent):void
                     {
                          temp=event.itemRenderer.data.col1;     
                          
                     }
      
      
                     protected function dg2_dragDropHandler(event:DragEvent):void
                     {
                          event.preventDefault();                         
                          dg2.hideDropFeedback(event as DragEvent)
                          var win:Dialog = PopUpManager.createPopUp(this, Dialog, true) as Dialog;
                          win.btn.addEventListener(MouseEvent.CLICK, addIt);
                          PopUpManager.centerPopUp(win);                              
                          win.mySlide.addEventListener(Event.CHANGE, slideIt);
                          
                     }
                     
                     private function txtClickHandler(event:CloseEvent):void {
                          trace("alert");
                          if (event.detail==Alert.YES){
                               myDP2.addItem({label:temp});
                          }
                     }
                     private function addIt(event:MouseEvent):void{                    
                          myDP2.addItem({col1:temp, col2:slideTemp})
                          
                          
                     }
                     private function slideIt(event:SliderEvent):void{                    
                          slideTemp = event.target.value;               
                     }
                ]]>
           </mx:Script>
           
                     <mx:Panel x="10" y="10" width="906" height="481" layout="absolute">
                          <mx:Image x="812" y="367" source="assets/woofie.png" width="64" height="64" dragDrop="image_dragDrop(event);" dragEnter="image_dragEnter(event);"/>
                          <mx:DataGrid x="14" y="81" width="307" height="251" dragEnabled="true" id="dg1" dataProvider="{myDP1}" wordWrap="true" variableRowHeight="true" change="dg1_changeHandler(event)">
                               <mx:columns>
                                    <mx:DataGridColumn headerText="Examples of Life Events" dataField="col1"/>
                               </mx:columns>
                          </mx:DataGrid>
                          
                          <mx:DataGrid x="329" y="81" height="351" width="475" dragEnabled="true" dropEnabled="true" id="dg2" 
                                          wordWrap="true" variableRowHeight="true" dataProvider="{myDP2}" editable="true" 
                                          dragDrop="dg2_dragDropHandler(event)"  rowHeight="50" verticalGridLines="false" horizontalGridLines="true" >
                               <mx:columns>
                                    <mx:DataGridColumn headerText="Stressor" dataField="col1" width="300" wordWrap="true" editable="false">
                                         
                                    </mx:DataGridColumn>
                                    <mx:DataGridColumn headerText="Stress Level" dataField="col2" width="82" editable="false"/>
                                    <mx:DataGridColumn headerText="Indicator" dataField="col3" width="175" paddingLeft="0" paddingRight="0" wordWrap="true" editable="false">
                                         <mx:itemRenderer>
                                              <mx:Component>
                                                   <components:Compslide/>
                                              </mx:Component>
                                         </mx:itemRenderer>
                                    </mx:DataGridColumn>
                               </mx:columns>
                          </mx:DataGrid>                    
                          <mx:Text x="14" y="10" text="The first category of underlying stressors is called Life Events. The list includes both positive and negative changes that individuals experience. Both can be stressful. For example, becoming a parent is usually viewed as a positive thing, but it also involves many new responsibilities that can cause stress. " width="581" height="73" fontSize="12"/>
                          <mx:TextInput x="10" y="380" width="311" id="txt"/>
                          <mx:Text x="10" y="335" text="Add events to your list that are not represented in the example list.  Type and click &quot;Add to List&quot;&#xa;" width="311" height="51" fontSize="12"/>
                          <mx:Button x="234" y="410" label="Add to List" click="button1_clickHandler(event)"/>
                     </mx:Panel>     
      </mx:Application>