• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 652
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell┬« is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

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