ExtJS筆記 Tree

The Tree Panel Component is one of the most versatile Components in Ext JS and is an excellent tool for displaying heirarchical data in an application. Tree Panel extends from the same class as Grid Panel, so all of the benefits of Grid Panels - features, extensions, and plugins can also be used on Tree Panels. Things like columns, column resizing, dragging and dropping, renderers, sorting and filtering can be expected to work similarly for both components.css

樹面板組件是在 ExtJS 裏面最多彩繽紛的組件之一,用於顯示層次狀明顯的數據來講十分適合。樹面板跟 Grid 都是來自同一個基類的,之因此這樣的設計,是爲了那些擴展或者插件通通均可以複用一致的功能。好比多列、尺寸控制、拖放、渲染和篩選這些功能都是二者共性的內容。html

Let's start by creating a very simple Tree.node

讓咱們開始建立一個顆很是簡單的樹。ios

Ext.create('Ext.tree.Panel', {
    renderTo: Ext.getBody(),
    title: 'Simple Tree',
    width: 150,
    height: 150,
    root: {
        text: 'Root',
        expanded: true,
        children: [
            {
                text: 'Child 1',
                leaf: true
            },
            {
                text: 'Child 2',
                leaf: true
            },
            {
                text: 'Child 3',
                expanded: true,
                children: [
                    {
                        text: 'Grandchild',
                        leaf: true
                    }
                ]
            }
        ]
    }
});

 

This Tree Panel renders itself to the document body. We defined a root node that is expanded by default. The root node has three children, the first two of which are leaf nodes which means they cannot have any children. The third node is not a leaf node and has has one child leaf node. The text property is used as the node's text label. See Simple Tree for a live demo.git

此樹面板渲染到 document.body 元素上。咱們把定義的根節點(The Root Node)自動擴張開來,這是默認的狀況。根節點有三個子節點 ,其中前兩個是 leaf 節點,表示他們下面沒有任何子節點(children)了(終結了)。第三個節點是一個葉子節點,已經有一個 child 的葉節點(one child leaf node)。 text 屬性是節點的顯示的文本。可打開例子看看效果如何。ajax

Internally a Tree Panel stores its data in a TreeStore. The above example uses the root config as a shortcut for configuring a store. If we were to configure the store separately, the code would look something like this:編程

樹面板的數據存儲在 TreeStore。上面的例子看不見 Store 配置的地方不是沒有 Store,而是使用內部缺省的。若是咱們要另外配置不一樣的 Store,應該看起來像這樣:json

var store = Ext.create('Ext.data.TreeStore', {
    root: {
        text: 'Root',
        expanded: true,
        children: [
            {
                text: 'Child 1',
                leaf: true
            },
            {
                text: 'Child 2',
                leaf: true
            },
            ...
        ]
    }
});

Ext.create('Ext.tree.Panel', {
    title: 'Simple Tree',
    store: store,
    ...
});

 

For more on Stores see the Data Guide.api

參閱《DataGuide》以瞭解更多 Store 內容。數組

The Node Interface

In the above examples we set a couple of different properties on tree nodes. But what are nodes exactly? As mentioned before, the Tree Panel is bound to a TreeStore. A Store in Ext JS manages a collection of Model instances. Tree nodes are simply Model instances that are decorated with a NodeInterface. Decorating a Model with a NodeInterface gives the Model the fields, methods and properties that are required for it to be used in a tree. The following is a screenshot that shows the structure of a node in the developer tools.

從上面的例子咱們能夠看到關於樹節點其下的許多屬性。那麼真正的節點究竟爲什麼物?前面已提到過,樹面板綁定到 TreeStore。而 ExtJS 中的 Store 是負責管理 Model 實例的集合。樹節點只是 Model  實例經過 NodeInterface 的裝飾接口。用 NodeInterface 裝飾 Model 的好處是賦予了 Model  在樹控件的狀態下,有新的方法與屬性。如下的截圖顯示了在開發工具節點其結構如何。

 

A model instance decorated with the NodeInterface

 

In order to see the full set of fields, methods and properties available on nodes, see the API documentation for the NodeInterface class.

要全面瞭解一下節點的屬性、方法、事件,應參閱 API 文檔。

Visually changing your tree

Let's try something simple. When you set the useArrows configuration to true, the Tree Panel hides the lines and uses arrows as expand and collapse icons.

讓咱們試試簡單的。在你設置 useArrows 配置項爲 true 的時候,樹面板會隱藏旁邊的線條,而採用圖標箭頭表示展開和摺疊。

 

Arrows

 

Setting the rootVisible property to false visually removes the root node. By doing this, the root node will automatically be expanded. The following image shows the same tree with rootVisible set to false and lines set to false.

設置根節點的 rootVisible 可決定根節點顯示與否。經過這樣作的話,通常就是根節點會自動擴大。下面的圖片顯示,rootVisible 設置爲 false,並設置 lines 爲 false 的時候看不見線的樹。

 

Root not visible and no lines

 

Multiple columns

Since Tree Panel extends from the same base class as Grid Panel adding more columns is very easy to do.

鑑於樹面板跟 Grid 面板來自同一個基類,因此構成多列是很是輕鬆的。

var tree = Ext.create('Ext.tree.Panel', {
    renderTo: Ext.getBody(),
    title: 'TreeGrid',
    width: 300,
    height: 150,
    fields: ['name', 'description'],
    columns: [{
        xtype: 'treecolumn',
        text: 'Name',
        dataIndex: 'name',
        width: 150,
        sortable: true
    }, {
        text: 'Description',
        dataIndex: 'description',
        flex: 1,
        sortable: true
    }],
    root: {
        name: 'Root',
        description: 'Root description',
        expanded: true,
        children: [{
            name: 'Child 1',
            description: 'Description 1',
            leaf: true
        }, {
            name: 'Child 2',
            description: 'Description 2',
            leaf: true
        }]
    }
});

 

The columns configuration expects an array of Ext.grid.column.Column configurations just like a Grid Panel would have. The only difference is that a Tree Panel requires at least one column with an xtype of 'treecolumn'. This type of column has tree-specific visual effects like depth, lines and expand and collapse icons. A typical Tree Panel would have only one 'treecolumn'.

和 Grid 面板那樣子配置 Ext.grid.column.Column,Tree 面板也是經過 columns 數組進行配置。惟一區別在於 Tree 面板至少得要一個 xtype 是「treecolumn」的列。該類型的列根據樹而設計的,擁有深度(depth)、線條(lines)、展開與閉合圖標等的特性。典型的 Tree 面板即是一個單獨的「treecolumn」。

The fields configuration is passed on to the Model that the internally created Store uses (See the Data Guide for more information on Models). Notice how the dataIndex configurations on the columns map to the fields we specified - name and description.

配置項 fields 會由內部的 Store 被複制到 Model 上(該方面可參閱《Data Guide》的 Model 部分)。請注意列其 dataIndex 配置項就是映射到 field 的。

It is also worth noting that when columns are not defined, the tree will automatically create one single treecolumn with a dataIndex set to 'text'. It also hides the headers on the tree. To show this header when using only a single column set the hideHeaders configuration to 'false'.

應該提出,當未定義列的時候,樹會自動建立一個單獨的 treecolumn,帶有「text」 的 dataIndex。還會隱藏樹的頭部。要顯示的話,可設置配置項 hideHeaders 爲 false。

Adding nodes to the tree

The root node for the Tree Panel does not have to be specified in the initial configuration. We can always add it later:

不必定要在配置的時候將全部的節點添加到樹上,及後再加也能夠的。

var tree = Ext.create('Ext.tree.Panel');
tree.setRootNode({
    text: 'Root',
    expanded: true,
    children: [{
        text: 'Child 1',
        leaf: true
    }, {
        text: 'Child 2',
        leaf: true
    }]
});

 

Although this is useful for very small trees with only a few static nodes, most Tree Panels will contain many more nodes. So let's take a look at how we can programmatically add new nodes to the tree.

這樣子在一棵靜態的樹上加上幾個節點毫無問題,可是通常而言樹面板仍是會動態地加入許多節點。這樣咱們就來看看如何經過編程來添加新的節點樹。

var root = tree.getRootNode();

var parent = root.appendChild({
    text: 'Parent 1'
});

parent.appendChild({
    text: 'Child 3',
    leaf: true
});

parent.expand();

 

Every node that is not a leaf node has an appendChild method which accepts a Node, or a config object for a Node as its first parameter, and returns the Node that was appended. The above example also calls the expand method to expand the newly created parent.

只要不是 leaf 節點,它都有 appendChild 的方法,送入一個節點的實例,或者節點的配置項對象做爲參數,該方法就是返回新建立的節點。上一例調用了新建立節點其 expand 的方法。

 

Appending to the tree

 

Also useful is the ability to define children inline when creating the new parent nodes. The following code gives us the same result.

另外你能夠經過內聯的寫法建立父節點。下一例與上例的做用同樣。

var parent = root.appendChild({
    text: 'Parent 1',
    expanded: true,
    children: [{
        text: 'Child 3',
        leaf: true
    }]
});

 

Sometimes we want to insert a node into a specific location in the tree instead of appending it. Besides the appendChild method, Ext.data.NodeInterfacealso provides insertBefore and insertChild methods.

有些時候咱們想將節點插入到某個固定的位置。這樣的話,就須要 insertBefore 或 insertChild 方法,都由 Ext.data.NodeInterface 提供。

var child = parent.insertChild(0, {
    text: 'Child 2.5',
    leaf: true
});

parent.insertBefore({
    text: 'Child 2.75',
    leaf: true
}, child.nextSibling);

 

The insertChild method expects an index at which the child will be inserted. The insertBefore method expects a reference node. The new node will be inserted before the reference node.

insertChild 方法須要一個 child 將被插入索引。 insertBefore 方法預計的參考節點。將參考節點以前插入新的節點。

 

Inserting children into the tree

 

NodeInterface also provides several more properties on nodes that can be used to reference other nodes.

NodeInterface 還提供瞭如下幾個屬性,供其餘節點做「引用」時之用。

Loading and Saving Tree Data using a Proxy

Loading and saving Tree data is somewhat more complex than dealing with flat data because of all the fields that are required to represent the hierarchical structure of the tree. This section will explain the intricacies of working with tree data.

加載和保存tree數據要比處理平面數據複雜,由於數據表明了樹形層次結構。本小節說明操做樹形數據的複雜工做。

NodeInterface Fields

The first and most important thing to understand when working with tree data is how the NodeInterface class' fields work. Every node in a Tree is simply aModel instance decorated with the NodeInterface's fields and methods. Assume for a moment that an application has a Model called Person. A Person only has two fields - id and name:

理解tree數據如何工做的首要事情是知道NodeInterface類如何工做。樹中的每一個節點都是一個model實例,並被修飾了NodeInterface的自動和方法。假設一個app具備一個model叫作person,有兩個字段id和name:

Ext.define('Person', {
    extend: 'Ext.data.Model',
    fields: [
        { name: 'id', type: 'int' },
        { name: 'name', type: 'string' }
    ]
});

 

At this point Person is just a plain vanilla Model. If an instance is created, it can easily be verified that it only has two fields by looking at its fieldscollection

這是person僅是一個平面數據。若是一個實例被建立,能夠很容易驗證它只有兩個字段:

console.log(Person.prototype.fields.getCount()); // outputs '2'

 

When the Person model is used in a TreeStore, something interesting happens. Notice the field count now:

當一個personmodel用在了TreeStore中,一些有趣的事情發生了。注意下面的字段數統計:

