Question

I have an ExtJS DataView with following template:

<ul>
    <tpl for=".">
        <li class="report-field" id="{listId}">
            {[this.getReorderLinks(values, xindex, xcount)]}
            <span class="field-title small" data-qtip="{displayName}">{displayName}</span>
            <i class="field-remove" title="' + deleteLabel + '"></i>
        </li>
    </tpl>
</ul>

Which makes each list of items look like this:

DataView of Items

Where user can click on different icons and perform related action, moving up/down in order and remove.

Note that these items are added to dataview using Drag and Drop, where there's another source dataview container from which I drag the items and add here. While these up/down arrows are working fine with reordering them, I want to reorder these items using drag-n-drop internally.

So, to make each individual item draggable and droppable in the same region, I used refresh event of dataview and registered DNDs there as follows:

listeners: {
    'refresh': function(dataview, eOpts) {
        var fieldsList = Ext.query('.added-field');

        // Iterate over the list and make each item draggable/droppable.
        Ext.each(fieldsList,function(field){
            var dragSource,
                fieldId;

            fieldId = field.id;

            dragSource = new Ext.dd.DragSource(field, {
                isTarget  : false
            });

            dropZone = new Ext.dd.DropTarget(field);

            dragSource.dragData = {
                record: me.viewStore.findRecord('listId', fieldId),
                fieldId: fieldId
            };

            dropZone.notifyDrop = function(source, event, params) {
                var targetRecord = me.viewStore.findRecord('listId', fieldId),
                    targetRecordIdx = me.viewStore.indexOf(targetRecord),
                    sourceRecordIdx = me.viewStore.indexOf(params.record);

                //Perform rearrangement in the store.
                me.viewStore.removeAt(sourceRecordIdx);
                me.viewStore.insert(targetRecordIdx, params.record);
                return true;
            };
        });
    }

But it is giving me weird behaviours; when I try to drag "Person Email" on top of "Person City", DataView gets broken to look like following:

Broken DataView

Also, I get Uncaught TypeError: Cannot read property 'internalId' of undefined when drop operation completes. I even tried to defer calls to removeAt() and insert() by certain ms, but still no luck, moreover, ExtJS has no documentation or working example available for Drag n Drop to Reorder DataView items.

Any help would be appreciated, thanks.

Was it helpful?

Solution

This reminds me a bad experience I had with drag drop in ExtJS 4. Anyway, you may try this plugin. Otherwise, here is something I have tried (scroll is not handled yet) (jsFiddle) :

new Ext.view.DragZone({
    view: view,
    ddGroup: 'test',
    dragText: 'test'
});

new Ext.view.DropZone({
    view: view,
    ddGroup: 'test',
    handleNodeDrop : function(data, record, position) {
        var view = this.view,
            store = view.getStore(),
            index, records, i, len;
        if (data.copy) {
            records = data.records;
            data.records = [];
            for (i = 0, len = records.length; i < len; i++) {
                data.records.push(records[i].copy(records[i].getId()));
            }
        } else {
            data.view.store.remove(data.records, data.view === view);
        }
        index = store.indexOf(record);
        if (position !== 'before') {
            index++;
        }
        store.insert(index, data.records);
        view.getSelectionModel().select(data.records);
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top