0
votes

I am new to Ext JS, I have tried 'drag and drop' and it is working fine when i drag rows. What I am trying now is dragging a row (a field from MySQL) from left grid and dropping it on right grid that gives data from database table as a column.

The code I am using now is,

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

    Ext.define('DataObject', {
        extend: 'Ext.data.Model',
        fields: ['Field']
    });

    Ext.onReady(function(){

            var firstGridStore = Ext.create('Ext.data.Store', {
            model: 'DataObject',

                autoLoad: true,
                pageSize: 4,
                proxy: {
                    type: 'ajax',
                    url : 'user.php',
                    reader: {
                    type: 'json',
                    root: 'users'            
                }
            }
        });

        // Column Model shortcut array
        var columns = [
            {text: "Fields", sortable: true, width: 50, dataIndex: 'Field'}
        ];

        var firstGrid = Ext.create('Ext.grid.Panel', {
            multiSelect: true,
            viewConfig: {
                plugins: {
                    ptype: 'gridviewdragdrop',
                    dragGroup: 'firstGridDDGroup',
                    dropGroup: 'secondGridDDGroup'
                },
                listeners: {
                    drop: function(node, data, dropRec, dropPosition) {
                        var dropOn = dropRec ? ' ' + dropPosition + ' ' + dropRec.get('name') : ' on empty view';
                        Ext.example.msg("Drag from right to left", 'Dropped ' + data.records[0].get('name') + dropOn);
                    }
                }
            },
            store            : firstGridStore,
            columns          : columns,
            stripeRows       : true,
            title            : 'First Grid',
            margins          : '0 2 0 0'
        });

        var secondGridStore = Ext.create('Ext.data.Store', {
            model: 'DataObject'
        });

        // create the destination Grid
        var secondGrid = Ext.create('Ext.grid.Panel', {
            viewConfig: {
                plugins: {
                    ptype: 'gridviewdragdrop',
                    dragGroup: 'secondGridDDGroup',
                    dropGroup: 'firstGridDDGroup'
                },
                listeners: {
                    drop: function(node, data, dropRec, dropPosition) {
                        var dropOn = dropRec ? ' ' + dropPosition + ' ' + dropRec.get('name') : ' on empty view';
                        Ext.example.msg("Drag from left to right", 'Dropped ' + data.records[0].get('name') + dropOn);
                    }
                }
            },
            store            : secondGridStore,
            columns            : columns,
            stripeRows       : true,
            title                   : 'Second Grid',
            margins             : '0 0 0 3'
        });

        //Simple 'border layout' panel to house both grids
        var displayPanel = Ext.create('Ext.Panel', {
            layout: 'fit',
            height       : 300,
            layout       : {
                type: 'hbox',
                align: 'stretch',
                padding: 5
            },
            renderTo     : 'panel',
            defaults     : { flex : 1 }, //auto stretch
            items        : [
                firstGrid,
                secondGrid
            ],
            dockedItems: {
                xtype: 'toolbar',
                dock: 'bottom',
                items: ['->', // Fill
                {
                    text: 'Reset both grids',
                    handler: function(){
                        //refresh source grid
                        firstGridStore.loadData(myData);

                        //purge destination grid
                        secondGridStore.removeAll();
                    }
                }]
            }
        });
    });

The field from left grid should be title for data (from database) on the right grid for my requirement.

Left grid will have grid data like:
Fields
id
name
data
After dragging data from left grid, right grid should have data like:
id
1
2
3
Note: Here only id is dragged.

Can someone help me out?

Thanks.

2

2 Answers

2
votes

you can work with "beforedrop" instead of "drop" event, but so you can set your own custom drop handler by assign a new function to dropFunction attribute, hopefully to be that a spot light to help you resolve your problem!! also see that http://docs.sencha.com/ext-js/4-1/#!/api/Ext.grid.plugin.DragDrop

0
votes

Couple of things to keep in mind..

  1. your Model drives the layout - you're defining 'DataObject' of one-field 'Field', to be filled by users.php json call/return, if you want all the fields from the json-return to be draggable, then you need to include all the fields in your model ie,

    Ext.define('DataObject', {
        extend: 'Ext.data.Model',
        fields: [
           'id',
           'name',
           'data'
        ]
    });
    
  2. field names must match between source and target for drag-drop - as long as your desired drag-fields are identically named(dataField definition) then your dragdrop proxy will pull all your desired items.

  3. Naturally, if you only want the title to show, then your display would be:

    items: [
       { dataIndex: 'id', hidden: true } ,
       { dataIndex: 'name', hidden: true },
       { text: 'title', dataIndex: 'data', width: xx }   // loose example
    ]
    

the dragdrop proxy will pull everything in the grid/form row, whether displayed or hidden..

Hope this helps