var store = Ext.create('Ext.data.TreeStore', {
    model: 'Person',
    root: {
        name: 'Phil'
    }
});

console.log(Person.prototype.fields.getCount()); // outputs '24'

 

The Person model's prototype got 22 extra fields added to it just by using it in a TreeStore. All of these extra fields are defined on the NodeInterface class and are added to the Model's prototype the first time an instance of that Model is used in a TreeStore (by setting it as the root node).

person模型的prototype得到了22個額外添加的字段,僅僅是在treestore中使用它。全部這些額外的字段是在 NodeInterface類中定義的,而且在第一次在treestore中使用的時候(經過設置它爲root 節點)被加入model的prototype。

So what exactly are these 22 extra fields, and what do they do? A quick look at the NodeInterface source code reveals that it decorates the Model with the following fields. These fields are used internally to store information relating to the tree's structure and state:

{name: 'parentId',   type: idType,    defaultValue: null},
{name: 'index',      type: 'int',     defaultValue: null, persist: false},
{name: 'depth',      type: 'int',     defaultValue: 0, persist: false},
{name: 'expanded',   type: 'bool',    defaultValue: false, persist: false},
{name: 'expandable', type: 'bool',    defaultValue: true, persist: false},
{name: 'checked',    type: 'auto',    defaultValue: null, persist: false},
{name: 'leaf',       type: 'bool',    defaultValue: false},
{name: 'cls',        type: 'string',  defaultValue: null, persist: false},
{name: 'iconCls',    type: 'string',  defaultValue: null, persist: false},
{name: 'icon',       type: 'string',  defaultValue: null, persist: false},
{name: 'root',       type: 'boolean', defaultValue: false, persist: false},
{name: 'isLast',     type: 'boolean', defaultValue: false, persist: false},
{name: 'isFirst',    type: 'boolean', defaultValue: false, persist: false},
{name: 'allowDrop',  type: 'boolean', defaultValue: true, persist: false},
{name: 'allowDrag',  type: 'boolean', defaultValue: true, persist: false},
{name: 'loaded',     type: 'boolean', defaultValue: false, persist: false},
{name: 'loading',    type: 'boolean', defaultValue: false, persist: false},
{name: 'href',       type: 'string',  defaultValue: null, persist: false},
{name: 'hrefTarget', type: 'string',  defaultValue: null, persist: false},
{name: 'qtip',       type: 'string',  defaultValue: null, persist: false},
{name: 'qtitle',     type: 'string',  defaultValue: null, persist: false},
{name: 'children',   type: 'auto',   defaultValue: null, persist: false}

 

NodeInterface Fields are Reserved Names   NodeInterface 字段與保留字

It is important to note that all of the above field names should be treated as "reserved" names. For example, it is not allowed to have a field called "parentId" in a Model, if that Model is intended to be used in a Tree, since the Model's field will override the NodeInterface field. The exception to this rule is when there is a legitimate need to override the persistence of a field.

很重要的一點是,上面這些字段名稱要被當作 "reserved" 名稱。例如,在model中不容許具備一個叫作 "parentId" 的字段,若是model將被用於tree,model的字段將覆蓋NodeInterface字段。這個規則的例外是,若是覆蓋這些字段是正當的。

Persistent Fields vs Non-persistent Fields and Overriding the Persistence of Fields  持久字段vs非持久化字段和覆蓋持久化字段

Most of NodeInterface's fields default to persist: false. This means they are non-persistent fields by default. Non-persistent fields will not be saved via the Proxy when calling the TreeStore's sync method or calling save() on the Model. In most cases, the majority of these fields can be left at their default persistence setting, but there are cases where it is necessary to override the persistence of some fields. The following example demonstrates how to override the persistence of a NodeInterface field. When overriding a NodeInterface field it is important to only change the persist property. name,type, and defaultValue should never be changed.

