extjs表格行拖动插件

简介: extjs表格行拖动插件
Ext.ux.GridDragDropRowOrder = Ext.extend(Ext.util.Observable, {
  copy : false,

  scrollable : false,

  constructor : function(config) {
    if (config)
      Ext.apply(this, config);

    this.addEvents({
          beforerowmove : true,
          afterrowmove : true,
          beforerowcopy : true,
          afterrowcopy : true
        });

    Ext.ux.GridDragDropRowOrder.superclass.constructor.call(this);
  },

  init : function(grid) {
    this.grid = grid;
    grid.enableDragDrop = true;

    grid.on({
          render : {
            fn : this.onGridRender,
            scope : this,
            single : true
          }
        });
  },

  onGridRender : function(grid) {
    var self = this;

    this.target = new Ext.dd.DropTarget(grid.getEl(), {
      ddGroup : grid.ddGroup || 'GridDD',
      grid : grid,
      gridDropTarget : this,

      notifyDrop : function(dd, e, data) {
        if (this.currentRowEl) {
          this.currentRowEl.removeClass('grid-row-insert-below');
          this.currentRowEl.removeClass('grid-row-insert-above');
        }
                
        var t = Ext.lib.Event.getTarget(e);
        var rindex = this.grid.getView().findRowIndex(t);
        
        if (rindex === false || rindex == data.rowIndex) {
          return false;
        }
        if (this.gridDropTarget.fireEvent(self.copy
                ? 'beforerowcopy'
                : 'beforerowmove', this.gridDropTarget,
            data.rowIndex, rindex, data.selections, 123) === false) {
          return false;
        }

        var ds = this.grid.getStore();

        var selections = new Array();
        var keys = ds.data.keys;
        for (var key in keys) {
          for (var i = 0; i < data.selections.length; i++) {
            if (keys[key] == data.selections[i].id) {
              if (rindex == key) {
                return false;
              }
              selections.push(data.selections[i]);
            }
          }
        }
        var targetRecord = this.grid.store.getAt(rindex);
        
        if (rindex > data.rowIndex && this.rowPosition < 0) {
          rindex--;
        }
        if (rindex < data.rowIndex && this.rowPosition > 0) {
          rindex++;
        }

        if (rindex > data.rowIndex && data.selections.length > 1) {
          rindex = rindex - (data.selections.length - 1);
        }

        if (rindex == data.rowIndex) {
          return false;
        }
        if (!self.copy) {
          for (var i = 0; i < data.selections.length; i++) {
            ds.remove(ds.getById(data.selections[i].id));
          }
        }

        for (var i = selections.length - 1; i >= 0; i--) {
          var insertIndex = rindex;
          ds.insert(insertIndex, selections[i]);
        }
        var sm = this.grid.getSelectionModel();
        if (sm) {
          sm.selectRecords(data.selections);
        }
        this.gridDropTarget.fireEvent(self.copy
                ? 'afterrowcopy'
                : 'afterrowmove', this.gridDropTarget,
            data.rowIndex, rindex, data.selections,this.grid.store);
        return true;
      },

      notifyOver : function(dd, e, data) {
        var t = Ext.lib.Event.getTarget(e);
        var rindex = this.grid.getView().findRowIndex(t);
        var ds = this.grid.getStore();
        var keys = ds.data.keys;
        for (var key in keys) {
          for (var i = 0; i < data.selections.length; i++) {
            if (keys[key] == data.selections[i].id) {
              if (rindex == key) {
                if (this.currentRowEl) {
                  this.currentRowEl
                      .removeClass('grid-row-insert-below');
                  this.currentRowEl
                      .removeClass('grid-row-insert-above');
                }
                return this.dropNotAllowed;
              }
            }
          }
        }
        if (rindex < 0 || rindex === false) {
          if(this.currentRowEl)
          this.currentRowEl.removeClass('grid-row-insert-above');
          return this.dropNotAllowed;
        }

        try {
          var currentRow = this.grid.getView().getRow(rindex);
          var resolvedRow = new Ext.Element(currentRow).getY()
              - this.grid.getView().scroller.dom.scrollTop;
          var rowHeight = currentRow.offsetHeight;
          this.rowPosition = e.getPageY() - resolvedRow
              - (rowHeight / 2);

          if (this.currentRowEl) {
            this.currentRowEl.removeClass('grid-row-insert-below');
            this.currentRowEl.removeClass('grid-row-insert-above');
          }

          if (this.rowPosition > 0) {
            this.currentRowEl = new Ext.Element(currentRow);
            this.currentRowEl.addClass('grid-row-insert-below');
          } else {
            if (rindex - 1 >= 0) {
              var previousRow = this.grid.getView().getRow(rindex
                  - 1);
              this.currentRowEl = new Ext.Element(previousRow);
              this.currentRowEl.addClass('grid-row-insert-below');
            } else {
              this.currentRowEl.addClass('grid-row-insert-above');
            }
          }
        } catch (err) {
          console.warn(err);
          rindex = false;
        }
        return (rindex === false)
            ? this.dropNotAllowed
            : this.dropAllowed;
      },
      notifyOut : function(dd, e, data) {
        if (this.currentRowEl) {
          this.currentRowEl.removeClass('grid-row-insert-above');
          this.currentRowEl.removeClass('grid-row-insert-below');
        }
      }
    });
    if (this.targetCfg) {
      Ext.apply(this.target, this.targetCfg);
    }
    if (this.scrollable) {
      Ext.dd.ScrollManager.register(grid.getView().getEditorParent());
      grid.on({
            beforedestroy : this.onBeforeDestroy,
            scope : this,
            single : true
          });
    }
  },
  getTarget : function() {
    return this.target;
  },
  getGrid : function() {
    return this.grid;
  },
  getCopy : function() {
    return this.copy ? true : false;
  },
  setCopy : function(b) {
    this.copy = b ? true : false;
  },
  onBeforeDestroy : function(grid) {
    Ext.dd.ScrollManager.unregister(grid.getView().getEditorParent());
  }
});
目录
相关文章
|
7月前
|
前端开发
Element常用组件—表格、表单、对话框和分页工具条
Element常用组件—表格、表单、对话框和分页工具条
114 0
|
XML JavaScript 小程序
使用jquery treetable 实现树形表格拖拽
这里记录一下使用jquery treetable时遇到的坑。 我这里的需求是做一个树形表格,并且可拖拽。 最后要实现的效果大概是这样的:(文末有实例)
53 0
06EasyUI 拖放- 基本的拖动和放置
06EasyUI 拖放- 基本的拖动和放置
35 0
|
数据可视化 JavaScript 前端开发
ElementUI实现表格内嵌进度条功能
ElementUI实现表格内嵌进度条功能
495 1
|
JavaScript 前端开发 Java
jquery实现动态表格项目(表格增加删除/全选/表格变色特效等功能)(附源码+javaWEB开发如果需要可以直接使用)
jquery实现动态表格项目(表格增加删除/全选/表格变色特效) 🍅 Java学习路线:搬砖工的Java学习路线 🍅 作者:程序员小王 🍅 程序员小王的博客:https://www.wolai.com/wnaghengjie/ahNwvAUPG2Hb1Sy7Z8waaF 🍅 扫描主页左侧二维码,加我微信 一起学习、一起进步 🍅 欢迎点赞 👍 收藏 ⭐留言 📝
360 0
jquery实现动态表格项目(表格增加删除/全选/表格变色特效等功能)(附源码+javaWEB开发如果需要可以直接使用)
|
移动开发 前端开发 信息无障碍
【BootStrap】栅格系统、表单样式与按钮样式-附有源码(3)
【BootStrap】栅格系统、表单样式与按钮样式-附有源码
229 0
【BootStrap】栅格系统、表单样式与按钮样式-附有源码(3)
|
前端开发
【BootStrap】栅格系统、表单样式与按钮样式-附有源码(2)
【BootStrap】栅格系统、表单样式与按钮样式-附有源码
218 0
【BootStrap】栅格系统、表单样式与按钮样式-附有源码(2)
|
前端开发 容器
【BootStrap】栅格系统、表单样式与按钮样式-附有源码(1)
【BootStrap】栅格系统、表单样式与按钮样式-附有源码
203 0
【BootStrap】栅格系统、表单样式与按钮样式-附有源码(1)
|
JavaScript
jQuery自定义美化下拉框
在线演示 本地下载
874 0