2
« on: April 22, 2014, 04:36:47 am »
I don't know if it's appropriate to share code here, but I've created something for my use which does just that. The code below allows a user to paste a bunch of selected Excel cells into a ParamQuery grid. This is specific to my purposes, but it should be enough to get you going. This seems to work properly in IE8+, FF, Chrome. The relevant part starts at "$inp.on('paste', function (event) {", I provided the rest for context.
obj.editor = {
type: function (ui) {
var $cell = ui.$cell,
rowData = ui.rowData,
dataIndx = ui.dataIndx,
cls = ui.cls,
dc = $.trim(rowData[dataIndx]);
$cell.css('padding', '0');
var $inp = $('<input class="pq-grid-editor-default ' + cls + '" type="text" style="outline: none; width: 100%; height: 100%;" tabindx="0" value="' + dc + '"/>').appendTo($cell);
$inp.blur(function (event) {
grid.Container.pqGrid('saveEditCell');
});
$inp.on('paste', function (event) {
var data;
if (event.originalEvent && event.originalEvent.clipboardData && event.originalEvent.clipboardData.getData) {
data = event.originalEvent.clipboardData.getData('Text');
} else if (window.clipboardData && window.clipboardData.getData) {
data = window.clipboardData.getData('Text');
} else if (event.clipboardData && event.clipboardData.getData) {
data = event.clipboardData.getData('text/plain');
}
if (typeof data != "undefined") {
if (data.indexOf(String.fromCharCode(9)) == -1 && data.indexOf('\n') == -1)
return;
var cell = grid.Container.pqGrid("selection", { type: 'cell', method: 'getSelection' })[0];
var colIdx = grid.Container.pqGrid("getColIndx", { dataIndx: cell.dataIndx });
var rowIndx = cell.rowIndx;
var colModel = grid.Container.pqGrid("getColModel");
var pRows = data.split('\n');
for (i = 0; i < pRows.length; i++) {
pRows = pRows.split(String.fromCharCode(9));
}
for (i = 0; i < pRows.length; i++) {
if (grid.Container.pqGrid("hasClass", { rowIndx: rowIndx + i, cls: 'gridRowDivider' }) || grid.Container.pqGrid("hasClass", { rowIndx: rowIndx + i, cls: 'gridRowCalculated' }))
continue;
var row = grid.Container.pqGrid("getRowData", { rowIndx: rowIndx + i });
if (typeof row == "undefined")
break;
for (j = 0; j < pRows.length; j++) {
var value = parseInt($.trim(pRows[j]));
if (isNaN(value))
value = null;
var column = colModel[colIdx + j];
if (typeof column == "undefined")
break;
if (pRows[j].length > 0) {
SetValue(grid, column.dataIndx, row.rowId, value, true);
if (j == 0 && i == 0)
$(this).text(value);
}
}
}
event.stopPropagation();
event.preventDefault();
}
});
$inp[0].select();
},
getData: function (ui) {
var $cell = ui.$cell;
var value = $(ui.$cell.children()[0]).val();
return value;
}
};