How to save selection in DataGrid when data changes?

I have a DataGrid and I have set the DataProvider to my data. When my data changes, the DataGrid loses the selected row, and the scrollbar returns to the top. How to save selection and scroll position?

+3
source share
2 answers

If you just want to keep the position:

in any function that changes data, first grab the selected index

var myidx:int = new int(myDG.selectedIndex);

and scroll position

var myVertPos:int = new int(myDG.verticalScrollPosition);

run the code that modifies the data and then follow the above steps back:

myDG.selectedIndex = myidx;
myDG.verticalScrollPosition = myVertPos;

, , , , , DG , . , max, .

+8

. DataGrid String uniqueIdField.

vaule uniqueIdField .

dataProvider, : , . FIXED * , hightlights, ( , , ). *

.

set dataProvider (: Object): void   { var vScroll: int = 0;    // ,    //    if (uniqueIdField.length > 0 && selectedItem!= null)    {     uniqueIdData = this.selectedItem [uniqueIdField];     vScroll = this.verticalScrollPosition;    }

super.dataProvider = ;

        if(uniqueIdField.length > 0 
        && uniqueIdData != null
        && selectedItems.length <= 1)

{
  var currentObj: Object;   var found: Boolean = false;

if(dataProvider is ArrayCollection)
{

 //find object in dataprovider
 for(var i:int=0; i < dataProvider.length; i++)
 {
  currentObj = dataProvider.getItemAt(i);
  if(currentObj[uniqueIdField] == uniqueIdData)
  {
   this.selectedItem = currentObj;
   found = true;
   vScroll = this.selectedIndex;
   break;

  }
 }

 if(!found)
 {
  this.selectedItem = null;
  uniqueIdData = null;
 }


}

//, , null  this.verticalScrollPosition = vScroll;  dispatchEvent ( ListEvent (ListEvent.CHANGE));

}

+2

Source: https://habr.com/ru/post/1740014/


All Articles