多數NodeInterface's 字段具備缺省值persist: false,這意味着它們缺省是非持久化字段。當調用TreeStore的sync()或save()方法時,非持久化字段不會經過Proxy保存。在多數狀況下,這些字段不須要持久化,可是有些狀況下須要。下面的示例演示瞭如何覆蓋NodeInterface 的持久化設置。當覆蓋一個NodeInterface 字段,重要的一點是修改persist 屬性。 name,type, 和defaultValue屬性不要修改。

 

// overriding the persistence of NodeInterface fields in a Model definition
Ext.define('Person', {
    extend: 'Ext.data.Model',
    fields: [
        // Person fields
        { name: 'id', type: 'int' },
        { name: 'name', type: 'string' }

        // override a non-persistent NodeInterface field to make it persistent
        { name: 'iconCls', type: 'string',  defaultValue: null, persist: true },
    ]
});

 

Let's take a more in-depth look at each NodeInterface field and the scenarios in which it might be necessary to override its persist property. In each example below it is assumed that a Server Proxy is being used unless otherwise noted.

咱們來看看一個關於NodeInterface 字段更深刻的例子和須要覆蓋persist 屬性的場景。下面的每一個例子都假定使用了Server Proxy ,除非特別說明。

Persistent by default:

缺省持久化:

  • parentId - used to store the id of a node's parent node. This field should always be persistent, and should not be overridden.用於保存父節點,不可覆蓋。
  • leaf - used to indicate that the node is a leaf node, and therefore cannot have children appended to it. This field should not normally need to be overridden.用於指示是否葉子節點,葉子節點不能被添加子節點。這個字段一般不須要覆蓋。

Non-persistent by default:

缺省非持久化:

  • index - used to store the order of nodes within their parent. When a node is inserted or removed, all of its sibling nodes after the insertion or removal point will have their indexes updated. If desired, the application can use this field to persist the ordering of nodes. However, if the server uses a different method of storing order, it may be more appropriate to leave the index field as non-persistent. When using a WebStorage Proxy if storing order is required, this field must be overridden to be persistent. Also if client-side sorting is being used it is recommended for the index field to be left as non-persistent, since sorting updates the indexes of all the sorted nodes, which would cause them to be persisted on next sync or save if the persist property is true.用來存儲在父節點中的屬性。當一個節點被 inserted or removed,全部在插入或移除點後的節點都要更新其索引。必要時,app可使用此節點來持久化節點順序。然而,若是服務器使用其餘不一樣的方法來保存順序,將index字段設爲非持久化會更合適。當使用WebStorage Proxy,若是須要保存順序,這個字段必須被覆蓋以持久化。而且,若是使用客戶端排序,推薦將index字段保持爲非持久化,由於排序會更新全部已排序字段的index,若是persist爲true,這會致使在下次調用sync()或save()方法時持久化它們。
  • depth - used to store the depth of a node in the tree hierarchy. Override this field to turn on persistence if the server needs to store the depth field. When using a WebStorage Proxy it is recommended to not override the persistence of the depth field since it is not needed to properly store the tree structure and will just take up extra space.用於存儲節點在樹層次中的深度。若是服務端須要保存depth字段,則覆蓋這個屬性。當使用WebStorage Proxy,建議不要存儲這個屬性,由於保存樹結構不須要它,從而僅僅是浪費空間。
  • checked - this field should be overridden to be persistent if the tree is using the checkbox feature   若是樹須要checkbox,則須要覆蓋此屬性
  • expanded - used to store the expanded/collapsed state of a node. This field should not normally need to be overridden. 保存展開/收縮狀態,通常不須要覆蓋
  • expandable - used internally to indicate that this node is expandable. Do not override the persistence of this field.  內部使用,用來判斷節點是否可展開。不要覆蓋此屬性。
  • cls - used to apply a css class to the node when it is rendered in a TreePanel. Override this field to be persistent if desired.  節點的css樣式類。根據須要覆蓋此屬性。
  • iconCls - used to apply a css class to the node's icon when it is rendered in a TreePanel. Override this field to be persistent if desired.節點圖標樣式。根據須要覆蓋。
  • icon - used to apply a cutom icon to the node node when it is rendered in a TreePanel. Override this field to be persistent if desired.節點圖標。根據須要覆蓋。
  • root - used to indicate that this node is the root node. This field should not be overridden.用於指明是否根節點,不要覆蓋。
  • isLast - used to indicate that this node is the last of its siblings. This field should not normally need to be overridden.兄弟中的最後一個接待,不要覆蓋。
  • isFirst - used to indicate that this node is the first of its siblings. This field should not normally need to be overridden.兄弟中第一個節點,不要覆蓋。
  • allowDrop - used internally to deny dropping on the node. Do not override the persistence of this field.內部使用,是否拒絕拖放到此節點,不要覆蓋。
  • allowDrag - used internally to deny dragging the node. Do not override the persistence of this field.內部使用,是否可拖動此節點,不要覆蓋。
  • loaded - used internally to indicate that the node's children have been loaded. Do not override the persistence of this field.內部使用,節點的孩子是否已加載。不要覆蓋。
  • loading - used internally to indicate that the proxy is in the process of loading the node's children. Do not override the persistence of this field.內部使用,節點是否真正加載孩子。不要覆蓋。
  • href - used to specify a url that the node should be a link to. Override to be persistent if desired.節點連接,根據須要覆蓋。
  • hrefTarget - used to specify the target for the href. Override to be persistent if desired.     href的target屬性,根據須要覆蓋。
  • qtip - used to add a tooltip text to the node. Override to be persistent if desired.   用於添加提示文本到節點,根據須要覆蓋。
  • qtitle - used to specify the title for the tooltip. Override to be persistent if desired.  tooltip的title,根據須要覆蓋。
  • children - used internally when loading a node and its children all in one request. Do not override the persistence of this field.  內部使用,用於一次加載節點及其孩子,不要覆蓋。

