首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

Flex4之DataGrid增删改同步数据库及页面数据示范总结

2013-11-09 
Flex4之DataGrid增删改同步数据库及页面数据示例总结有关Flex的DataGrid文章的确不少,都是零零碎碎的,目前

Flex4之DataGrid增删改同步数据库及页面数据示例总结

有关Flex的DataGrid文章的确不少,都是零零碎碎的,目前还没有发现有个完整的例子供网友参考,这次我花了两天时间做了下Flex关于DataGrid一个指标数据管理系统,当然设计到的mxml会很多,大都是与DataGrid相关,我就抽取最常用的DataGrid的增删改同步数据库及页面数据来讲解

首先整理下思路,首先无论是删除还是修改,必须得获取当前行所在的记录,那么可以设置个全局变量,当DataGrid的itemClick事件出发时将选中的行赋给全局变量

[Bindable]public var acItemsSelected:Object;;

//事件方法

?protected function myGrid_itemClickHandler(event:ListEvent):void
???{
????acItemsSelected = myGrid.selectedItem;?
???}

这样的话就可以获得了当前选中的对象了

如果删除和修改的话,通常传到后台的肯定含有对象的ID项,那么这个ID是怎么获取的呢,通过acItemsSelected.xxxId能获取,那么这个xxxId必须是在DataGrid中有,设置为不显示就好了,例如我就是这么做的

?<mx:DataGridColumn visible="false" dataField="targetCalId" />

这样的话,要更改的数据等都能通过ID传到后台查询相应对象更改删除了,接下来的事是比较重要的,如果光删除数据库是不行的,页面数据也要保持同步,关于dataGrid也没有好的刷新方法,所以你上网一搜,可能有的人会告诉你对于删除这样做:dataArray.removeItemAt(myGrid.selectedIndex);

增加这样做:dataArray.addItemAt(obj,0);

修改这样做:dataArray.setItemAt(obj,myGrid.selectedIndex);

这里说的dataArray是指的是DataGrid的DataProvider的值集合【当然肯定得声明称全局变量】

这样的写法呢可能你觉得,哎,是对的,其实不然,这并没有真正起到作用,对于FLEX来说缓存是相当大的,不行的话你通过这个修改行记录的属性后,再点这行记录的属性编辑页面发现值根本没改,所以所没有进行二次查询数据库了,利用的是缓存。即便是你调用了初始化查询数据库的那个方法,也是不行的,还是有缓存,最好的做法就是,抛弃上面的三种写法,只需要两步就可以实现刷新,第一初始化DataGrid的那个方法请求必须是URLRequest的,添加一个参数类似于

var u:URLVariables=new URLVariables("temp="+Math.random());

当然不一定非要是temp什么名字都行,然后在返回操作成功提示后调用这个初始化方法,你会发现真的起作用了。

其实建议关于URLRequest传参数的最好带上这个参数,也不费事,可以在很多场合下解决缓存不更新问题

说了这么多其实就是先做好个思想准备,理清思路,下面我就贴上我的一段代码

mxml文件

?

?

?

关于上面这个程序的mainApp就不用管它,它只是主应用程序而已,细心的可以发现这个是个弹出窗口程序

?关于这个程序的三个Servletet处理类【只贴上关键代码】

FindTargetCalServlet

?

UpdateTargetCalServlet

?这里的DataOptUtils是我写的一个数据处理类,判断是否为空什么的

?还有就是TTargetCal

<SPAN style="FONT-SIZE: medium">@Entity@Table(name = "t_target_cal", catalog = "sxtele")public class TTargetCal implements java.io.Serializable {// Fieldsprivate Integer targetCalId;private String targetCal;private String targetTypeName;// Constructors/** default constructor */public TTargetCal() {}/** minimal constructor */public TTargetCal(String targetCal) {this.targetCal = targetCal;}/** full constructor */public TTargetCal(String targetCal, String targetTypeName) {this.targetCal = targetCal;this.targetTypeName = targetTypeName;}// Property accessors@Id@GeneratedValue@Column(name = "Target_cal_id", unique = true, nullable = false)public Integer getTargetCalId() {return this.targetCalId;}public void setTargetCalId(Integer targetCalId) {this.targetCalId = targetCalId;}@Column(name = "Target_cal", nullable = false, length = 50)public String getTargetCal() {return this.targetCal;}public void setTargetCal(String targetCal) {this.targetCal = targetCal;}@Column(name = "Target_type_name", length = 50)public String getTargetTypeName() {return this.targetTypeName;}public void setTargetTypeName(String targetTypeName) {this.targetTypeName = targetTypeName;}}</SPAN>

?

?

热点排行