dojo中关于tree更新的问题
前阶段在搞dojo中的tree是发现一个问题,当我把tree的store写死在页面上tree的控件里,当我更新完tree刷新页面时,tree并没有发生变化
- <div dojoType="dojo.data.JsonItemStore" jsId="account"
- url="/Fiscal/initAccountTreeAction.action"></div>
- <div dojoType="dijit.Tree" id=tree store="account" query="{type:'continent'}"
- labelAttr="name" typeAttr="type"></div>
从代码中可以看到,tree的数据来源是通过自定义initAccountTreeAction中返回的数据得到的,这个Action代码如下
- package com.sanfang.fiscal.action.account;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- import com.sanfang.fiscal.business.interfaces.IBusinessDelegate;
- import com.sanfang.fiscal.util.AjaxUtil;
- public class AccountTreeAction extends ActionSupport {
- private static final long serialVersionUID = 1L;
- private IBusinessDelegate businessDelegate;
- private String mark;
- private AjaxUtil ajaxUtil;
- public String getMark() {
- return mark;
- }
- public void setMark(String mark) {
- this.mark = mark;
- }
- public AjaxUtil getAjaxUtil() {
- return ajaxUtil;
- }
- public void setAjaxUtil(AjaxUtil ajaxUtil) {
- this.ajaxUtil = ajaxUtil;
- }
- public IBusinessDelegate getBusinessDelegate() {
- return businessDelegate;
- }
- public void setBusinessDelegate(IBusinessDelegate businessDelegate) {
- this.businessDelegate = businessDelegate;
- }
- public String initAccountTree() throws IOException{
- Map session = ActionContext.getContext().getSession();
- ServletActionContext.getResponse().setContentType("text/json; charset=UTF-8");
- String language = (String)session.get("language");
- String fiscalSetID = (String)session.get("fiscalSetID");
- List<String> accountSetList = new ArrayList<String>();
- if("all".equals(mark)){
- accountSetList = businessDelegate.findAccountSetIDIsUsedByFiscalSetID(fiscalSetID, null, null);
- }else if("isSettleAcct".equals(mark)){
- accountSetList = businessDelegate.findAccountSetIDIsUsedByFiscalSetID(fiscalSetID, true, null);
- }else if("isUserObjAcct".equals(mark)){
- accountSetList = businessDelegate.findAccountSetIDIsUsedByFiscalSetID(fiscalSetID, null, true);
- }
- String result = ajaxUtil.AccountListToTreeJSON(language, fiscalSetID, accountSetList);
- ServletActionContext.getResponse().getWriter().write(result);
- return null;
- }
- }
可以看到是通过response动态写到页面上的,可以这样有一个问题,每当动态改变了tree后(比如增加删除子节点),tree的store并没有直接在页面中重新更新加载进来。
没办法,只能在去研究tree,后发现利用tree的动态创建加上用dojo异部调用函数dojo.xhrGet可以解决这个不更新的问题, 具体代码如下:
- //把数据转换为符合tree的store格式的方法
- function strToObj(json){
- return eval("("+json+")");
- }
- function init(response){
- //先创建tree的store的显示数据
- var data = strToObj(response);
- //创建store
- var store1 = new dojo.data.ItemFileReadStore({data:data});
- //创建tree,这是的tree数据是动态创建出来的,同时tree也是动态的
- var _tree=new dijit.Tree({
- label:"Accounting",
- store:store1,
- query:{type:'continent'},
- labelAttr:"name",
- typeAttr:"type",
- preventCache:true
- },
- dojo.byId('tree'));
- dojo.subscribe("tree", null, treeHandler);
- }
- function test(){
- //采用异步调用,目的是时时更新tree的store
- dojo.xhrGet({
- url: '/Fiscal/initAccountTreeAction.action',
- method: 'POST',
- load: function(response, ioArgs) {
- //此处的resonse就是从Action得到的数据
- init(response);
- },
- preventCache: true,
- content: {mark:'all'}
- });
- }
- //每次刷新页面都调用test方法
- dojo.addOnLoad(test);
当然在页面里也是要定义tree控件的,但只要简单定一个div就可以了,具体Tree的格式问题可以参考Demo里test文件夹里面关于tree store格式的资料。
- <div id="tree"></div>