Loading Data    加載數據

There are two ways to load tree data. The first is to for the proxy to fetch the entire tree all at once. For larger trees where loading everything at once is not ideal, it may be preferable to use the second method - dynamically loading the children for each node when it is expanded.

有兩種方式加載樹數據。第一種是一次加載整棵樹。對於大的樹,使用第二種方法--在節點展開的時候動態加載。

Loading the Entire Tree    加載整棵樹

Internally the tree only loads data in response to a node being expanded. However the entire hierarchy can be loaded if the proxy retrieves a nested object containing the whole tree structure. To accomplish this, initialize the TreeStore's root node to expanded:

在內部,tree只在節點展開的時候加載數據。然而,若是proxy獲取了一次獲取了包含整棵樹的嵌套數據,樹能夠一次加載完成。要作到這點,初始化時將TreeStore的root設爲expanded:

Ext.define('Person', {
    extend: 'Ext.data.Model',
    fields: [
        { name: 'id', type: 'int' },
        { name: 'name', type: 'string' }
    ],
    proxy: {
        type: 'ajax',
        api: {
            create: 'createPersons',
            read: 'readPersons',
            update: 'updatePersons',
            destroy: 'destroyPersons'
        }
    }

});

var store = Ext.create('Ext.data.TreeStore', {
    model: 'Person',
    root: {
        name: 'People',
        expanded: true
    }
});

Ext.create('Ext.tree.Panel', {
    renderTo: Ext.getBody(),
    width: 300,
    height: 200,
    title: 'People',
    store: store,
    columns: [
        { xtype: 'treecolumn', header: 'Name', dataIndex: 'name', flex: 1 }
    ]
});

 

Assume that the readPersons url returns the following json object

假設readPersons url返回下面的json對象

{
    "success": true,
    "children": [
        { "id": 1, "name": "Phil", "leaf": true },
        { "id": 2, "name": "Nico", "expanded": true, "children": [
            { "id": 3, "name": "Mitchell", "leaf": true }
        ]},
        { "id": 4, "name": "Sue", "loaded": true }
    ]
}

 

