演示链接-“ https://jsfiddle.net/OlegKi/ovq05x0c/6/”使用代码
.jqGrid("navButtonAdd", {
caption: "",
title: "Export to Excel(.XLSX)",
onClickButton: function () {
var filename = "jqGrid.xlsx",
data = $(this).jqGrid("getGridParam", "lastSelectedData"), i, item,
dataAsArray = [["Client", "Date", "Amount", "Tax", "Total",
"Closed", "Shipped via"]];
for (i = 0; i < data.length; i++) {
item = data[i];
dataAsArray.push([
item.name,
new Date(item.invdate),
item.amount, item.tax, item.total,
item.closed, item.ship_via]);
}
var ws_name = "SheetJS";
var wb = XLSX.utils.book_new(),
ws = XLSX.utils.aoa_to_sheet(dataAsArray);
XLSX.utils.book_append_sheet(wb, ws, ws_name);
XLSX.writeFile(wb, filename);
}
});
在此演示中,单击jqgrid select formatter列的导出按钮的导出值,而不是formatter select列的文本(通过“ Shipped via”)。我的要求是文本应在excel中导出。谁能帮助解决这个问题。
答案 0 :(得分:0)
通过使用formatter
属性,可以调用<span class='ui-jqgrid-cell-wrapper'>...</span>
函数并删除autoResizable: true
包装器来修复代码。修改后的代码看起来像
.jqGrid("navButtonAdd", {
caption: "",
title: "Export to Excel(.XLSX)",
onClickButton: function () {
var filename = "jqGrid.xlsx",
self = this,
data = $(this).jqGrid("getGridParam", "lastSelectedData"), i, item,
iColByName = $(this).jqGrid("getGridParam", "iColByName"),
dataAsArray = [["Client", "Date", "Amount", "Tax", "Total", "Closed", "Shipped via"]];
function getFormattedValue (item, cmName) {
var value = self.formatter(item.id, item[cmName], iColByName[cmName], item, "add", item),
ex = /^<span class='ui-jqgrid-cell-wrapper'>([^<]+?)<\/span>$/i.exec(value);
return Array.isArray(ex) && ex.length === 2 ? ex[1] :value;
}
for (i = 0; i < data.length; i++) {
item = data[i];
dataAsArray.push([
item.name,
getFormattedValue(item, "invdate"),
getFormattedValue(item, "amount"),
getFormattedValue(item, "tax"),
getFormattedValue(item, "total"),
item.closed,
getFormattedValue(item, "ship_via")]);
}
var ws_name = "SheetJS";
var wb = XLSX.utils.book_new(),
ws = XLSX.utils.aoa_to_sheet(dataAsArray);
XLSX.utils.book_append_sheet(wb, ws, ws_name);
XLSX.writeFile(wb, filename);
}
});