javascript – 使用拖放重新排列DataView的项目

我有一个带有以下模板的ExtJS DataView:

<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>

这使得每个项目列表看起来像这样:

用户可以点击不同的图标并执行相关操作,按顺序上下移动并删除.

请注意,这些项目使用拖放功能添加到数据视图中,其中有另一个源数据视图容器,我可以从中拖动项目并添加到此处.虽然这些向上/向下箭头在重新排序时工作正常,但我想在内部使用drag-n-drop重新排序这些项目.

因此,为了使每个单独的项目在同一区域中可拖动和可放置,我使用了数据视图的刷新事件和注册的DND,如下所示:

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;
            };
        });
    }

但它给了我奇怪的行为;当我尝试在“人员城市”之上拖动“人员电子邮件”时,DataView会被破坏,如下所示:

另外,我得到Uncaught TypeError:当drop操作完成时,无法读取未定义的属性’internalId’.我甚至尝试将调用removeAt()和insert()推迟一定的ms,但仍然没有运气,而且,ExtJS没有可用于Drag n Drop to Reorder DataView项目的文档或工作示例.

任何帮助将不胜感激,谢谢.

解决方法:

这让我想起a bad experience我在ExtJS 4中拖拽掉了.无论如何,你可以尝试这个plugin.否则,这是我尝试过的东西(滚动尚未处理)(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);
    }
});
上一篇:筛选DataTable中的空字符串


下一篇:sencha touch笔记(5)——DataView组件(1)