public
Created

YUI 2 DataTable keyboard navigation prototypes

  • Download Gist
datatable_nav.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
//--------------------------------------------------------------------------------------
// TODD'S Table Nav Stuff ...... BEGIN
//
// Functions allow cell navigation within a DataTable, first developed in pmgr/trans_grid.html
//
//
//
 
YAHOO.widget.DataTable.prototype.getFirstVisTdEl = function(cell) {
if ( !cell ) return null;
var cols = this.getColumnSet().keys;
for(var i=0; i<cols.length; i++)
if ( !cols[i].hidden ) break;
return this.getTdEl( {record:this.getRecord(cell), column:this.getColumn(i)} );
};
 
YAHOO.widget.DataTable.prototype.getLastVisTdEl = function(cell) {
if ( !cell ) return null;
var cols = this.getColumnSet().keys;
for(var i=cols.length-1; i>=0; i--)
if ( !cols[i].hidden ) break;
return this.getTdEl( {record:this.getRecord(cell), column:this.getColumn(i)} );
};
 
YAHOO.widget.DataTable.prototype.getTopVisTdEl = function(cell) {
if ( !cell ) return null;
return this.getTdEl( {record:this.getFirstTrEl(), column:this.getColumn(cell)} );
};
 
YAHOO.widget.DataTable.prototype.getBottomVisTdEl = function(cell) {
if ( !cell ) return null;
return this.getTdEl( {record:this.getLastTrEl(), column:this.getColumn(cell)} );
};
YAHOO.widget.DataTable.prototype.getLeftVisTdEl = function(cell) {
var newCell = this.getPreviousTdEl( cell );
if ( newCell === null ) return cell;
if ( this.getColumn(newCell).hidden ) {
if ( cell === this.getFirstVisTdEl(cell) )
return this.getLastVisTdEl(cell);
//
// there may be hidden / visible cells to the LEFT of us ...
//
var cols = this.getColumnSet().keys,
col_index = this.getColumn(newCell).getIndex();
var inext = null;
for(var i=col_index-1; i>=0; i--) {
if (!cols[i].hidden) {
inext = i;
break;
}
}
if ( i === -1 ) // should never get here
newCell = this.getLastVisTdEl(cell);
else {
newCell = this.getTdEl({record:this.getRecord(cell), column:this.getColumn(inext)})
}
} else {
// not hidden, make sure we are on same record index
if ( this.getRecordIndex(newCell) !== this.getRecordIndex(cell) )
newCell = this.getLastVisTdEl(cell);
}
return newCell;
};
 
YAHOO.widget.DataTable.prototype.getRightVisTdEl = function(cell) {
var newCell = this.getNextTdEl( cell );
if ( newCell === null ) return cell;
if ( this.getColumn(newCell).hidden ) {
if ( cell === this.getLastVisTdEl(cell) )
return this.getFirstVisTdEl(cell);
//
// there may be hidden / visible cells LEFT of us ...
//
var cols = this.getColumnSet().keys,
col_index = this.getColumn(newCell).getIndex();
var inext = null;
for(var i=col_index; i<cols.length; i++) {
if (!cols[i].hidden) {
inext = i;
break;
}
}
if ( i === -1 ) // should never get here
newCell = this.getFirstVisTdEl(cell);
else {
newCell = this.getTdEl({record:this.getRecord(cell), column:this.getColumn(inext)})
}
} else {
// not hidden, make sure we are on same record index
if ( this.getRecordIndex(newCell) !== this.getRecordIndex(cell) )
newCell = this.getFirstVisTdEl(cell);
}
return newCell;
};
 
 
YAHOO.widget.DataTable.prototype.getUpVisTdEl = function(cell) {
if ( cell === this.getTopVisTdEl(cell) )
return this.getBottomVisTdEl(cell);
else
return this.getAboveTdEl(cell);
};
 
YAHOO.widget.DataTable.prototype.getDownVisTdEl = function(cell) {
if ( cell === this.getBottomVisTdEl(cell) )
return this.getTopVisTdEl(cell);
else
return this.getBelowTdEl(cell);
};
 
 
/**
* method setVisKeyListeners
*
*
*/
YAHOO.widget.DataTable.prototype.setVisKeyListeners = function() {
var YEvent = YAHOO.util.Event,
YUtil = YAHOO.util,
elCont = this.getTbodyEl();
// elCont = this.getContainerEl();
YEvent.addListener( elCont, "keydown", function (evt) {
var KEYS = YUtil.KeyListener.KEY,
cur_cell = this.getSelectedTdEls()[0],
new_cell=null,
cell_edit=false;
// if ( evt.keyCode === KEYS.ENTER || (evt.ctrlKey && evt.keyCode>36 && evt.keyCode<41) ) {
if ( evt.keyCode === KEYS.ENTER || ( evt.keyCode>36 && evt.keyCode<41) ) {
switch (evt.keyCode) {
case KEYS.ENTER :
/*
// if this._oCellEditor
*/
var col = this.getColumn(cur_cell);
if ( col.editor ) {
this.showCellEditor( cur_cell );
cell_edit = true;
}
break;
case KEYS.UP : // up
new_cell = this.getUpVisTdEl(cur_cell);
break;
case KEYS.DOWN : // down
new_cell = this.getDownVisTdEl(cur_cell);
break;
case KEYS.LEFT: // left
new_cell = this.getLeftVisTdEl(cur_cell);
break;
case KEYS.RIGHT: // right
new_cell = this.getRightVisTdEl(cur_cell);
break;
}
// If a direction key (not ENTER), then update cell position
if ( !cell_edit ) {
new_cell = ( !new_cell ) ? cur_cell : new_cell;
this.unselectCell(cur_cell);
this.selectCell( new_cell );
cur_cell = new_cell;
this.fireEvent( "updateVisCellPosition", {event:evt, newCell:new_cell} );
// If scrolling, update scroll position
if ( this instanceof YAHOO.widget.ScrollingDataTable )
this.scrollTo( new_cell );
}
}
YEvent.stopEvent(evt);
}, this, true);
 
 
};
 
 
//
// TODD'S Table Nav Stuff ...... END
//--------------------------------------------------------------------------------------

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.