• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 649
  • Last Modified:

AdvancedDatagrid ColumnMove Question

I have an advanceddatagrid and trying to prevent the shift of FIRST column but it's not working.
Could any of you help? Code attached.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
      <![CDATA[
      
       import mx.collections.ArrayCollection;
       import flash.net.FileReference;

       import mx.controls.Alert;
    import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
             
            import mx.events.IndexChangedEvent;

             
            [Bindable]
private var dp:ArrayCollection=new ArrayCollection([

{A:'G',B:'Mamta',C:'Song 4',D: 100},
{A:'H',B:'Mamta',C:'Song 4',D: 100},
{A:'I',B:'Mamta',C:'Song 4',D: 100},
{A:'J',B:'Mamta',C:'Song 4',D: 100},
{A:'K',B:'Mamta',C:'Song 4',D: 100},
{A:'D',B:'Mamta',C:'Song 4',D: 100},
{A:'D',B:'Mamta',C:'Song 4',D: 100},
{A:'D',B:'Mamta',C:'Song 4',D: 100},
{A:'D',B:'Mamta',C:'Song 4',D: 100},
{A:'D',B:'Mamta',C:'Song 4',D: 100},
{A:'D',B:'Mamta',C:'Song 4',D: 100},
{A:'D',B:'Mamta',C:'Song 4',D: 100},
{A:'D',B:'Mamta',C:'Song 4',D: 100},
{A:'D',B:'Mamta',C:'Song 4',D: 100},
{A:'D',B:'Mamta',C:'Song 4',D: 100},
{A:'D',B:'Mamta',C:'Song 4',D: 100},
{A:'D',B:'Mamta',C:'Song 4',D: 100},
{A:'D',B:'Mamta',C:'Song 4',D: 100},
{A:'D',B:'Mamta',C:'Song 4',D: 100},
{A:'D',B:'Mamta',C:'Song 4',D: 100},
{A:'D',B:'Mamta',C:'Song 4',D: 100}
]);
            
      
    private function columnMoveHandler(event:IndexChangedEvent):void{
          var array:Array = dg.columns;
        var oldIndex:uint = event.oldIndex;
        var newIndex:uint = event.newIndex;
        var col:AdvancedDataGridColumn;
        col = array[0];
       
        //event.preventDefault();
        if (//oldIndex !=newIndex ||
        col.dataField !="A"){
       
        //event.stopImmediatePropagation();
        //event.preventDefault();
        //event.cancelable;
        dg.columns = array;
        dg.validateNow();
        }
       
      }


      ]]>
</mx:Script>

<mx:VBox width="100%" height="50%">
      

<mx:AdvancedDataGrid  dataProvider="{dp}" id="dg" headerShift="columnMoveHandler(event)">
<mx:columns>
      
      <mx:AdvancedDataGridColumn headerText="A"  dataField="A"/>
      
      <mx:AdvancedDataGridColumn headerText="B" dataField="B"/>
      <mx:AdvancedDataGridColumn headerText="C" dataField="C"/>
      <mx:AdvancedDataGridColumn headerText="D" dataField="D"/>
</mx:columns>
</mx:AdvancedDataGrid>
</mx:VBox>
</mx:Application>
0
VKN9876
Asked:
VKN9876
1 Solution
 
Fuzzy_Logic_Commented:
This is a bit of a hack but it works.

Hope you find it useful.