That's all that's needed to load the entire tree.

那些就是加載整棵樹所需的所有。

 

Tree with Bulk Loaded Data

 

Important items to note:須要注意的要點:

  • For all non-leaf nodes that do not have children (for example, Person with name Sue above), the server response MUST set the loaded property to true. Otherwise the proxy will attempt to load children for these nodes when they are expanded.對於全部沒有孩子的非葉子節點(例如,名爲Sue的Person),服務器響應必須設置loaded 屬性爲true。不然,當它們被展開的時候,proxy會視圖去獲取其子節點。
  • The question then arises - if the server is allowed to set the loaded property on a node in the JSON response can it set any of the other non-persistent fields? The answer is yes - sometimes. In the example above the node with name "Nico" has is expanded field set to true so that it will be initially displayed as expanded in the Tree Panel. Caution should be exercised as there are cases where this is not appropriate and could cause serious problems, like setting the root property on a node that is not the root node for example. In general loaded and expanded are the only cases where it is recommended for the server to set a non-persistent field in the JSON response.問題來了--若是服務器被容許設置loaded 屬性,它能夠設置其它非持久化字段嗎?答案是 yes --有時候。上例中,"Nico "節點是被展開的節點,所以在tree panel中初始是展開的。須要注意的是,有些屬性設置會引發嚴重的問題,例如對非root節點設置root屬性。一般,loaded 和expanded 是僅有的兩個推薦在服務器端設置的非持久化屬性。

Dynamically Loading Children When a Node is Expanded    當節點展開時的動態加載

For larger trees it may be desirable to only load parts of the tree by loading child nodes only when their parent node is expanded. Suppose in the above example, that the node with name "Sue" does not have its loaded field set to true by the server response. The Tree would display an expander icon next to the node. When the node is expanded the proxy will make another request to the readPersons url that looks something like this:

對於較大的樹,指望的作法是開始只加載部分節點,而後當父節點展開的時候,才加載孩子節點。假設在上面的例子中,「sue節點的loaded屬性沒有被服務器設置爲true。樹會在節點的旁邊顯示一個expander 圖標。當節點被展開,proxy會發起另一個請求到readPersons 連接,如:

/readPersons?node=4

 

This tells the server to retrieve the child nodes for the node with an id of 4. The data should be returned in the same format as the data that was used to load the root node:

這告訴服務器去獲取id爲4的節點的孩子節點。數據將以與加載根節點同樣的格式返回:

{
    "success": true,
    "children": [
        { "id": 5, "name": "Evan", "leaf": true }
    ]
}

 

Now the Tree looks something like this:

 如今,樹節點看起來是這樣:

Tree with Dynamically Loaded Node

 

Saving Data   保存數據

Creating, updating, and deleting nodes is handled automatically and seamlessly by the Proxy.

建立、更新、刪除節點會自動處理,並經過proxy無縫銜接。

Creating a New Node   建立新節點

// Create a new node and append it to the tree:
var newPerson = Ext.create('Person', { name: 'Nige', leaf: true });
store.getNodeById(2).appendChild(newPerson);

 

Since the proxy is defined directly on the Model, the Model's save() method can be used to persist the data:

因爲直接在model上定義了proxy,model的save()方法能夠被用來持久化數據:

newPerson.save();

 

Updating an Existing Node   更新節點

store.getNodeById(1).set('name', 'Philip');

 

Removing a Node   移除節點

store.getRootNode().lastChild.remove();

 

Bulk Operations   批量操做

After creating, updating, and removing several nodes, they can all be persisted in one operation by calling the TreeStore's sync() method:

在建立、更新、刪除幾個節點後,它們能夠調用TreeStore的sync()方法在一個操做中完成持久化。

store.sync();
相關文章
相關標籤/搜索