flex AdvancedDataGrid树级结构方向移动


/*******************************************************************************************


* 控制按钮是否有效


* *****************************************************************************************/


public function changeItemButtonEnable(itemId:String,array:ArrayCollection):void{


for(var i:int= 0;i
{


if(itemId == array[i].id){


var epsVoSource:ArrayCollection = ((this.epsModule.dgEpsItems.dataProvider as HierarchicalCollectionView).source as HierarchicalData).source as ArrayCollection;


if(i-1 == -1){


this.epsModule.btnMoveUp.enabled = false;


this.epsModule.btnMoveRight.enabled = false;


parentSecondEpsVo = getParentItem(array[i].parentId,epsVoSource);


if(parentSecondEpsVo != null){


this.epsModule.btnMoveLeft.enabled = true;


}else{


this.epsModule.btnMoveLeft.enabled = false;


}


}else{


parentSecondEpsVo = getParentItem(array[i].parentId,epsVoSource);


if(parentSecondEpsVo != null){


this.epsModule.btnMoveLeft.enabled = true;


}else{


this.epsModule.btnMoveLeft.enabled = false;


}


this.epsModule.btnMoveUp.enabled = true;


this.epsModule.btnMoveRight.enabled = true;


}





if(i+1 == array.length){


this.epsModule.btnMoveDown.enabled = false;





if(i-1 == -1){


this.epsModule.btnMoveRight.enabled = false;


}else{


this.epsModule.btnMoveRight.enabled = true;


}


}else{


this.epsModule.btnMoveDown.enabled = true;


}


}else if(array[i].children != null && array[i].children.length!=0){


changeItemButtonEnable(itemId,array[i].children);


}


}


}





/************************************************************************************************************/


/**条目的上移动**/


public function upItem(itemId:String,array:ArrayCollection):void{


var selectedEpsVo:EpsVo = this.epsModule.dgEpsItems.selectedItem as EpsVo;


var epsVoTemp:EpsVo;


var epsVo:EpsVo;


for(var i:int = 0;i
if(itemId == array[i].id){


var index:int = 0;


for(var k:int = 0;k
{


if(itemId == array[k].id && k >0)


{


epsVoTemp = array[k] as EpsVo;


array[k] = array[k-1];


array[k-1] = epsVoTemp;


index++;


}


}


if(index == 0)


{


trace("找父Id");


var epsVoSource:ArrayCollection = ((this.epsModule.dgEpsItems.dataProvider as HierarchicalCollectionView).source as HierarchicalData).source as ArrayCollection;


parentSecondEpsVo = getParentItem(selectedEpsVo.parentId,epsVoSource);


if(parentSecondEpsVo != null)


{


if(parentSecondEpsVo.children != null && parentSecondEpsVo.children.length != 0)


{


parentSecondEpsVo.children.removeItemAt(0);


}


}else


{


Alert.show("你不能将此项目移至最高级别,请重新操作!","操作提示");


break;


}


parentFirstEpsVo = getParentItem(parentSecondEpsVo.parentId,epsVoSource);


if(parentFirstEpsVo != null)


{


if(parentFirstEpsVo.children != null && parentFirstEpsVo.children.length != 0)


{


selectedEpsVo.parentId = parentFirstEpsVo.id;


parentFirstEpsVo.children.addItemAt(selectedEpsVo,parentFirstEpsVo.children.length);


}


}


else


{


for(var j:int = 0;j
if(epsVoSource[i].id == parentSecondEpsVo.id)


{


selectedEpsVo.parentId = epsVoSource[j].parentId;


epsVoSource.addItemAt(selectedEpsVo,j+1);


break;


}


}





}


}


}else if(array[i].children != null && array[i].children.length!=0){


upItem(itemId,array[i].children);


}


}


}


/**条目的上移动**/


/**条目的下移动**/


public function downItem(itemId:String,array:ArrayCollection):void{


var selectedEpsVo:EpsVo = this.epsModule.dgEpsItems.selectedItem as EpsVo;


var epsVoTemp:EpsVo;


var epsVo:EpsVo;


for(var i:int = 0;i
if(itemId == array[i].id){


var index:int = 0;


for(var k:int = 0;k
{


if(itemId == array[k].id && k
{


epsVoTemp = array[k+1] as EpsVo;


array[k+1] = array[k];


array[k] = epsVoTemp;


index++;


break;


}


}


if(index != 0)


{


break;


}else


{


Alert.show("已经是最底层,不能再往下了!","操作提示");


}


}else if(array[i].children != null && array[i].children.length!=0){


downItem(itemId,array[i].children);


}


}


}


/**条目的下移动**/


/**条目的左移动**/


private var parentSecondEpsVo:EpsVo;


private var parentFirstEpsVo:EpsVo;


public function leftItem(itemId:String,array:ArrayCollection,index:int):void


{


var indexCount:int = 0;


var selectedEpsVo:EpsVo = this.epsModule.dgEpsItems.selectedItem as EpsVo;


for(var i:int =0; i
{


if(itemId == array[i].id && parentFirstEpsVo == null)


{


var epsVoSource:ArrayCollection = ((this.epsModule.dgEpsItems.dataProvider as HierarchicalCollectionView).source as HierarchicalData).source as ArrayCollection;


if(index == epsVoSource.length)


{


parentSecondEpsVo = getParentItem(array[i].parentId,epsVoSource);


if(parentSecondEpsVo != null){


parentSecondEpsVo.children.removeItemAt(i);


}else


{


Alert.show("你不能将此项目移至最高级别,请重新操作!","操作提示");


break;


}


parentFirstEpsVo = getParentItem(parentSecondEpsVo.parentId,epsVoSource);


if(parentFirstEpsVo != null)


{


for(var k:int = 0;k
{


if(parentFirstEpsVo.children[k].id == parentSecondEpsVo.id)


{


selectedEpsVo.parentId = parentFirstEpsVo.id;


parentFirstEpsVo.children.addItemAt(selectedEpsVo,k+1);


break;


}


}


break;


}


else


{


for(var j:int = 0;j
{


if(epsVoSource[j].id == parentSecondEpsVo.id)


{


selectedEpsVo.parentId = epsVoSource[j].parentId;


epsVoSource.addItemAt(selectedEpsVo,j+1);


break;


}


}


break;


}


}


}else if(array[i].children != null && array[i].children.length!=0){


leftItem(itemId,array[i].children,index);


}


}


}





/**条目的左移动**/


/**条目的右移动**/


public function rightItem(itemId:String,array:ArrayCollection):void{


var selectedEpsVo:EpsVo = this.epsModule.dgEpsItems.selectedItem as EpsVo;


for(var i:int = 0;i
if(itemId == array[i].id)


{


if(i-1 != -1)


{


var epsVo:EpsVo = array[i-1] as EpsVo;


array.removeItemAt(i);


selectedEpsVo.parentId = array[i-1].id;


epsVo.children.addItemAt(selectedEpsVo,epsVo.children.length);


break;


}else


{


Alert.show(",请重新操作!","操作提示");


break;


}


}else if(array[i].children != null && array[i].children.length!=0){


rightItem(itemId,array[i].children);


}


}


}


/**条目的右移动**/


public function getParentItem(parentId:String,array:ArrayCollection):EpsVo


{


var parentEpsVo:EpsVo;


for(var i:int = 0;i
if(parentId == array[i].id){


parentEpsVo = array[i] as EpsVo;



trace(parentEpsVo.id+"====父ID"+parentId+"====="+array[i].id);


return parentEpsVo;


}else if(array[i].children != null && array[i].children.length!=0){


if(parentEpsVo == null)


{


parentEpsVo = getParentItem(parentId,array[i].children);


}


}


}


return parentEpsVo;


}


/********************有待优化,如有朋友了解给点建议******/

/*************优化过的********************************/
/**测试用排序算法**/
private function sortDataSource(parentId:String, array:ArrayCollection, node:ProjectCodeVO, index:int):void {
for(var i:int=0; i<array.length; i++) {
var code:ProjectCodeVO = array[i] as ProjectCodeVO;
if(code.id == this.moveTargetVO.id) this.moveIndex = ProjectCodeModuleAS.getIndex();
if(code.parentId+"" == parentId+"") {
ProjectCodeModuleAS.addIndex();
//code.sequence = TaskCodeModuleAS.getIndex();
sortDataSource(code.id, array[i].children as ArrayCollection, code, ProjectCodeModuleAS.getIndex());
}
}
}
/**静态方法,计算顺序用**/
public static function addIndex():void {
index++;
}
/**静态方法,计算顺序用**/
public static function getIndex():int {
return index;
}
/**静态方法,设置顺序初始化值**/
public static function setIndex(i:int):void {
index = i;
}

/**向下移动节点**/
public function moveDown(event:MouseEvent):void {
//首先递归找到要交换位置的目标及其兄弟元素
var proCodeVo:ProjectCodeVO = this.currentWin.dgProCode.selectedItem as ProjectCodeVO;
var proCodes:ArrayCollection=((this.currentWin.dgProCode.dataProvider as HierarchicalCollectionView).source as HierarchicalData).source as ArrayCollection;
swapProjectCodeNode(proCodeVo, proCodes, this.projectCodeVoDataSource, "DOWN");

}
/**向上移动节点**/
public function moveUp(event:MouseEvent):void {
//首先递归找到要交换位置的目标及其兄弟元素
var proCodeVo:ProjectCodeVO = this.currentWin.dgProCode.selectedItem as ProjectCodeVO;
var proCodes:ArrayCollection=((this.currentWin.dgProCode.dataProvider as HierarchicalCollectionView).source as HierarchicalData).source as ArrayCollection;
swapProjectCodeNode(proCodeVo, proCodes, this.projectCodeVoDataSource, "UP");

}

/**向左移动节点**/
public function moveLeft(event:MouseEvent):void {
//首先递归找到要交换位置的目标及其兄弟元素
var proCodeVo:ProjectCodeVO = this.currentWin.dgProCode.selectedItem as ProjectCodeVO;
var proCodes:ArrayCollection=((this.currentWin.dgProCode.dataProvider as HierarchicalCollectionView).source as HierarchicalData).source as ArrayCollection;
swapProjectCodeNode(proCodeVo, proCodes, this.projectCodeVoDataSource, "LEFT");

}

/**向右移动节点**/
public function moveRight(event:MouseEvent):void {
//首先递归找到要交换位置的目标及其兄弟元素
var proCodeVo:ProjectCodeVO = this.currentWin.dgProCode.selectedItem as ProjectCodeVO;
var proCodes:ArrayCollection=((this.currentWin.dgProCode.dataProvider as HierarchicalCollectionView).source as HierarchicalData).source as ArrayCollection;
swapProjectCodeNode(proCodeVo, proCodes, this.projectCodeVoDataSource, "RIGHT");

}

/**递归查找到需要交换位置的元素的兄弟元素,并调换其位置**/
private function swapProjectCodeNode(targetVO:ProjectCodeVO, datas:ArrayCollection, parentVO:ProjectCodeVO, swapType:String):void {
try{
this.moveTargetVO = targetVO;
for(var i:int = 0; i<datas.length; i++) {
//Alert.show(targetVO.parentId);
//非根节点的情形
if(datas[i].id == targetVO.parentId) {
for(var j:int=0; j<datas[i].children.length; j++) {
if(datas[i].children[j].id == targetVO.id) {

if(swapType == "DOWN")
datas[i].children.addItemAt(targetVO, j+2);
else if(swapType == "UP")
datas[i].children.addItemAt(targetVO, j-1);
else if(swapType == "LEFT") {
//Alert.show("向左边移动!"+targetVO.parentId+"\n"+parentVO.parentId);
targetVO.parentId = parentVO.id;
parentVO.children.addItemAt(targetVO,i+1);
}
else if(swapType == "RIGHT") {
//Alert.show("向右移动!");
targetVO.parentId = datas[i].children[j-1].id;
datas[i].children[j-1].children.addItemAt(targetVO,datas[i].children[j-1].children.length);
}

this.currentWin.dgProCode.expandChildrenOf(targetVO, false);
this.currentWin.dgProCode.expandChildrenOf(targetVO, true);
this.currentWin.dgProCode.invalidateList();
this.currentWin.dgProCode.validateNow();

if(swapType == "DOWN")
datas[i].children.removeItemAt(j);
else if(swapType == "UP")
datas[i].children.removeItemAt(j+1);
else if(swapType == "LEFT")
datas[i].children.removeItemAt(j);
else if(swapType == "RIGHT")
datas[i].children.removeItemAt(j);

this.currentWin.dgProCode.expandChildrenOf(targetVO, false);
this.currentWin.dgProCode.expandChildrenOf(targetVO, true);
this.currentWin.dgProCode.invalidateList();
this.currentWin.dgProCode.validateNow();

var array:ArrayCollection=((this.currentWin.dgProCode.dataProvider as HierarchicalCollectionView).source as HierarchicalData).source as ArrayCollection;
ProjectCodeModuleAS.setIndex(0);
var root:ProjectCodeVO = new ProjectCodeVO();
sortDataSource(null, array, root,ProjectCodeModuleAS.getIndex());

CairngormEventDispatcher.getInstance().dispatchEvent(new ProjectCodeEvent(ProjectCodeEvent.EVENT_UPDATE_PROJECTCODEDEFINPARENT,this.projectCodeVoDataSource,null,currentWin as ProjectCodeModuleInterface));
break;
}
}
}else if(targetVO.parentId+"" == "null") {//根节点的情形
if(datas[i].id == targetVO.id) {

if(swapType == "DOWN")
this.projectCodeVoDataSource.children.addItemAt(targetVO, i+2);
else if(swapType == "UP")
this.projectCodeVoDataSource.children.addItemAt(targetVO, i-1);
else if(swapType == "RIGHT") {
targetVO.parentId = this.projectCodeVoDataSource.children[i-1].id;
this.projectCodeVoDataSource.children[i-1].children.addItemAt(targetVO, this.projectCodeVoDataSource.children[i-1].children.length);
}

this.currentWin.dgProCode.expandChildrenOf(targetVO, false);
this.currentWin.dgProCode.expandChildrenOf(targetVO, true);
this.currentWin.dgProCode.invalidateList();
this.currentWin.dgProCode.validateNow();

if(swapType == "DOWN")
this.projectCodeVoDataSource.children.removeItemAt(i);
else if(swapType == "UP")
this.projectCodeVoDataSource.children.removeItemAt(i+1);
else if(swapType == "RIGHT") {
this.projectCodeVoDataSource.children.removeItemAt(i);
}

this.currentWin.dgProCode.expandChildrenOf(targetVO, false);
this.currentWin.dgProCode.expandChildrenOf(targetVO, true);
this.currentWin.dgProCode.invalidateList();
this.currentWin.dgProCode.validateNow();

var array:ArrayCollection=((this.currentWin.dgProCode.dataProvider as HierarchicalCollectionView).source as HierarchicalData).source as ArrayCollection;
ProjectCodeModuleAS.setIndex(0);
var root:ProjectCodeVO = new ProjectCodeVO();
sortDataSource(null, array, root,ProjectCodeModuleAS.getIndex());

CairngormEventDispatcher.getInstance().dispatchEvent(new ProjectCodeEvent(ProjectCodeEvent.EVENT_UPDATE_PROJECTCODEDEFINPARENT,this.projectCodeVoDataSource,null,currentWin as ProjectCodeModuleInterface));
break;
}
}else {
swapProjectCodeNode(targetVO, datas[i].children, datas[i], swapType);
}
}
//this.taskCodeModule.dgTaskCode.selectedItem = targetVO;
}catch(error:Error) {

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值