extjs studies four treePanel

The use of the tree is very frequent, the various operations on the tree node. And database interactivity.
tree.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <title> Tree display </title>
        <!-- Ext -->
        <link rel="stylesheet" type="text/css" href="ext4/resources/css/ext-all.css" />
        <script type="text/javascript" src="ext4/bootstrap.js">
        </script>
        <script type="text/javascript" src="ext4/locale/ext-lang-zh_CN.js">
        </script>
        <!-- tree.jsp page -->
        <link rel="stylesheet" type="text/css" href="ext4/ux/css/TabScrollerMenu.css" />
        <script type="text/javascript" src="forever.js">
        </script>
        <script type="text/javascript" src="tree-simple.js">
        </script>
        <script type="text/javascript" src="tree-check.js">
        </script>
        <script type="text/javascript" src="tree-app.js">
        </script>
    </head>
    <body>
    </body>


forever.js are tools:
Ext.ns("org.forever.util");

Ext.define('org.forever.util.TreeUtil', {
    extend: 'Object',
    userName: ' Default ',//property
    constructor: function(config){// Constructor
        Ext.apply(this, config);
    }
});
// Recursive update node selected
org.forever.util.TreeUtil.updateCheckStatus = function(parentNode, checked){
    Ext.each(parentNode.childNodes, function(childNode, index, allItems){
        //Ext.Msg.alert(' Node information ',node.get('text')+';index=' + index);
        childNode.set('checked', checked);
        org.forever.util.TreeUtil.updateCheckStatus(childNode, checked);
    });
}


tree-app.js is the process initialization code:
Ext.Loader.setConfig({
    enabled: true
});
Ext.Loader.setPath('Ext.ux', 'ext4/ux/');
Ext.onReady(function(){
    var tabscrollermenu = Ext.create('Ext.ux.TabScrollerMenu', {
        ptype: 'tabscrollermenu',
        maxText: 15,
        pageSize: 10
    });
    var simpleTreePanel = createSimpleTree();
    var checkTreePanel = createCheckTree();
    var tabPanel = Ext.create('Ext.tab.Panel', {
        region: 'center',
        activeTab: 1,
        plugins: [tabscrollermenu],
        items: [simpleTreePanel, checkTreePanel]
    });

    Ext.create('Ext.Window', {
        title: 'tree app',
        width: 500,
        height: 400,
        x: 100,
        y: 100,
        bodyStyle: 'padding: 5px;',
        layout: 'border',
        items: [tabPanel]
    }).show();
});


tree-simple.js is to create a simple tree, extract the value of the node operation.
function createSimpleTree(){
    var treeStore;
    var treePanel;
    var toolbarPanel;

    store = Ext.create('Ext.data.TreeStore', {
        root: {
            expanded: true,
            text: " The root node ",
            children: [{
                text: "1-1",
                leaf: true
            }, {
                text: "1-2",
                expanded: true,
                children: [{
                    text: "2-1",
                    leaf: true
                }, {
                    text: "2-2",
                    leaf: true
                }]
            }, {
                text: "1-3",
                leaf: true
            }]
        }
    });
    // Tool Panel
    toolbarPanel = new Ext.panel.Panel({
        dockedItems: [{
            xtype: 'toolbar',
            items: [{
                text: ' Selected node value ',
                handler: function(){
                    var selectionMode = treePanel.getSelectionModel();
                    var modeType = selectionMode.getSelectionMode();//SINGLE, MULTI or SIMPLE
                    var selection = selectionMode.getSelection();// Gets the selected values
                    if (selection.length == 1) {
                        Ext.MessageBox.show({
                            title: ' Node actions ',
                            msg: " Value of the selected node :" + selection[0].data.text,
                            icon: Ext.MessageBox.INFO
                        });
                    }
                    else {
                        Ext.MessageBox.show({
                            title: ' Node actions ',
                            msg: ' The node is not selected ',
                            icon: Ext.MessageBox.INFO
                        });
                    }

                }
            }]
        }]
    });

    // Tree Panel
    treePanel = Ext.create('Ext.tree.Panel', {
        title: 'simple-tree',
        store: store,
        closable: true,
        rootVisible: true,// Display the root node
        dockedItems: [toolbarPanel],
        listeners: {
            itemclick: function(view, record, item, index, e){// In the select the event does not fire .
                Ext.MessageBox.show({
                    title: ' Node actions ',
                    msg: 'itemclick:index=' + index + ",text=" + record.data.text,
                    icon: Ext.MessageBox.INFO
                });
            }
        }
        //renderTo: 'simple-tree-div'//<div></div>
    });
    // Register event actions
    treePanel.getSelectionModel().on('select', function(selModel, record){
        Ext.MessageBox.show({
            title: ' Node actions ',
            msg: 'select:modeType=' + selModel.mode + ",text=" + record.data.text,
            icon: Ext.MessageBox.INFO
        });
    });
    return treePanel;
}


tree-check.js is a tree with a check box to extract the value of the selected tree:
function createCheckTree(){
    var treeStore;
    var treePanel;
    var toolbarPanel;

    store = Ext.create('Ext.data.TreeStore', {
        root: {
            checked: false,
            expanded: true,
            text: " The root node ",
            children: [{
                checked: false,
                text: "1-1",
                leaf: true
            }, {
                checked: false,
                text: "1-2",
                expanded: true,
                children: [{
                    checked: false,
                    text: "2-1",
                    leaf: true
                }, {
                    checked: false,
                    text: "2-2",
                    children: [{
                        checked: false,
                        text: "2-2-1",
                        leaf: true
                    }, {
                        checked: false,
                        text: "2-2-2",
                        leaf: true
                    }]
                }]
            }, {
                checked: false,
                text: "1-3",
                leaf: true
            }]
        }
    });

    toolbarPanel = new Ext.panel.Panel({
        dockedItems: [{
            xtype: 'toolbar',
            items: [{
                text: ' Selected node value ',
                handler: function(target, e){
                    var records = treePanel.getView().getChecked(), names = [];
                    Ext.Array.each(records, function(rec){
                        names.push(rec.get('text'));
                    });
                    if (records.length > 0) {
                        Ext.MessageBox.show({
                            title: 'Selected Nodes',
                            msg: names.join('<br />'),
                            icon: Ext.MessageBox.INFO
                        });
                    }
                    else {
                        Ext.MessageBox.show({
                            title: ' Node actions ',
                            msg: ' The node is not selected ',
                            icon: Ext.MessageBox.INFO
                        });
                    }

                }
            }]
        }]
    });

    treePanel = Ext.create('Ext.tree.Panel', {
        title: 'check-tree',
        store: store,
        animate: true,
        closable: true,
        rootVisible: true,
        dockedItems: [toolbarPanel],
        listeners: {
            itemclick: function(view, record, item, index, e){

            },
            checkchange: function(node, checked){
                org.forever.util.TreeUtil.updateCheckStatus(node, checked);
            }
        }
    });
    return treePanel;
}


猜你喜欢

转载自beckham-xiao.iteye.com/blog/2276094
今日推荐