Regards FL.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
      <![CDATA[
      
        import mx.collections.ArrayCollection;
        
        import flash.net.FileReference;
 
        import mx.controls.Alert;
        import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
        import mx.controls.advancedDataGridClasses.AdvancedDataGridHeaderRenderer;
           
        import mx.events.IndexChangedEvent;
               
             
        [Bindable]
        private var dp:ArrayCollection=new ArrayCollection([
 
                {A:'G',B:'Mamta',C:'Song 4',D: 100},
                {A:'H',B:'Mamta',C:'Song 4',D: 100},
                {A:'I',B:'Mamta',C:'Song 4',D: 100},
                {A:'J',B:'Mamta',C:'Song 4',D: 100},
                {A:'K',B:'Mamta',C:'Song 4',D: 100},
                {A:'D',B:'Mamta',C:'Song 4',D: 100},
                {A:'D',B:'Mamta',C:'Song 4',D: 100},
                {A:'D',B:'Mamta',C:'Song 4',D: 100},
                {A:'D',B:'Mamta',C:'Song 4',D: 100},
                {A:'D',B:'Mamta',C:'Song 4',D: 100},
                {A:'D',B:'Mamta',C:'Song 4',D: 100},
                {A:'D',B:'Mamta',C:'Song 4',D: 100},
                {A:'D',B:'Mamta',C:'Song 4',D: 100},
                {A:'D',B:'Mamta',C:'Song 4',D: 100},
                {A:'D',B:'Mamta',C:'Song 4',D: 100},
                {A:'D',B:'Mamta',C:'Song 4',D: 100},
                {A:'D',B:'Mamta',C:'Song 4',D: 100},
                {A:'D',B:'Mamta',C:'Song 4',D: 100},
                {A:'D',B:'Mamta',C:'Song 4',D: 100},
                {A:'D',B:'Mamta',C:'Song 4',D: 100},
                {A:'D',B:'Mamta',C:'Song 4',D: 100}
                ]);
            
      
    private function setDraggableColumns(e:MouseEvent):void //this will disable dragging of columns specified in the if statement
    {
        try
        {
            var head:AdvancedDataGridHeaderRenderer = e.target.parent;
            
            var col:AdvancedDataGridColumn
                        
            const FROZEN_COLUMN:int = 0;            
                        
                if (head == dg.headerItems[0][FROZEN_COLUMN])
                {
                    dg.draggableColumns = false;
                }
         
                else
                {
                    dg.draggableColumns = true;
                }
            
        }
        catch(te:TypeError)
        {
            // do nothing, mouse is not over an AdvancedDataGridHeaderRenderer
        }
        
    }
    
    // if another column is shifted to the left we can catch this and stop column A from moving
    private function shiftColumns(e:IndexChangedEvent):void
    {
        
        if (e.newIndex == 0) // catch column if it is shifted to the left of the first column
        {
           if (dg.groupedColumns == null)
           {   
               //*********manipulate the array of columns***********//
               
               // store columns for later
           var firstCol:AdvancedDataGridColumn = dg.columns[0];
           var secondCol:AdvancedDataGridColumn = dg.columns[1];
               // set up new array to manipulate
           var newColArray:Array = dg.columns;
               // remove first 2 columns
           newColArray.shift();
           newColArray.shift();
               // insert 2 columns at the beginning of the array in reverse order
           newColArray.unshift(firstCol);
           newColArray.unshift(secondCol);
               // reassign datagrid column array
           dg.columns = newColArray;
           }
           else
           {
              
            // code doesn't work with grouped columns
            
            }
        
         }
    }
 
      ]]>
</mx:Script>
 
<mx:VBox width="100%" height="50%">
      
 
<mx:AdvancedDataGrid  dataProvider="{dp}" id="dg" mouseOver="setDraggableColumns(event)" headerShift="shiftColumns(event)">
 
<mx:columns>
      <mx:AdvancedDataGridColumn headerText="A"  dataField="A"/>
      <mx:AdvancedDataGridColumn headerText="B" dataField="B"/>
      <mx:AdvancedDataGridColumn headerText="C" dataField="C"/>
      <mx:AdvancedDataGridColumn headerText="D" dataField="D"/>
</mx:columns>
 
</mx:AdvancedDataGrid>
</mx:VBox>
</mx:Application>

Open in new window

0
 
VKN9876Author Commented:
You are the BEST. Thanks a lot.
0

Featured Post

Get Certified for a Job in Cybersecurity

Want an exciting career in an emerging field? Earn your MS in Cybersecurity and get certified in ethical hacking or computer forensic investigation. WGU’s MSCSIA degree program was designed to meet the most recent U.S. Department of Homeland Security (DHS) and NSA guidelines.  

Tackle projects and never again get stuck behind a technical roadblock.
Join Now