【问题标题】:Drag drop in ExtJS TreeGrid在 ExtJS TreeGrid 中拖放
【发布时间】:2013-12-27 15:25:25
【问题描述】:

我想在 ExtJS TreeGrid 中实现拖放节点。我能够填充 TreeGrid,但不知道如何在其中处理拖放。这是我用来填充 TreeGrid 的 ExtJS 代码:

Ext.require([
    'Ext.data.*',
    'Ext.grid.*',
    'Ext.tree.*'
]);

Ext.onReady(function () {
    Ext.define('Task', {
        extend: 'Ext.data.Model',
        fields: [
            { name: 'task', type: 'string' },
            { name: 'assignedto', type: 'string' }
        ]
    });

    var store = Ext.create('Ext.data.TreeStore', {
        model: 'Task',
        proxy: {
            type: 'ajax',
            url: 'treegrid.json'
        },
        folderSort: false
    });

    var tree = Ext.create('Ext.tree.Panel', {
        title: 'Core Team Projects',
        width: 500,
        height: 300,
        renderTo: Ext.getBody(),
        collapsible: true,
        useArrows: true,
        rootVisible: false,
        store: store,
        multiSelect: true,
        singleExpand: true,
        columns: [{
            xtype: 'treecolumn',
            text: 'Task',
            flex: 2,
            sortable: true,
            dataIndex: 'task'
        }, {
            text: 'Assigned To',
            flex: 1,
            dataIndex: 'assignedto',
            sortable: true
        }]
    });

以下是示例 JSON 数据:

{"text":".","children": [
    {
        task:'Task: R&D',       
        assignedto:'Reserved Team',
        iconCls:'task-folder',
        expanded: true,
        children:[{
            task:'Grid',
            duration:1.25,
            assignedto:'Gopal Kanjoliya',
            iconCls:'task-folder',
            children:[{
                task:'Create',
                duration:0.25,
                assignedto:'Gopal Kanjoliya',
                leaf:true,
                iconCls:'task'
            },{
                task:'Read',
                duration:.4,
                assignedto:'Gopal Kanjoliya',
                leaf:true,
                iconCls:'task'
            }]
        }, {
            task:'Tree',
            duration:12,
            assignedto:'Gopal Kanjoliya',
            iconCls:'task-folder',
            expanded: true,
            children:[{
                task:'Add Node',
                duration: 2.75,
                assignedto:'Gopal Kanjoliya',
                iconCls:'task-folder',
                children: [{
                    task: 'Delete',
                    duration: 1.25,
                    assignedto: 'Gopal Kanjoliya',
                    iconCls: 'task',
                    leaf: true
                }]
            },{
                task:'Form',
                duration:2.75,
                assignedto:'Gopal Kanjoliya',
                leaf:true,
                iconCls:'task'
            }]
        }]
    },{
        task:'Task: Implementation',
        duration:2,
        assignedto:'Development Team',
        iconCls:'task-folder',
        children:[{
            task: 'Forms',
            duration: 0.75,
            assignedto: 'Gopal Kanjoliya',
            iconCls: 'task-folder',
            children: [{
                task: 'Grids',
                duration: 0.25,
                assignedto: 'Gopal Kanjoliya',
                iconCls: 'task',
                leaf: true
            }]
        },{
            task: 'Components',
            duration: 3.75,
            assignedto: 'Shyam Dhanotiya',
            iconCls: 'task-folder',
            children: [{
                task: 'Panels',
                duration: 0.25,
                assignedto: 'Shyam Dhanotiya',
                iconCls: 'task',
                leaf: true
            }]
        },{
            task: 'Test',
            duration: 0.5,
            assignedto: 'Snehil Chaturvedi',
            iconCls: 'task-folder',
            children: [{
                task: 'Manual',
                duration: 0.25,
                assignedto: 'Snehil Chaturvedi',
                iconCls: 'task',
                leaf: true
            }]
        }]
    }
]}

【问题讨论】:

    标签: extjs extjs4


    【解决方案1】:

    ExtJS 有一个很好的插件: http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.tree.plugin.TreeViewDragDrop 阅读配置,它们非常重要且有用

    只需将其放入树形面板的代码中即可:

        viewConfig: {
            plugins: {
                ptype: 'treeviewdragdrop'
            }
        },
    

    另外,这是一个工作小提琴 我只是在商店里放了一些随机数据,不想格式化你的所有数据

    Ext.onReady(function () {
    
    Ext.define('Task', {
        extend: 'Ext.data.Model',
        fields: [
            { name: 'task', type: 'string' },
            { name: 'assignedto', type: 'string' }
        ]
    });
    
    var store = Ext.create('Ext.data.TreeStore', {
        model: 'Task',
        folderSort: false,
        root: {
            expanded: true,
            children: [
                {
                    task: 'Child 1',
                    assignedto: 'me',
                    leaf: true
                },
                {
                    task: 'Child 2',
                    assignedto: 'me',
                    expanded: true,
                    children: [
                        {
                            task: 'GrandChild 1',
                            assignedto: 'you',
                            leaf: true
                        }
                    ]
                },
                {
                    task: 'Child 3',
                    assignedto: 'me',
                    expanded: true,
                    children: [
                        {
                            task: 'GrandChild 2',
                            assignedto: 'you',
                            leaf: true
                        }
                    ]
                },
            ]
        }
    });
    
    var tree = Ext.create('Ext.tree.Panel', {
    
        viewConfig: {
            plugins: {
                ptype: 'treeviewdragdrop'
            }
        },
    
        title: 'Core Team Projects',
        width: 500,
        height: 300,
        renderTo: Ext.getBody(),
        collapsible: true,
        useArrows: true,
        rootVisible: false,
        store: store,
        multiSelect: true,
        singleExpand: true,
        columns: [{
            xtype: 'treecolumn',
            text: 'Task',
            flex: 2,
            sortable: true,
            dataIndex: 'task'
        }, {
            text: 'Assigned To',
            flex: 1,
            dataIndex: 'assignedto',
            sortable: true
        }]
    });
    });
    

    【讨论】:

    • 啊..我错过了。谢谢 Akori。
    • 很高兴这对您有所帮助^^
    猜你喜欢
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    • 2022-10-02
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    相关资源
    最近更新 更多