当前位置: 代码迷 >> Web前端 >> 对于datagrid中删除后,对selectedIndex赋值后的有关问题
  详细解决方案

对于datagrid中删除后,对selectedIndex赋值后的有关问题

热度:84   发布时间:2012-11-10 10:48:50.0
对于datagrid中删除后,对selectedIndex赋值后的问题

对于datagrid中删除后,想要自动选中下一条记录,如果是最后一条,就选中上一条。

这个看似简单的需求,实际操作中却出现了问题。

当选中第一条,删除一条后,selectedIndex的值变成-1,然后对其赋值:

dg.selectedIndex=si;

从debug中看到,这里的si=0,但是dg的selectedIndex却是1,也就是flash内部对datagrid的selectedIndex的时候做了其他一些操作,时间关系也没细看源码,网上找了个方法就解决了这个问题,就是在对数据源remove的之前加入:

dp.disableAutoUpdate();

在remove并且对selectedIndex赋值完了后,再dp.enableAutoUpdate();

这样问题就解决了,但是我用debug看还是没能赋值成功。。

希望有经验的兄弟提醒提醒,在下感激不尽。

下面是解决后的代码:

?

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" minWidth="955" minHeight="600" initialize="init();">
	<mx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			[Bindable]
			private var dp:ArrayCollection=new ArrayCollection();
			private function init():void{
				dp.addItem({id:1,name:"join"});
				dp.addItem({id:2,name:"jason"});
				dp.addItem({id:3,name:"lori"});
				dp.addItem({id:4,name:"tony"});
				dp.addItem({id:5,name:"randy"});
			}
			private function deleteColumn():void{
				var si:int=dg.selectedIndex;
				dp.disableAutoUpdate();
				if(si==dp.length-1){
					dp.removeItemAt(si);
					dg.selectedIndex=si--;
					dg.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
				}else{
					dp.removeItemAt(si);
					dg.selectedIndex=si;
					dg.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
				}
				dp.enableAutoUpdate();
			}
		]]>
	</mx:Script>
	<mx:DataGrid id="dg" dataProvider="{dp}">
		<mx:columns>
			<mx:DataGridColumn dataField="id" headerText="ID" />
			<mx:DataGridColumn dataField="name" headerText="NAME" />
		</mx:columns>
	</mx:DataGrid>
	<mx:Button label="del" click="deleteColumn()"/>
</mx:Application>
  相关解决方案