@layui/layui-vue
Version:
a component library for Vue 3 base on layui-vue
58 lines (57 loc) • 2.67 kB
JavaScript
import { inject as y, computed as k } from "vue";
import { useI18n as T } from "../../language/index.js";
import { LAY_TABLE_CONTEXT as E } from "../constant.js";
import { isArray as W, isValueArray as x } from "../../utils/arrayUtil.js";
const g = (o) => {
const { columnsState: i } = y(E), { t: u } = T(), s = k(() => !o.defaultToolbar || W(o.defaultToolbar) && o.defaultToolbar.length === 0 ? [] : x(o.defaultToolbar) ? o.defaultToolbar : ["filter", "export", "print"]);
return { t: u, showToolbars: s, toolbarStyle: (t) => {
if (x(s.value))
return { order: s.value.indexOf(t) };
}, exportData: () => {
let t = "";
for (const a of o.hierarchicalColumns) {
t += "<tr>";
for (const e of a)
e.ignoreExport || (e.type && e.type === "number" || !e.type) && (t += `<td colspan=${i.setColSpanValue(e)} rowspan=${i.setRowSpanValue(e)}>${e.title || ""}</td>`);
t += "</tr>";
}
const n = (a) => {
a.forEach((e, d) => {
t += "<tr>", o.lastLevelAllColumns.forEach((r, h) => {
var m;
if (!r.ignoreExport) {
if (r.type && r.type == "number")
t += `<td>${d + 1}</td>`;
else if (r.type == null) {
let c;
Object.keys(e).forEach((w) => {
r.key === w && (c = e);
});
const l = (m = o.spanMethod) == null ? void 0 : m.call(o, e, r, d, h), f = l ? l[0] : 1, p = l ? l[1] : 1;
f != 0 && p != 0 && (t += `<td colspan=${p} rowspan=${f} x:str>${c ? c[r.key] : ""}</td>`);
}
}
}), t += "</tr>", e.children && n(e.children);
});
};
n(o.tableDataSource);
const b = `<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
<x:Name>Sheet1</x:Name>
<x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>
</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
</head>
<body>
<table syle="table-layout: fixed;word-wrap: break-word; word-break: break-all;">${t}</table>
</body>
</html>`;
window.location.href = "data:application/vnd.ms-excel;base64," + window.btoa(unescape(encodeURIComponent(b)));
}, print: () => {
const t = o.tableRef.innerHTML, n = document.body.innerHTML;
document.body.innerHTML = t, window.print(), window.location.reload(), document.body.innerHTML = n;
} };
};
export {
g as useToolBar
};