日期:2014-05-16  浏览次数:20386 次

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