Move cell editing code into treegrid, add defaults for propdata, add working setposition() api

rel-1.0
Vitaliy Filippov 2016-03-09 23:52:32 +03:00
parent 9e96539be3
commit 140427f0d5
2 changed files with 78 additions and 34 deletions

View File

@ -1,6 +1,7 @@
/**
* Very simple and fast tree grid/table, compatible with dynamic loading and jQuery fixedHeaderTable
* License: MPL 2.0+, (c) Vitaliy Filippov 2016
* License: MPL 2.0+, (c) Vitaliy Filippov 2016+
* Version: 2016-03-09
*/
/**
@ -66,6 +67,81 @@ TreeGrid.prototype._setProps = function(el, props)
el[j] = props[j];
}
// Simple cell editing
TreeGrid.prototype.getNodeIndex = function(n)
{
var i;
for (i = 0, n = n.previousSibling; n = n.previousSibling; )
i += (n.nodeType != 3 ? 1 : 0);
return i;
}
// onStartEdit(node, colIndex, td), onStopEdit(node, colIndex, td)
TreeGrid.prototype.initCellEditing = function(onStartEdit, onStopEdit)
{
var self = this;
self.onStartCellEdit = onStartEdit;
self.onStopCellEdit = onStopEdit;
self.editedCells = [];
addListener(this.table, 'dblclick', function(evt)
{
var td = evt.target||evt.srcElement;
while (td.nodeName != 'TABLE' && td.nodeName != 'TD')
td = td.parentNode;
if (td.nodeName == 'TD' && td.parentNode._node && td.previousSibling && td.className != 'celleditor')
{
var params = self.onStartCellEdit ? self.onStartCellEdit(td.parentNode._node, self.getNodeIndex(td), td) : {};
if (params.abort)
return;
self.editedCells.push(td);
td.className += ' celleditor';
if (params.value === undefined)
params.value = td.innerHTML;
td.innerHTML = '<input type="text" value="'+td.innerHTML+'" />';
addListener(td.firstChild, 'keydown', function(evt)
{
if (evt.keyCode == 13 || evt.keyCode == 10)
self.stopCellEditing(td);
});
td.firstChild.focus();
}
});
addListener(document.body, 'click', function(evt)
{
if (!self.editedCells.length)
return;
var td = evt.target||evt.srcElement;
while (td && td.nodeName != 'TD')
td = td.parentNode;
if (td && /\bcelleditor\b/.exec(td.className))
return;
for (var i = self.editedCells.length-1; i >= 0; i--)
self.stopCellEditing(self.editedCells[i], 1);
self.editedCells = [];
});
}
TreeGrid.prototype.stopCellEditing = function(td, _int)
{
var params = this.onStopCellEdit ? this.onStopCellEdit(td.parentNode._node, this.getNodeIndex(td), td) : {};
td.innerHTML = params.html || td.firstChild.value;
td.className = '';
if (!_int)
{
for (var i = 0; i < this.editedCells.length; i++)
{
if (this.editedCells[i] == td)
{
this.editedCells.splice(i, 1);
break;
}
}
}
}
// Tree grid node class
function TreeGridNode(node, grid, level, insertBefore, startHidden)
{
this.grid = grid;

View File

@ -47,39 +47,7 @@ onDomReady(function()
};
// Cell editing example
$(TG.table).dblclick(function(evt)
{
var td = evt.target;
while (td.nodeName != 'TABLE' && td.nodeName != 'TD')
td = td.parentNode;
if (td.nodeName == 'TD' && td.parentNode._node && td.previousSibling && td.className != 'celleditor')
{
td.className = 'celleditor';
td.innerHTML = '<input type="text" value="'+td.innerHTML+'" />';
$(td.firstChild).on('keydown', function(evt)
{
if (evt.keyCode == 13 || evt.keyCode == 10)
{
td.innerHTML = td.firstChild.value;
td.className = '';
}
});
td.firstChild.focus();
}
});
$(document.body).on('click', function(evt)
{
var td = evt.target;
while (td && td.nodeName != 'TD')
td = td.parentNode;
if (td && td.className == 'celleditor')
return;
$(TG.table).find('td.celleditor').each(function(e)
{
this.innerHTML = this.firstChild.value;
this.className = '';
});
});
TG.initCellEditing();
// Cell selection by mouse drag example
var startDrag, dragDiv;