Codehiker Codehiker - 18 days ago 8
HTML Question

Mac clipboard data from Excel issue



$(function() {
quickOrder();
});

function quickOrder() {

$("#quick-order input[type='text']").val("");

var order = $(".quick-order");
if (!order.length) {
return;
}

var items = order.find(".items");
var tpl = items.prev(".item-template").html();
var button = order.find("button");

order.find(".show-more").unbind().on("click", function(e) {
e.preventDefault();
add(3);
typeahead();
});

order.on("paste", "input", function(e) {
var paste;
var rows;

if (window.clipboardData && window.clipboardData.getData) { // IE
paste = window.clipboardData.getData('Text');
rows = paste.split("\n");
alert("Ja")
} else if (e.originalEvent.clipboardData && e.originalEvent.clipboardData.getData) { // other browsers
paste = e.originalEvent.clipboardData.getData('text/plain');
rows = paste.split("\n");
console.log(rows)
}

for (var i in rows) {
rows[i] = rows[i].split("\t");
if (!rows[i][1]) {
rows[i][1] = "";
}
}

var current = $(this);
var qty = current.next("input");

var index = current.parents(".item").attr("id").replace("item-", "");

if (!isNaN(index)) {
index = parseInt(index);
} else {
index = 0;
}

var count = items.find(".item").length;
var overflow = count - rows.length - index;

if (overflow < 0) {
add(Math.abs(overflow));
}

for (var i in rows) {
var row = rows[i];
var num = row[0];
var qty = row[1];

var item = items.find("#item-" + (parseInt(i) + index));

(function() {
var n = item.find(".num");
var q = item.find(".qty");

var nn = num;
var qq = qty;

setTimeout(function() {
n.val(nn);
q.val(qq);
}, 0);
})();
}

order.find("input[type=text]").unbind("focus blur");
});

function add(num) {
var html = new Array(num + 1).join(tpl);
items.append(html);

var id = 0;
items.find(".item").each(function() {
var item = $(this);
item.attr("id", "item-" + id);
id++;
});

button.appendTo(items.find(".item").last());
}
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js">
</script>
<div id="quick-order" class="quick-order">
<div class="description">
<i class="fa fa-info-circle">
<div class="tt">Test</div>
</i>
</div>
<div class="item-template" style="display: none;">
<div class="item">
<input type="text" class="num" name="ProductCode[]" placeholder="Prod no.">
<input type="text" class="qty" name="ProductQuantity[]" placeholder="Qty">
</div>
</div>
<div class="items">
<div class="item" id="item-@i">
<input type="text" class="num" name="ProductCode[]" placeholder="Prod no.">
<input type="text" class="qty" name="ProductQuantity[]" placeholder="Qty">

<button class="quick-add-to-cart-button load-button">
uu
</button>

</div>

</div>
<a href="javascript:void(0);" class="show-more">

</a>

</div>





Here's the code I have for appending excel rows and adding more fields based on the clipboard amount.

You can try by copying two columns from Excel and paste it in. However, this does not seem to work on Mac. It works across all other browsers with windows.

Can anyone help me find out whats going wrong?

This is the output from a windows computer with the console.log(rows) above:

["1 2", "3 4", "5 6", "7 8", ""]


And in mac:

["1 2 3 4 5 6 7 8"]

Answer

Change the split line to

....split(/\r\n|\r|\n/g)

and see if it picks up on the carriage returns.