Friday, June 14, 2013

Spark Datagrid - Controlling the Sorting Arrows

I recently came across a bug where I need to control the sorting arrow on a spark datagrid based on data that is already being sorted by the server.  In my case, I actually have 2 grids put together to look like one to act as a spreadsheet but the results will be the same if you didn't.  Everything I have found online to to programmatic sorting tells me to just call 

  myGrid.sortByColumns(columnIndices:Vector.<int>, isInteractive:Boolean=false);

This works great to apply the sorting array but what if you don't want a sorting arrow anymore?  I am recreating the dataProvider and my dataProvider don't have anything like a sort or anything.  You can't just try to fake out the columnIndices because you have to provide a valid one or it just returns and does  nothing.  

So here's what I found:

GridColumnHeaderGroup has a property called visibleSortIndicatorIndices that is used to control the arrows.  You can still use the sortByColumns to have it show your arrows but if you are doing your sorting on the backend, you can just use myGrid.columnHeaderGroup.visibleSortIndicatorIndices = Vector.<int> of indicies you what the arrow to show up in.  If you want to remove them just pass in a new Vector.<int>(); Remember you are still responsible for setting the arrow direction in the column using the col.descending flag, i'm not showing that side of the equation, just how to show certain arrows and remove them

private function hideSortableColumnArrow(columnIndices:Vector.):void
   var blankColumnIndices:Vector. = new Vector.();

   if(myGrid && myGrid.columnHeaderGroup)
      myGrid.columnHeaderGroup.visibleSortIndicatorIndices = blankColumnIndices;