---
title: "extjs4 的 异步加载"
author: "xinjiang"
date: "2013-01-06T14:08:56Z"
tags: []
categories: ["Web前端"]
slug: "extjs4"
draft: false
lastmod: "2013-01-06T14:14:11Z"
id: 1763672
url: "https://xinjiang.iteye.com/blog/1763672"
---

```

```

```
unitTreeStore = Ext.create('Ext.data.TreeStore', {
	storeId:'unitTreeStore',
	nodeParam: 'pid',  //向服务端发送结点的名称，默认为node
	defaultRootId: '', //向服务端发送root结点的值
	proxy: {
		type : 'ajax',
		url : 'unit/getUnits.do',
		reader : {
			type : 'json',
		}
	}
});

var unitTreePanel = Ext.create(&quot;Ext.tree.Panel&quot;,{
	title: '单位列表',
  	width: 250,
    	region: 'west',
        store: unitTreeStore,
    	rootVisible: false
});
//让ext全屏显示
Ext.create('Ext.container.Viewport',{
	layout:'border',//Border布局
	items: [unitTreePanel]
});
```

 

服务器返回的字符串是：\[{ id : '2',text : '分公司1',leaf:false},{ id : '3',text : '分公司2',leaf:true},{ id : '4',text : '分公司3',leaf:true},{ id : '5',text : '分公司4',leaf:true},{ id : '6',text : '分公司5',leaf:true},{ id : '7',text : '分公司6',leaf:true}]

不管你用的是struts还是spring mvc只要返回是类似于这样的json字符串就行啦

在这儿主要说明一点默认第一次发送url为http://localhost:8989/oa\_project/unit/getUnits.do?node=root

而我上面的代码发送的是http://localhost:8989/oa\_project/unit/getUnits.do?pid=

如果你不想发送参数名是node而是pid可以这样设置：nodeParam: 'pid'

root结点的默认值为root，这样在后台还的单独处理，你可以设置为0或者是其他的值，只需要加上一条这样的

语句：defaultRootId: 0

 

效果图如下：

![](http://dl.iteye.com/upload/attachment/0078/9104/2daf96ec-0e7a-3827-8d5e-2cbfe9953a13.png)