The purpose of the JavaScript code below is to enable the user to copy multiple cells at once from excel (rows & columns) and then paste them into multiple text inputs, so every cell is copied to the following input. That code works well - http://jsfiddle.net/vqa8feL4/2/.
However I have two problems:
With table: http://jsfiddle.net/vqa8feL4/1/
HTML:
<table>
<thead>
<th>Name</th>
<th>Age</th>
<th>Description</th>
</thead>
<tbody>
<tr>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<textarea></textarea>
</td>
</tr>
<tr>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<textarea></textarea>
</td>
</tr>
</tbody>
</table>
JS:
$('input').bind('paste', null, function(e){
$this = $(this);
setTimeout(function(){
var columns = $this.val().split(/\s+/);
var i;
var input = $this
for(i=0; i < columns.length; i++){
input .val(columns[i]);
input = input.next();
}
}, 0);
});
After selecting the range of cells press Ctrl + C together to copy the range of cells. Again, select a range of cells where you want to paste it and press on to Ctrl + V together to paste it. This is the easiest way of copying and pasting multiple cells altogether.
Hold the Ctrl and Shift keys and press Up arrow and it will select all cells up to and including the copied cell. (It does not matter that the copied cell is included in the destination selection.) Hold Ctrl and press V to paste. (Shift/Insert also will paste.)
https://api.jquery.com/next/
jQuery.next() looks for the immediately following sibling of each element
Since you've added the inputs to a td there are most likely no siblings.
You would need to get the current input then traverse up to the td then goto the next td and find the input contained.
input = input.closest('td').next('td').find('input');
However then you will run into trouble because if you're at the last td you need to traverse to the next tr (row) in the table.
here is a fiddle that might help you.
$('input,textarea').bind('paste', function (e) {
var $start = $(this);
var source
//check for access to clipboard from window or event
if (window.clipboardData !== undefined) {
source = window.clipboardData
} else {
source = e.originalEvent.clipboardData;
}
var data = source.getData("Text");
if (data.length > 0) {
if (data.indexOf("\t") > -1) {
var columns = data.split("\n");
$.each(columns, function () {
var values = this.split("\t");
$.each(values, function () {
$start.val(this);
if ($start.closest('td').next('td').find('input,textarea')[0] != undefined || $start.closest('td').next('td').find('textarea')[0] != undefined) {
$start = $start.closest('td').next('td').find('input,textarea');
}
else
{
return false;
}
});
$start = $start.closest('td').parent().next('tr').children('td:first').find('input,textarea');
});
e.preventDefault();
}
}
});
Forgot the link to the fiddle:
http://jsfiddle.net/SeanWessell/cav8h5d1/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With