c1.documentReady(function () {
var theGrid = wijmo.Control.getControl(
'#theGrid'
);
theGrid.rows.defaultSize = 40;
theGrid.formatItem.addHandler(function(s, e) {
if
(e.panel == s.cells) {
var col = s.columns[e.col],
item = s.rows[e.row].dataItem;
if
(item == currentEditItem) {
switch
(col.binding) {
case
'Buttons'
:
e.cell.innerHTML = document.getElementById(
'tplBtnEditMode'
).innerHTML;
e.cell.dataItem = item;
break
;
case
'Country'
:
case
'Sales'
:
case
'Expenses'
:
e.cell.innerHTML =
'<input class="form-control" '
+
'id="'
+ col.binding +
'" '
+
'value="'
+ s.getCellData(e.row, e.col,
true
) +
'"/>'
;
break
;
}
}
else
{
switch
(col.binding) {
case
'Buttons'
:
e.cell.innerHTML = document.getElementById(
'tplBtnViewMode'
).innerHTML;
e.cell.dataItem = item;
break
;
}
}
}
});
theGrid.addEventListener(theGrid.hostElement,
'click'
, function (e) {
var target = e.target;
if
(e.target.tagName !=
'BUTTON'
) {
target = e.target.parentNode;
}
if
(target !=
null
&& target.tagName ==
'BUTTON'
) {
var item = wijmo.closest(e.target,
'.wj-cell'
).dataItem;
switch
(target.id) {
case
'btnEdit'
:
editItem(item);
break
;
case
'btnDelete'
:
theGrid.collectionView.remove(item);
break
;
case
'btnOK'
:
commitEdit();
break
;
case
'btnCancel'
:
cancelEdit();
break
;
}
}
});
theGrid.scrollPositionChanged.addHandler(cancelEdit);
theGrid.lostFocus.addHandler(cancelEdit);
theGrid.resizingColumn.addHandler(cancelEdit);
theGrid.draggingColumn.addHandler(cancelEdit);
theGrid.sortingColumn.addHandler(cancelEdit);
var currentEditItem =
null
;
function editItem(item) {
cancelEdit();
currentEditItem = item;
theGrid.invalidate();
}
function commitEdit() {
if
(currentEditItem) {
theGrid.columns.forEach(function (col) {
var input = theGrid.hostElement.querySelector(
'#'
+ col.binding);
if
(input) {
var value = wijmo.changeType(input.value, col.dataType, col.format);
if
(wijmo.getType(value) == col.dataType) {
currentEditItem[col.binding] = value;
}
}
});
}
currentEditItem =
null
;
theGrid.invalidate();
}
function cancelEdit() {
if
(currentEditItem) {
currentEditItem =
null
;
theGrid.invalidate();
}
}
});