如何在拖动项目时在Extjs中创建占位符

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在拖动项目时在Extjs中创建占位符相关的知识,希望对你有一定的参考价值。

我想在将项目从一个面板拖动到另一个面板时在Extjs中创建占位符。项目是dataView记录。

Ext.application({
  name: 'Fiddle',

  launch: function () {
    simpsonsStore = Ext.create('Ext.data.Store', {
      storeId: 'simpsonsStore',
      fields: ['id', 'name', 'email'],
      data: [{
        name: 'Lisa',
        email: 'lisa@simpsons.com',
        id: 1
      }, {
        name: 'Bart',
        email: 'bart@simpsons.com',
        id: 2
      }, {
        name: 'Homer',
        email: 'homer@simpsons.com',
        id: 3
      }, {
        name: 'Marge',
        email: 'marge@simpsons.com',
        id: 4
      }]
    });

    Ext.create('Ext.panel.Panel', {
      scrollable: 'horizontal',
      bodyCls: 'scrollBarOn',
      region: 'center',
      width: '100%',
      height: 800,
      layout: 'hbox',
      renderTo: Ext.getBody(),
      items: [{
        xtype: 'panel',
        width: 200,
        height: 500,
        items: [{
          xtype: 'dataview',
          itemSelector: 'div.detail',
          tpl: ['<tpl for=".">',
            '<div class="detail">{name}</div>', '</tpl>'
          ],
          margin: '0 0 0 0',
          store: simpsonsStore,
          scrollable: 'vertical',
          focusable: false,
          setTemplate: function (template, itemSelector) {
            this.tpl = template;
            this.itemSelector = itemSelector;
            this.refresh();
          },
          listeners: {
            render: function (v) {
              var currentRef = this;
              new Ext.view.DragZone({ //Create Drag Zone
                view: currentRef,
                ddGroup: 'kanbanDataviewDrag',
                dragText: '1 row selected'
              });
              new Ext.view.DropZone({ //Create Drop Zone
                view: currentRef,
                ddGroup: 'kanbanDataviewDrag',
                handleNodeDrop: function (data, record,
                  position) { //When Handle Node Drop
                  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);
                  store.commitChanges();
                }
              });
            }
          }
        }]
      }, {
        xtype: 'panel',
        width: 200,
        height: 500,
        items: [{
          xtype: 'dataview',
          itemSelector: 'div.detail',
          tpl: ['<tpl for=".">',
            '<div class="detail">{name}</div>', '</tpl>'
          ],
          margin: '0 0 0 0',
          store: simpsonsStore,
          scrollable: 'vertical',
          focusable: false,
          setTemplate: function (template, itemSelector) {
            this.tpl = template;
            this.itemSelector = itemSelector;
            this.refresh();
          },
          listeners: {
            render: function (v) {
              var currentRef = this;
              new Ext.view.DragZone({ //Create Drag Zone
                view: currentRef,
                ddGroup: 'kanbanDataviewDrag',
                dragText: '1 row selected'
              });
              new Ext.view.DropZone({ //Create Drop Zone
                view: currentRef,
                ddGroup: 'kanbanDataviewDrag',
                handleNodeDrop: function (data, record,
                  position) { //When Handle Node Drop
                  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);
                  store.commitChanges();
                }
              });
            }
          }
        }]
      }]
    });
  }
});

小提琴:https://fiddle.sencha.com/#fiddle/1i2u

我想在你拖动任何项目时创建这样的占位符:http://www.bryntum.com/examples/taskboard-latest/examples/kitchensink/index.html#examples/basic

答案

我已经更新了您的小提琴示例并制定了可行的解决方案。主要问题是拖动时选择文本。通过这个解决方案,我们通过使用平面按钮摆脱了这一点。

变化

  • index.css已添加。
  • jetsonsStore定义。
  • 渲染Func创建以删除重复的代码。
  • tpl改变了。

我没有小提琴帐户,所以我将在这里分享代码。

index.css

.detail
{
     border: 0;
     background: none;
     box-shadow:none;
     border-radius: 0px;
}

app.js

var renderFunc = function(v) {
    var currentRef = this;
    new Ext.view.DragZone({ //Create Drag Zone
        view: currentRef,
        ddGroup: 'kanbanDataviewDrag',
        dragText: '1 row selected'
    });
    new Ext.view.DropZone({ //Create Drop Zone
        view: currentRef,
        ddGroup: 'kanbanDataviewDrag',
        handleNodeDrop: function(data, record, position) { //When Handle Node Drop
            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);
            store.commitChanges();
        }
    });
}

var itemTeplate = function(){
    return '<div><button class="detail">{name}</button></div>';
}

Ext.application({
    name: 'Fiddle',

    launch: function() {
        simpsonsStore = Ext.create('Ext.data.Store', {
            fields: ['id', 'name', 'email'],
            data: [
                {name: 'Lisa', email: 'lisa@simpsons.com', id: 101},
                {name: 'Bart', email: 'bart@simpsons.com', id: 102},
                {name: 'Homer', email: 'homer@simpsons.com', id: 103},
                {name: 'Marge', email: 'marge@simpsons.com', id: 104}
            ]
        });
        jetsonsStore = Ext.create('Ext.data.Store', {
            fields: ['id', 'name', 'email'],
            data: [
                {name: 'George', email: 'george@jetsons.com', id: 201},
                {name: 'Jane', email: 'jane@jetsons.com', id: 202},
                {name: 'Judy', email: 'judy@jetsons.com', id: 203},
                {name: 'Elroy', email: 'elroy@jetsons.com', id: 204}
            ]
        });

        Ext.create('Ext.panel.Panel', {
            scrollable: 'horizontal',
            bodyCls: 'scrollBarOn',
            region: 'center',
            width: '100%',
            height: 800,
            layout: 'hbox',
            renderTo: Ext.getBody(),
            items: [{
                xtype: 'panel',
                width: 200,
                height: 500,
                items: [
                    {
                        xtype: 'dataview',
                        itemSelector: 'div',
                        tpl: ['<tpl for=".">', itemTeplate(), '</tpl>'],
                        margin: '0 0 0 0',
                        store: simpsonsStore,
                        scrollable: 'vertical',
                        focusable: false,
                        mode: 'single',
                        setTemplate: function(template, itemSelector) {
                            this.tpl = template;
                            this.itemSelector = itemSelector;
                            this.refresh();
                        },
                        listeners: {
                            render: renderFunc
                        }
                    }
                ]
            },
                   {
                xtype: 'panel',
                width: 200,
                height: 500,
                items: [
                    {
                        xtype: 'dataview',
                        itemSelector: 'div',
                        tpl: ['<tpl for=".">', itemTeplate(), '</tpl>'],
                        margin: '0 0 0 0',
                        store: jetsonsStore,
                        scrollable: 'vertical',
                        mode: 'single',
                        focusable: false,
                        setTemplate: function(template, itemSelector) {
                            this.tpl = template;
                            this.itemSelector = itemSelector;
                            this.refresh();
                        },
                        listeners: {
                            render: renderFunc
                        }
                    }
                ]
            }]
        });
    }
});

以上是关于如何在拖动项目时在Extjs中创建占位符的主要内容,如果未能解决你的问题,请参考以下文章

在占位符字段中创建空间

如何在对话框编辑器中创建窗口占位符

如何给占位符在yii2 gridview中创建的下拉框

Jquery 可拖动占位符

使用钩子在 Reactjs 中创建一个动态占位符

Extjs 4.1 - 如何在 initComponent 函数中创建项目