Skip to content

Instantly share code, notes, and snippets.

@Kalyse
Created May 11, 2011 08:23
Show Gist options
  • Save Kalyse/966106 to your computer and use it in GitHub Desktop.
Save Kalyse/966106 to your computer and use it in GitHub Desktop.
RowPositionChange
dojo.provide("discovery.grid.enhanced.plugins.RowPositionChange");
dojo.require("dojox.grid.enhanced._Plugin");
dojo.declare("discovery.grid.enhanced.plugins.RowPositionChange", dojox.grid.enhanced._Plugin, {
name: "rowPositionChange",
constructor: function(grid, args){
this.grid = grid;
this._mixinGrid();
var layout = this.grid.layout;
this.connect(layout, 'setStructure', dojo.hitch(layout, this._mixinLayout, this));
},
_mixinGrid: function(){
var g = this.grid;
},
_downButton: function(item,index,cell){
if(index != cell.grid.rowCount - 1) {
button = new dijit.form.Button({
baseClass: "negativeButton",
onClick:dojo.hitch(this,function(e){
console.log(e);
dojo.stopEvent(e);
}),
iconClass: "negativeBlock",
label: "Down"
});
dojo.connect( button, "onClick", this, function() {
cell.grid.store.setValue( cell.grid.getItem(index), "position", index + 1);
cell.grid.store.setValue( cell.grid.getItem(index + 1), "position", index);
cell.grid.setSortIndex(2,true);
});
return button;
} else {
return "";
}
},
_upButton: function(item,index,cell){
if(index != 0) {
button = new dijit.form.Button({
baseClass: "positiveButton",
onClick:dojo.hitch(this,function(e){
console.log(e);
dojo.stopEvent(e);
}),
iconClass: "plusBlock",
label: "Up"
});
dojo.connect( button, "onClick", this, function() {
cell.grid.store.setValue( cell.grid.getItem(index), "position", index - 1);
cell.grid.store.setValue( cell.grid.getItem(index -1), "position", index);
cell.grid.setSortIndex(2,true);
});
return button;
} else {
return "";
}
},
_setPosition: function(position,index,cell){
// If the position hasn't already been set, OR is isn't in the store. Then set it.
if( typeof cell.grid.getItem(index).position == "undefined") {
cell.grid.store.setValue( cell.grid.getItem(index), "position", index);
}
return cell.grid.getItem(index).position;
},
_mixinLayout: function(parent){
// All of this is executed in the scope of grid.layout
var defaultUpCellDef = { name: "Up" , width: 4, formatter: dojo.hitch(parent, "_upButton") };
var defaultDownCellDef = { name: "Down" , width: 4,formatter: dojo.hitch(parent, "_downButton")}
var defaultPositionCellDef = { hidden:true, field:"position", formatter: dojo.hitch(parent, "_setPosition"), name: "" , width: 5,editable: false,filterable: false,notselectable: true,}
var mixins = { field:"_item", editable: false, notselectable: true, filterable: false, navigatable: true, nosort: true};
dojo.forEach(this.structure, function(view){
var cells = view.cells;
if(cells && cells.length > 0){
var firstRow = cells[0];
var rowPositionCell = this.addCellDef(0, 0, defaultPositionCellDef);
rowPositionCell.index = 0;
firstRow.unshift(rowPositionCell);
this.cells.unshift(rowPositionCell);
rowPositionCellAdded = true;
var firstRow = cells[0];
selectDef = dojo.mixin(defaultDownCellDef, mixins );
var rowDownCell = this.addCellDef(0, 0, selectDef);
rowDownCell.index = 0;
firstRow.unshift(rowDownCell);
this.cells.unshift(rowDownCell);
rowDownCellAdded = true;
var firstRow = cells[0];
selectDef = dojo.mixin(defaultUpCellDef, mixins);
var rowUpCell = this.addCellDef(0, 0, selectDef);
rowUpCell.index = 0;
firstRow.unshift(rowUpCell);
this.cells.unshift(rowUpCell);
rowUpCellAdded = true;
}
},this);
}
});
dojox.grid.EnhancedGrid.registerPlugin(discovery.grid.enhanced.plugins.RowPositionChange,{"preInit": true});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment