效果展示:
1.增加节点
2.删除节点
3.修改节点
技术分析:
利用DWR可以在客户端利用JavaScript直接调用服务端的Java方法并返回值给JavaScript就好像直接本地客户端调用一样DWR(根据Java类来动态生成JavaScrip代码).
树是一个我们日常用的组件,EXT给我们提供了一个非常好用的树控件,对于传统HTML页面完全靠手编写代码是非常困难的,因为要编写许多JS代码,还要实现AJAX功能,使用EXT编写树我们的工作将简单方便许多.
Ext.ux.DWRTreeLoader 是对树加载器 Ext.tree.TreeLoader 的扩展,实现了通过DWR直接调用后台JAVA类方法,将树结点返回到前台,除此之外,还支持对树进行查找时,将查找参数直接传给后台JAVA类方法.
具体JS代码实现:
- //全局路径
- varbasePath="http://localhost:8080/exttree";
- if(typeof(glbRootPath)!="undefined"){
- basePath=glbRootPath;
- }
- //扩展窗体
- FormEditWin=function(){
- varcurFormWin;
- return{
- width:600,
- height:400,
- showAddDirWin:function(parentNode){
- //显示添加子目录窗口
- varnumber=parentNode.indexOf(parentNode.lastChild)+1;
- vareditpage=basePath
- +"/navigateedit?parentId="
- +parentNode.id+"&leaf=0&number="+number;
- varwindow=this.createWin("windirnew","新建目录节点",editpage,function(){
- parentNode.reload();
- });
- window.show();
- },108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> showAddLeafWin:function(parentNode){
- //显示添加子叶子节点窗口
- varnumber=parentNode.indexOf(parentNode.lastChild)+1;
- vareditpage=basePath
- +"/navigateedit?parentId="
- +parentNode.id+"&leaf=1&number="+number;
- this.createWin("winleafnew","新建叶子节点",248); line-height:18px"> parentNode.reload();
- });
- window.show();
- },248); line-height:18px"> showEditDirWin:function(node){
- //显示目录编辑窗口
- +"/navigateedit?id="+node.id;
- this.createWin("win"+node.id,node.text,153); background-color:inherit; font-weight:bold">varnodeparent=node.parentNode;
- vartree=node.getOwnerTree();
- nodeparent.on("expand",153); background-color:inherit; font-weight:bold">function(pnode){
- tree.getNodeById(node.id).select();
- this,{
- single:true
- node.parentNode.reload();
- showEditLeafWin://显示叶子节点编辑窗口
- createWin:function(winId,winTitle,iframePage,closeFun){
- //供各类型窗口创建时调用
- varwin=Ext.getCmp(winId);
- if(!win){
- win=newExt.Window({
- id:winId,248); line-height:18px"> title:"菜单编辑窗口-"+winTitle,
- width:this.width,
- height:this.height,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> maximizable:true,248); line-height:18px"> modal: html:"<iframewidth='100%'height='100%'frameborder='0'src='"
- +iframePage+"'></iframe>"
- this.reloadNavNode=closeFun;
- curFormWin=win;
- returnwin;
- reloadNavNode: close:if(curFormWin){
- curFormWin.close();
- }
- }();
- //导航树
- NavTree=varnav;
- varnavEditor;
- varleafMenu;
- vardirMenu;
- varloader;
- varroot;
- varremoveFlag=false;
- vartitleChangeFlag=false;
- varnodeSelected;
- varmgr;
- init:if(!mgr){
- Ext.Msg.alert("警告提示","请先通过NavTree.setMgr()设置mgr");
- return;
- if(!loader){
- loader=newExt.tree.TreeLoader({
- url:basePath+'/navigatejson'
- loader.on('beforeload',153); background-color:inherit; font-weight:bold">function(treeloader,node){
- treeloader.baseParams={
- id:node.id,248); line-height:18px"> method:'tree'
- };
- this);
- if(!root){
- root=newExt.tree.AsyncTreeNode({
- id:'0',108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> text:"系统菜单"
- if(!nav){
- nav=newExt.tree.TreePanel({
- title:"左部导航",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> width:232,248); line-height:18px"> autoScroll: animate: loader:loader,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> root:root,248); line-height:18px"> enableDD: listeners:{
- 'click':function(node,event){
- if(!node.isLeaf()){
- //为目录节点时,点击不进入链接
- event.stopEvent();
- //事件:添加右键菜单
- nav.on("contextmenu",153); background-color:inherit; font-weight:bold">this.showTreeMenu);
- //事件:当节点文本改变时触发异步更新标题
- nav.on("textchange",newText,oldText){
- if(!titleChangeFlag&&newText!=oldText){
- mgr.ajaxUpdateTitle(node.id,153); background-color:inherit; font-weight:bold">function(success){
- if(!success){
- Ext.Msg.show({
- title:"操作失败!",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> msg:"菜单修改失败!",248); line-height:18px"> buttons:Ext.Msg.OK,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> icon:Ext.MessageBox.ERROR
- titleChangeFlag=true;
- node.setText(oldText);
- //事件:当节点移动时触发
- nav.on("movenode",153); background-color:inherit; font-weight:bold">function(tree,node,oldParent,newParent,index){
- mgr.ajaxMoveNode(node.id,oldParent.id,newParent.id,index);
- //事件:当节点删除时触发
- nav.on("remove",parentNode,153); background-color:inherit; font-weight:bold">if(removeFlag){
- mgr.ajaxRemoveNode(node.id);
- /*事件:节点选中单击编辑
- if(!navEditor){
- navEditor=newExt.tree.TreeEditor(nav,0); background-color:inherit">allowBlank:false,0); background-color:inherit">ignoreNoChange:true,0); background-color:inherit">completeOnEnter:true,0); background-color:inherit">blankText:'标题不能为空',0); background-color:inherit">selectOnFocus:true
- });
- }*/
- this.setLeafMenu();//设置叶子菜单
- this.setDirMenu();//设置目录菜单
- setMgr:function(manager){
- mgr=manager;
- getMgr:returnmgr;
- //设置叶子菜单
- setLeafMenu:if(!leafMenu){
- leafMenu=newExt.menu.Menu({
- items:[{
- text:"修改标题",248); line-height:18px"> handler: navEditor=newExt.tree.TreeEditor(nav,248); line-height:18px"> allowBlank:false,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> ignoreNoChange: completeOnEnter: blankText:'标题不能为空',248); line-height:18px"> selectOnFocus: navEditor.triggerEdit(nodeSelected);
- "-",{
- text:"编辑",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> FormEditWin.showEditLeafWin(nodeSelected);
- text:"删除",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> handler:this.delTreeItemComfirm
- }]
- setDirMenu:if(!dirMenu){
- dirMenu= FormEditWin.showEditDirWin(nodeSelected);
- text:"添加叶子节点",248); line-height:18px"> FormEditWin.showAddLeafWin(nodeSelected);
- text:"添加目录节点",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> FormEditWin.showAddDirWin(nodeSelected);
- showTreeMenu: nodeSelected=node;
- nodeSelected.select();
- if(node.isLeaf()){
- //显示叶子节点菜单
- leafMenu.showAt(e.getPoint());
- }else{
- //显示目录节点菜单
- dirMenu.showAt(e.getPoint());
- delTreeItemComfirm: Ext.Msg.confirm("确认删除","确定要删除所选节点吗?",153); background-color:inherit; font-weight:bold">function(btn){
- if(btn=="yes"){
- NavTree.delTreeItem();
- delTreeItem:if(nodeSelected!=nav.getRootNode()){
- removeFlag=true;
- nodeSelected.remove();
- }else{
- Ext.Msg.alert("警告","不能删除树的根节点!");
- show: nav.render(Ext.getBody());
- nav.getRootNode().toggle();
- }();
- //文档加载完毕执行
- Ext.onReady( Ext.BLANK_IMAGE_URL="../scripts/ext/resources/images/default/s.gif";
- typeof(NavigateManager)=="undefined"){
- "请先设置DWR,并实例化NavigateManager");
- NavTree.setMgr(NavigateManager);
- NavTree.init();
- NavTree.show();
- });
servlet 配置:
[html]
copy
- <?xmlversion="1.0"encoding="UTF-8"?>
- <web-appversion="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- servlet>
- servlet-name>navigatejson</servlet-class com.demo.navigate.web.NavigateJsonServlet
- >navigateedit com.demo.navigate.web.NavigateEditServlet
- >navigatesave com.demo.navigate.web.NavigateSaveServlet
- servlet-mappingurl-pattern>/navigatejson>/navigateedit>/navigatesave>dwr-invoker>uk.ltd.getahead.dwr.DWRServletinit-paramparam-name>debugparam-value>true allowGetForSafariButMakeForgeryEasier
- load-on-startup>1>/dwr/*welcome-file-listwelcome-file>index.jspweb-app>