| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
- var _xeUtils = _interopRequireDefault(require("xe-utils"));
- var _ui = require("../../../ui");
- var _util = require("../../src/util");
- var _utils = require("../../../ui/src/utils");
- var _dom = require("../../../ui/src/dom");
- var _util2 = require("./util");
- var _log = require("../../../ui/src/log");
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
- const {
- getI18n,
- renderer
- } = _ui.VxeUI;
- let htmlCellElem;
- const csvBOM = '\ufeff';
- const enterSymbol = '\r\n';
- function hasTreeChildren($xeTable, row) {
- const treeOpts = $xeTable.computeTreeOpts;
- const childrenField = treeOpts.children || treeOpts.childrenField;
- return row[childrenField] && row[childrenField].length;
- }
- function getSeq($xeTable, cellValue, row, $rowIndex, column, $columnIndex) {
- const seqOpts = $xeTable.computeSeqOpts;
- const seqMethod = seqOpts.seqMethod || column.seqMethod;
- if (seqMethod) {
- return seqMethod({
- $table: $xeTable,
- row,
- rowIndex: $xeTable.getRowIndex(row),
- $rowIndex,
- column,
- columnIndex: $xeTable.getColumnIndex(column),
- $columnIndex
- });
- }
- return cellValue;
- }
- function defaultFilterExportColumn(column) {
- return !!column.field || ['seq', 'checkbox', 'radio'].indexOf(column.type || '') === -1;
- }
- function toTableBorder(border) {
- if (border === true) {
- return 'full';
- }
- if (border) {
- return border;
- }
- return 'default';
- }
- function toBooleanValue(cellValue) {
- return _xeUtils.default.isBoolean(cellValue) ? cellValue ? 'TRUE' : 'FALSE' : cellValue;
- }
- const toStringValue = cellValue => {
- return (0, _utils.eqEmptyValue)(cellValue) ? '' : `${cellValue}`;
- };
- function getBodyLabelData($xeTable, opts, columns, datas) {
- const props = $xeTable;
- const {
- isAllExpand,
- mode
- } = opts;
- const {
- treeConfig
- } = props;
- const radioOpts = $xeTable.computeRadioOpts;
- const checkboxOpts = $xeTable.computeCheckboxOpts;
- const treeOpts = $xeTable.computeTreeOpts;
- const columnOpts = $xeTable.computeColumnOpts;
- if (!htmlCellElem) {
- htmlCellElem = document.createElement('div');
- }
- if (treeConfig) {
- const childrenField = treeOpts.children || treeOpts.childrenField;
- // 如果是树表格只允许导出数据源
- const rest = [];
- const expandMaps = {};
- const useMaps = {};
- const {
- handleGetRowId
- } = (0, _util.createHandleGetRowId)($xeTable);
- _xeUtils.default.eachTree(datas, (item, $rowIndex, items, path, parent, nodes) => {
- const row = item._row || item;
- const rowid = handleGetRowId(row);
- if (useMaps[rowid]) {
- return;
- }
- const parentRow = parent && parent._row ? parent._row : parent;
- const pRowid = parentRow ? handleGetRowId(parentRow) : '';
- if (isAllExpand || !parentRow || expandMaps[pRowid] && $xeTable.isTreeExpandByRow(parentRow)) {
- const hasRowChild = hasTreeChildren($xeTable, row);
- const item = {
- _row: row,
- _level: nodes.length - 1,
- _hasChild: hasRowChild,
- _expand: hasRowChild && $xeTable.isTreeExpandByRow(row)
- };
- columns.forEach((column, $columnIndex) => {
- let cellValue = '';
- const renderOpts = column.editRender || column.cellRender;
- let bodyExportMethod = column.exportMethod || columnOpts.exportMethod;
- if (!bodyExportMethod && renderOpts && renderOpts.name) {
- const compConf = renderer.get(renderOpts.name);
- if (compConf) {
- bodyExportMethod = compConf.tableExportMethod || compConf.exportMethod;
- }
- }
- if (!bodyExportMethod) {
- bodyExportMethod = columnOpts.exportMethod;
- }
- if (bodyExportMethod) {
- cellValue = bodyExportMethod({
- $table: $xeTable,
- row,
- column,
- options: opts
- });
- } else {
- switch (column.type) {
- case 'seq':
- {
- const seqVal = path.map((num, i) => i % 2 === 0 ? Number(num) + 1 : '.').join('');
- cellValue = mode === 'all' ? seqVal : getSeq($xeTable, seqVal, row, $rowIndex, column, $columnIndex);
- break;
- }
- case 'checkbox':
- cellValue = toBooleanValue($xeTable.isCheckedByCheckboxRow(row));
- item._checkboxLabel = checkboxOpts.labelField ? _xeUtils.default.get(row, checkboxOpts.labelField) : '';
- item._checkboxDisabled = checkboxOpts.checkMethod && !checkboxOpts.checkMethod({
- $table: $xeTable,
- row
- });
- break;
- case 'radio':
- cellValue = toBooleanValue($xeTable.isCheckedByRadioRow(row));
- item._radioLabel = radioOpts.labelField ? _xeUtils.default.get(row, radioOpts.labelField) : '';
- item._radioDisabled = radioOpts.checkMethod && !radioOpts.checkMethod({
- $table: $xeTable,
- row
- });
- break;
- default:
- if (opts.original) {
- cellValue = (0, _util.getCellValue)(row, column);
- } else {
- cellValue = $xeTable.getCellLabel(row, column);
- if (column.type === 'html') {
- htmlCellElem.innerHTML = cellValue;
- cellValue = htmlCellElem.innerText.trim();
- } else {
- const cell = $xeTable.getCellElement(row, column);
- if (cell && !(0, _dom.hasClass)(cell, 'is--progress')) {
- cellValue = cell.innerText.trim();
- }
- }
- }
- }
- }
- item[column.id] = toStringValue(cellValue);
- });
- useMaps[rowid] = true;
- if (pRowid) {
- expandMaps[pRowid] = true;
- }
- rest.push(Object.assign(item, row));
- }
- }, {
- children: childrenField
- });
- return rest;
- }
- return datas.map((row, $rowIndex) => {
- const item = {
- _row: row
- };
- columns.forEach((column, $columnIndex) => {
- let cellValue = '';
- const renderOpts = column.editRender || column.cellRender;
- let bodyExportMethod = column.exportMethod || columnOpts.exportMethod;
- if (!bodyExportMethod && renderOpts && renderOpts.name) {
- const compConf = renderer.get(renderOpts.name);
- if (compConf) {
- bodyExportMethod = compConf.tableExportMethod || compConf.exportMethod;
- }
- }
- if (bodyExportMethod) {
- cellValue = bodyExportMethod({
- $table: $xeTable,
- row,
- column,
- options: opts
- });
- } else {
- switch (column.type) {
- case 'seq':
- {
- const seqValue = $rowIndex + 1;
- cellValue = mode === 'all' ? seqValue : getSeq($xeTable, seqValue, row, $rowIndex, column, $columnIndex);
- break;
- }
- case 'checkbox':
- cellValue = toBooleanValue($xeTable.isCheckedByCheckboxRow(row));
- item._checkboxLabel = checkboxOpts.labelField ? _xeUtils.default.get(row, checkboxOpts.labelField) : '';
- item._checkboxDisabled = checkboxOpts.checkMethod && !checkboxOpts.checkMethod({
- $table: $xeTable,
- row
- });
- break;
- case 'radio':
- cellValue = toBooleanValue($xeTable.isCheckedByRadioRow(row));
- item._radioLabel = radioOpts.labelField ? _xeUtils.default.get(row, radioOpts.labelField) : '';
- item._radioDisabled = radioOpts.checkMethod && !radioOpts.checkMethod({
- $table: $xeTable,
- row
- });
- break;
- default:
- if (opts.original) {
- cellValue = (0, _util.getCellValue)(row, column);
- } else {
- cellValue = $xeTable.getCellLabel(row, column);
- if (column.type === 'html') {
- htmlCellElem.innerHTML = cellValue;
- cellValue = htmlCellElem.innerText.trim();
- } else {
- const cell = $xeTable.getCellElement(row, column);
- if (cell && !(0, _dom.hasClass)(cell, 'is--progress')) {
- cellValue = cell.innerText.trim();
- }
- }
- }
- }
- }
- item[column.id] = toStringValue(cellValue);
- });
- return item;
- });
- }
- function getExportData($xeTable, opts) {
- const $xeGrid = $xeTable.$xeGrid;
- const $xeGantt = $xeTable.$xeGantt;
- const {
- columns,
- dataFilterMethod
- } = opts;
- let datas = opts.data;
- if (dataFilterMethod) {
- datas = datas.filter((row, index) => dataFilterMethod({
- $table: $xeTable,
- $grid: $xeGrid,
- $gantt: $xeGantt,
- row,
- $rowIndex: index
- }));
- }
- return getBodyLabelData($xeTable, opts, columns, datas);
- }
- function getBooleanValue(cellValue) {
- return cellValue === 'TRUE' || cellValue === 'true' || cellValue === true;
- }
- function getHeaderTitle($xeTable, opts, column) {
- const columnOpts = $xeTable.computeColumnOpts;
- const headExportMethod = column.headerExportMethod || columnOpts.headerExportMethod;
- return headExportMethod ? headExportMethod({
- column,
- options: opts,
- $table: $xeTable
- }) : (opts.isTitle ? column.getTitle() : column.field) || '';
- }
- function getFooterCellValue($xeTable, opts, row, column) {
- const columnOpts = $xeTable.computeColumnOpts;
- const renderOpts = column.editRender || column.cellRender;
- let footLabelMethod = column.footerExportMethod;
- if (!footLabelMethod && renderOpts && renderOpts.name) {
- const compConf = renderer.get(renderOpts.name);
- if (compConf) {
- footLabelMethod = compConf.tableFooterExportMethod || compConf.footerExportMethod || compConf.footerCellExportMethod;
- }
- }
- if (!footLabelMethod) {
- footLabelMethod = columnOpts.footerExportMethod;
- }
- const _columnIndex = $xeTable.getVTColumnIndex(column);
- if (footLabelMethod) {
- return footLabelMethod({
- $table: $xeTable,
- items: row,
- itemIndex: _columnIndex,
- row,
- _columnIndex,
- column,
- options: opts
- });
- }
- // 兼容老模式
- if (_xeUtils.default.isArray(row)) {
- return _xeUtils.default.toValueString(row[_columnIndex]);
- }
- return _xeUtils.default.get(row, column.field);
- }
- function getFooterData($xeTable, opts, footerTableData) {
- const $xeGrid = $xeTable.$xeGrid;
- const $xeGantt = $xeTable.$xeGantt;
- const {
- footerFilterMethod
- } = opts;
- return footerFilterMethod ? footerTableData.filter((items, index) => footerFilterMethod({
- $table: $xeTable,
- $grid: $xeGrid,
- $gantt: $xeGantt,
- items,
- $rowIndex: index
- })) : footerTableData;
- }
- function getCsvCellTypeLabel(column, cellValue) {
- if (cellValue) {
- if (column.type === 'seq') {
- return `\t${cellValue}`;
- }
- switch (column.cellType) {
- case 'string':
- if (!isNaN(cellValue)) {
- return `\t${cellValue}`;
- }
- break;
- case 'number':
- break;
- default:
- if (cellValue.length >= 12 && !isNaN(cellValue)) {
- return `\t${cellValue}`;
- }
- break;
- }
- }
- return cellValue;
- }
- function toTxtCellLabel(val) {
- if (/[",\s\n]/.test(val)) {
- return `"${val.replace(/"/g, '""')}"`;
- }
- return val;
- }
- function toCsv($xeTable, opts, columns, datas) {
- const reactData = $xeTable;
- let content = csvBOM;
- if (opts.isHeader) {
- content += columns.map(column => toTxtCellLabel(getHeaderTitle($xeTable, opts, column))).join(',') + enterSymbol;
- }
- datas.forEach(row => {
- content += columns.map(column => toTxtCellLabel(getCsvCellTypeLabel(column, row[column.id]))).join(',') + enterSymbol;
- });
- if (opts.isFooter) {
- const {
- footerTableData
- } = reactData;
- const footers = getFooterData($xeTable, opts, footerTableData);
- footers.forEach(row => {
- content += columns.map(column => toTxtCellLabel(getFooterCellValue($xeTable, opts, row, column))).join(',') + enterSymbol;
- });
- }
- return content;
- }
- function toTxt($xeTable, opts, columns, datas) {
- const reactData = $xeTable;
- let content = '';
- if (opts.isHeader) {
- content += columns.map(column => toTxtCellLabel(getHeaderTitle($xeTable, opts, column))).join('\t') + enterSymbol;
- }
- datas.forEach(row => {
- content += columns.map(column => toTxtCellLabel(row[column.id])).join('\t') + enterSymbol;
- });
- if (opts.isFooter) {
- const {
- footerTableData
- } = reactData;
- const footers = getFooterData($xeTable, opts, footerTableData);
- footers.forEach(row => {
- content += columns.map(column => toTxtCellLabel(getFooterCellValue($xeTable, opts, row, column))).join('\t') + enterSymbol;
- });
- }
- return content;
- }
- function hasEllipsis($xeTable, column, property, allColumnOverflow) {
- const reactData = $xeTable;
- const columnOverflow = column[property];
- const headOverflow = _xeUtils.default.isUndefined(columnOverflow) || _xeUtils.default.isNull(columnOverflow) ? allColumnOverflow : columnOverflow;
- const showEllipsis = headOverflow === 'ellipsis';
- const showTitle = headOverflow === 'title';
- const showTooltip = headOverflow === true || headOverflow === 'tooltip';
- let isEllipsis = showTitle || showTooltip || showEllipsis;
- // 虚拟滚动不支持动态高度
- const {
- scrollXLoad,
- scrollYLoad
- } = reactData;
- if ((scrollXLoad || scrollYLoad) && !isEllipsis) {
- isEllipsis = true;
- }
- return isEllipsis;
- }
- function toHtml($xeTable, opts, columns, datas) {
- const props = $xeTable;
- const reactData = $xeTable;
- const internalData = $xeTable;
- const {
- id,
- border,
- treeConfig,
- headerAlign: allHeaderAlign,
- align: allAlign,
- footerAlign: allFooterAlign,
- showOverflow: allColumnOverflow,
- showHeaderOverflow: allColumnHeaderOverflow
- } = props;
- const {
- isAllSelected,
- isIndeterminate
- } = reactData;
- const {
- mergeBodyCellMaps
- } = internalData;
- const treeOpts = $xeTable.computeTreeOpts;
- const {
- print: isPrint,
- isHeader,
- isFooter,
- isColgroup,
- isMerge,
- colgroups,
- original
- } = opts;
- const allCls = 'check-all';
- const clss = ['vxe-table', `border--${toTableBorder(border)}`, isPrint ? 'is--print' : '', isHeader ? 'is--header' : ''].filter(cls => cls);
- const tables = [`<table class="${clss.join(' ')}" border="0" cellspacing="0" cellpadding="0">`, `<colgroup>${columns.map(column => `<col style="width:${column.renderWidth}px">`).join('')}</colgroup>`];
- if (isHeader) {
- tables.push('<thead>');
- if (isColgroup && !original) {
- colgroups.forEach(cols => {
- tables.push(`<tr>${cols.map(column => {
- const headAlign = column.headerAlign || column.align || allHeaderAlign || allAlign;
- const classNames = hasEllipsis($xeTable, column, 'showHeaderOverflow', allColumnHeaderOverflow) ? ['col--ellipsis'] : [];
- const cellTitle = getHeaderTitle($xeTable, opts, column);
- let childWidth = 0;
- let countChild = 0;
- _xeUtils.default.eachTree([column], item => {
- if (!item.childNodes || !column.childNodes.length) {
- countChild++;
- }
- childWidth += item.renderWidth;
- }, {
- children: 'childNodes'
- });
- const cellWidth = childWidth - countChild;
- if (headAlign) {
- classNames.push(`col--${headAlign}`);
- }
- if (column.type === 'checkbox') {
- return `<th class="${classNames.join(' ')}" colspan="${column._colSpan}" rowspan="${column._rowSpan}"><div ${isPrint ? '' : `style="width: ${cellWidth}px"`}><input type="checkbox" class="${allCls}" ${isAllSelected ? 'checked' : ''}><span>${cellTitle}</span></div></th>`;
- }
- return `<th class="${classNames.join(' ')}" colspan="${column._colSpan}" rowspan="${column._rowSpan}" title="${cellTitle}"><div ${isPrint ? '' : `style="width: ${cellWidth}px"`}><span>${(0, _utils.formatText)(cellTitle, true)}</span></div></th>`;
- }).join('')}</tr>`);
- });
- } else {
- tables.push(`<tr>${columns.map(column => {
- const headAlign = column.headerAlign || column.align || allHeaderAlign || allAlign;
- const classNames = hasEllipsis($xeTable, column, 'showHeaderOverflow', allColumnHeaderOverflow) ? ['col--ellipsis'] : [];
- const cellTitle = getHeaderTitle($xeTable, opts, column);
- if (headAlign) {
- classNames.push(`col--${headAlign}`);
- }
- if (column.type === 'checkbox') {
- return `<th class="${classNames.join(' ')}"><div ${isPrint ? '' : `style="width: ${column.renderWidth}px"`}><input type="checkbox" class="${allCls}" ${isAllSelected ? 'checked' : ''}><span>${cellTitle}</span></div></th>`;
- }
- return `<th class="${classNames.join(' ')}" title="${cellTitle}"><div ${isPrint ? '' : `style="width: ${column.renderWidth}px"`}><span>${(0, _utils.formatText)(cellTitle, true)}</span></div></th>`;
- }).join('')}</tr>`);
- }
- tables.push('</thead>');
- }
- if (datas.length) {
- tables.push('<tbody>');
- if (treeConfig) {
- datas.forEach(item => {
- tables.push('<tr>' + columns.map(column => {
- const cellAlign = column.align || allAlign;
- const classNames = hasEllipsis($xeTable, column, 'showOverflow', allColumnOverflow) ? ['col--ellipsis'] : [];
- const cellValue = item[column.id];
- if (cellAlign) {
- classNames.push(`col--${cellAlign}`);
- }
- if (column.treeNode) {
- let treeIcon = '';
- if (item._hasChild) {
- treeIcon = `<i class="${item._expand ? 'vxe-table--tree-fold-icon' : 'vxe-table--tree-unfold-icon'}"></i>`;
- }
- classNames.push('vxe-table--tree-node');
- if (column.type === 'radio') {
- return `<td class="${classNames.join(' ')}" title="${cellValue}"><div ${isPrint ? '' : `style="width: ${column.renderWidth}px"`}><div class="vxe-table--tree-node-wrapper" style="padding-left: ${item._level * treeOpts.indent}px"><div class="vxe-table--tree-icon-wrapper">${treeIcon}</div><div class="vxe-table--tree-cell"><input type="radio" name="radio_${id}" ${item._radioDisabled ? 'disabled ' : ''}${getBooleanValue(cellValue) ? 'checked' : ''}><span>${item._radioLabel}</span></div></div></div></td>`;
- } else if (column.type === 'checkbox') {
- return `<td class="${classNames.join(' ')}" title="${cellValue}"><div ${isPrint ? '' : `style="width: ${column.renderWidth}px"`}><div class="vxe-table--tree-node-wrapper" style="padding-left: ${item._level * treeOpts.indent}px"><div class="vxe-table--tree-icon-wrapper">${treeIcon}</div><div class="vxe-table--tree-cell"><input type="checkbox" ${item._checkboxDisabled ? 'disabled ' : ''}${getBooleanValue(cellValue) ? 'checked' : ''}><span>${item._checkboxLabel}</span></div></div></div></td>`;
- }
- return `<td class="${classNames.join(' ')}" title="${cellValue}"><div ${isPrint ? '' : `style="width: ${column.renderWidth}px"`}><div class="vxe-table--tree-node-wrapper" style="padding-left: ${item._level * treeOpts.indent}px"><div class="vxe-table--tree-icon-wrapper">${treeIcon}</div><div class="vxe-table--tree-cell">${cellValue}</div></div></div></td>`;
- }
- if (column.type === 'radio') {
- return `<td class="${classNames.join(' ')}"><div ${isPrint ? '' : `style="width: ${column.renderWidth}px"`}><input type="radio" name="radio_${id}" ${item._radioDisabled ? 'disabled ' : ''}${getBooleanValue(cellValue) ? 'checked' : ''}><span>${item._radioLabel}</span></div></td>`;
- } else if (column.type === 'checkbox') {
- return `<td class="${classNames.join(' ')}"><div ${isPrint ? '' : `style="width: ${column.renderWidth}px"`}><input type="checkbox" ${item._checkboxDisabled ? 'disabled ' : ''}${getBooleanValue(cellValue) ? 'checked' : ''}><span>${item._checkboxLabel}</span></div></td>`;
- }
- return `<td class="${classNames.join(' ')}" title="${cellValue}"><div ${isPrint ? '' : `style="width: ${column.renderWidth}px"`}>${(0, _utils.formatText)(cellValue, true)}</div></td>`;
- }).join('') + '</tr>');
- });
- } else {
- datas.forEach(item => {
- tables.push('<tr>' + columns.map(column => {
- const colid = column.id;
- const cellAlign = column.align || allAlign;
- const classNames = hasEllipsis($xeTable, column, 'showOverflow', allColumnOverflow) ? ['col--ellipsis'] : [];
- const cellValue = item[colid];
- let rowSpan = 1;
- let colSpan = 1;
- if (isMerge) {
- const _rowIndex = $xeTable.getVTRowIndex(item._row);
- const _columnIndex = $xeTable.getVTColumnIndex(column);
- const spanRest = mergeBodyCellMaps[`${_rowIndex}:${_columnIndex}`];
- if (spanRest) {
- const {
- rowspan,
- colspan
- } = spanRest;
- if (!rowspan || !colspan) {
- return '';
- }
- if (rowspan > 1) {
- rowSpan = rowspan;
- }
- if (colspan > 1) {
- colSpan = colspan;
- }
- }
- }
- if (cellAlign) {
- classNames.push(`col--${cellAlign}`);
- }
- if (column.type === 'radio') {
- return `<td class="${classNames.join(' ')}" rowspan="${rowSpan}" colspan="${colSpan}"><div ${isPrint ? '' : `style="width: ${column.renderWidth}px"`}><input type="radio" name="radio_${id}" ${item._radioDisabled ? 'disabled ' : ''}${getBooleanValue(cellValue) ? 'checked' : ''}><span>${item._radioLabel}</span></div></td>`;
- } else if (column.type === 'checkbox') {
- return `<td class="${classNames.join(' ')}" rowspan="${rowSpan}" colspan="${colSpan}"><div ${isPrint ? '' : `style="width: ${column.renderWidth}px"`}><input type="checkbox" ${item._checkboxDisabled ? 'disabled ' : ''}${getBooleanValue(cellValue) ? 'checked' : ''}><span>${item._checkboxLabel}</span></div></td>`;
- }
- return `<td class="${classNames.join(' ')}" rowspan="${rowSpan}" colspan="${colSpan}" title="${cellValue}"><div ${isPrint ? '' : `style="width: ${column.renderWidth}px"`}>${(0, _utils.formatText)(cellValue, true)}</div></td>`;
- }).join('') + '</tr>');
- });
- }
- tables.push('</tbody>');
- }
- if (isFooter) {
- const {
- footerTableData
- } = reactData;
- const footers = getFooterData($xeTable, opts, footerTableData);
- if (footers.length) {
- tables.push('<tfoot>');
- footers.forEach(row => {
- tables.push(`<tr>${columns.map(column => {
- const footAlign = column.footerAlign || column.align || allFooterAlign || allAlign;
- const classNames = hasEllipsis($xeTable, column, 'showOverflow', allColumnOverflow) ? ['col--ellipsis'] : [];
- const cellValue = getFooterCellValue($xeTable, opts, row, column);
- if (footAlign) {
- classNames.push(`col--${footAlign}`);
- }
- return `<td class="${classNames.join(' ')}" title="${cellValue}"><div ${isPrint ? '' : `style="width: ${column.renderWidth}px"`}>${(0, _utils.formatText)(cellValue, true)}</div></td>`;
- }).join('')}</tr>`);
- });
- tables.push('</tfoot>');
- }
- }
- // 是否半选状态
- const script = !isAllSelected && isIndeterminate ? `<script>(function(){var a=document.querySelector(".${allCls}");if(a){a.indeterminate=true}})()</script>` : '';
- tables.push('</table>', script);
- return isPrint ? tables.join('') : (0, _util2.createHtmlPage)(opts, tables.join(''));
- }
- function toXML($xeTable, opts, columns, datas) {
- const reactData = $xeTable;
- let xml = ['<?xml version="1.0"?>', '<?mso-application progid="Excel.Sheet"?>', '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">', '<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">', '<Version>16.00</Version>', '</DocumentProperties>', '<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">', '<WindowHeight>7920</WindowHeight>', '<WindowWidth>21570</WindowWidth>', '<WindowTopX>32767</WindowTopX>', '<WindowTopY>32767</WindowTopY>', '<ProtectStructure>False</ProtectStructure>', '<ProtectWindows>False</ProtectWindows>', '</ExcelWorkbook>', `<Worksheet ss:Name="${opts.sheetName}">`, '<Table>', columns.map(column => `<Column ss:Width="${column.renderWidth}"/>`).join('')].join('');
- if (opts.isHeader) {
- xml += `<Row>${columns.map(column => `<Cell><Data ss:Type="String">${getHeaderTitle($xeTable, opts, column)}</Data></Cell>`).join('')}</Row>`;
- }
- datas.forEach(row => {
- xml += '<Row>' + columns.map(column => `<Cell><Data ss:Type="String">${row[column.id]}</Data></Cell>`).join('') + '</Row>';
- });
- if (opts.isFooter) {
- const {
- footerTableData
- } = reactData;
- const footers = getFooterData($xeTable, opts, footerTableData);
- footers.forEach(row => {
- xml += `<Row>${columns.map(column => `<Cell><Data ss:Type="String">${getFooterCellValue($xeTable, opts, row, column)}</Data></Cell>`).join('')}</Row>`;
- });
- }
- return `${xml}</Table></Worksheet></Workbook>`;
- }
- function getContent($xeTable, opts, columns, datas) {
- if (columns.length) {
- switch (opts.type) {
- case 'csv':
- return toCsv($xeTable, opts, columns, datas);
- case 'txt':
- return toTxt($xeTable, opts, columns, datas);
- case 'html':
- return toHtml($xeTable, opts, columns, datas);
- case 'xml':
- return toXML($xeTable, opts, columns, datas);
- }
- }
- return '';
- }
- function downloadFile($xeTable, opts, content) {
- const {
- filename,
- type,
- download
- } = opts;
- if (!download) {
- const blob = (0, _util2.getExportBlobByContent)(content, opts);
- return Promise.resolve({
- type,
- content,
- blob
- });
- }
- if (_ui.VxeUI.saveFile) {
- _ui.VxeUI.saveFile({
- filename,
- type,
- content
- }).then(() => {
- if (opts.message !== false) {
- if (_ui.VxeUI.modal) {
- _ui.VxeUI.modal.message({
- content: getI18n('vxe.table.expSuccess'),
- status: 'success'
- });
- }
- }
- });
- }
- }
- function clearColumnConvert(columns) {
- _xeUtils.default.eachTree(columns, column => {
- delete column._level;
- delete column._colSpan;
- delete column._rowSpan;
- delete column._children;
- delete column.childNodes;
- }, {
- children: 'children'
- });
- }
- function handleExport($xeTable, opts) {
- const $xeGrid = $xeTable.$xeGrid;
- const $xeGantt = $xeTable.$xeGantt;
- const {
- remote,
- columns,
- colgroups,
- exportMethod,
- afterExportMethod
- } = opts;
- return new Promise(resolve => {
- if (remote) {
- const params = {
- options: opts,
- $table: $xeTable,
- $grid: $xeGrid,
- $gantt: $xeGantt
- };
- resolve(exportMethod ? exportMethod(params) : params);
- } else {
- const datas = getExportData($xeTable, opts);
- resolve($xeTable.preventEvent(null, 'event.export', {
- options: opts,
- columns,
- colgroups,
- datas
- }, () => {
- return downloadFile($xeTable, opts, getContent($xeTable, opts, columns, datas));
- }));
- }
- }).then(params => {
- clearColumnConvert(columns);
- if (!opts.print) {
- if (afterExportMethod) {
- afterExportMethod({
- status: true,
- options: opts,
- $table: $xeTable,
- $grid: $xeGrid,
- $gantt: $xeGantt
- });
- }
- }
- return Object.assign({
- status: true
- }, params);
- }).catch(() => {
- clearColumnConvert(columns);
- if (!opts.print) {
- if (afterExportMethod) {
- afterExportMethod({
- status: false,
- options: opts,
- $table: $xeTable,
- $grid: $xeGrid,
- $gantt: $xeGantt
- });
- }
- }
- const params = {
- status: false
- };
- return Promise.reject(params);
- });
- }
- function getElementsByTagName(elem, qualifiedName) {
- return elem.getElementsByTagName(qualifiedName);
- }
- function getTxtCellKey(now) {
- return `#${now}@${_xeUtils.default.uniqueId()}`;
- }
- function replaceTxtCell(cell, vMaps) {
- return cell.replace(/#\d+@\d+/g, key => _xeUtils.default.hasOwnProp(vMaps, key) ? vMaps[key] : key);
- }
- function getTxtCellValue(val, vMaps) {
- const rest = replaceTxtCell(val, vMaps);
- return rest.replace(/^"+$/g, qVal => '"'.repeat(Math.ceil(qVal.length / 2)));
- }
- function toExportField(tableConf, field) {
- const {
- fieldMaps,
- titleMaps
- } = tableConf;
- // title 转 field
- if (!fieldMaps[field]) {
- const teCol = titleMaps[field];
- if (teCol && teCol.field) {
- field = teCol.field;
- }
- }
- return field;
- }
- function parseCsvAndTxt(tableConf, content, cellSeparator) {
- const list = content.split(enterSymbol);
- const rows = [];
- let fields = [];
- if (list.length) {
- const vMaps = {};
- const now = Date.now();
- list.forEach(rVal => {
- if (rVal) {
- const item = {};
- rVal = rVal.replace(/("")|(\n)/g, (text, dVal) => {
- const key = getTxtCellKey(now);
- vMaps[key] = dVal ? '"' : '\n';
- return key;
- }).replace(/"(.*?)"/g, (text, cVal) => {
- const key = getTxtCellKey(now);
- vMaps[key] = replaceTxtCell(cVal, vMaps);
- return key;
- });
- const cells = rVal.split(cellSeparator);
- if (!fields.length) {
- fields = cells.map(val => toExportField(tableConf, getTxtCellValue(val.trim(), vMaps)));
- } else {
- cells.forEach((val, colIndex) => {
- if (colIndex < fields.length) {
- item[fields[colIndex]] = getTxtCellValue(val.trim(), vMaps);
- }
- });
- rows.push(item);
- }
- }
- });
- }
- return {
- fields,
- rows
- };
- }
- function parseCsv(tableConf, content) {
- return parseCsvAndTxt(tableConf, content, ',');
- }
- function parseTxt(tableConf, content) {
- return parseCsvAndTxt(tableConf, content, '\t');
- }
- function parseHTML(tableConf, content) {
- const domParser = new DOMParser();
- const xmlDoc = domParser.parseFromString(content, 'text/html');
- const bodyNodes = getElementsByTagName(xmlDoc, 'body');
- const rows = [];
- const fields = [];
- if (bodyNodes.length) {
- const tableNodes = getElementsByTagName(bodyNodes[0], 'table');
- if (tableNodes.length) {
- const theadNodes = getElementsByTagName(tableNodes[0], 'thead');
- if (theadNodes.length) {
- _xeUtils.default.arrayEach(getElementsByTagName(theadNodes[0], 'tr'), rowNode => {
- _xeUtils.default.arrayEach(getElementsByTagName(rowNode, 'th'), cellNode => {
- fields.push(toExportField(tableConf, cellNode.textContent || ''));
- });
- });
- const tbodyNodes = getElementsByTagName(tableNodes[0], 'tbody');
- if (tbodyNodes.length) {
- _xeUtils.default.arrayEach(getElementsByTagName(tbodyNodes[0], 'tr'), rowNode => {
- const item = {};
- _xeUtils.default.arrayEach(getElementsByTagName(rowNode, 'td'), (cellNode, colIndex) => {
- if (fields[colIndex]) {
- item[fields[colIndex]] = cellNode.textContent || '';
- }
- });
- rows.push(item);
- });
- }
- }
- }
- }
- return {
- fields,
- rows
- };
- }
- function parseXML(tableConf, content) {
- const domParser = new DOMParser();
- const xmlDoc = domParser.parseFromString(content, 'application/xml');
- const sheetNodes = getElementsByTagName(xmlDoc, 'Worksheet');
- const rows = [];
- const fields = [];
- if (sheetNodes.length) {
- const tableNodes = getElementsByTagName(sheetNodes[0], 'Table');
- if (tableNodes.length) {
- const rowNodes = getElementsByTagName(tableNodes[0], 'Row');
- if (rowNodes.length) {
- _xeUtils.default.arrayEach(getElementsByTagName(rowNodes[0], 'Cell'), cellNode => {
- fields.push(toExportField(tableConf, cellNode.textContent || ''));
- });
- _xeUtils.default.arrayEach(rowNodes, (rowNode, index) => {
- if (index) {
- const item = {};
- const cellNodes = getElementsByTagName(rowNode, 'Cell');
- _xeUtils.default.arrayEach(cellNodes, (cellNode, colIndex) => {
- if (fields[colIndex]) {
- item[fields[colIndex]] = cellNode.textContent;
- }
- });
- rows.push(item);
- }
- });
- }
- }
- }
- return {
- fields,
- rows
- };
- }
- function handleImport($xeTable, content, opts) {
- const internalData = $xeTable;
- const {
- tableFullColumn,
- _importResolve,
- _importReject
- } = internalData;
- let rest = {
- fields: [],
- rows: []
- };
- const tableFieldMaps = {};
- const tableTitleMaps = {};
- tableFullColumn.forEach(column => {
- const field = column.field;
- const title = column.getTitle();
- if (field) {
- tableFieldMaps[field] = column;
- }
- if (title) {
- tableTitleMaps[column.getTitle()] = column;
- }
- });
- const tableConf = {
- fieldMaps: tableFieldMaps,
- titleMaps: tableTitleMaps
- };
- switch (opts.type) {
- case 'csv':
- rest = parseCsv(tableConf, content);
- break;
- case 'txt':
- rest = parseTxt(tableConf, content);
- break;
- case 'html':
- rest = parseHTML(tableConf, content);
- break;
- case 'xml':
- rest = parseXML(tableConf, content);
- break;
- }
- const {
- fields,
- rows
- } = rest;
- const status = fields.some(field => tableFieldMaps[field] || tableTitleMaps[field]);
- if (status) {
- $xeTable.createData(rows).then(data => {
- let loadRest;
- if (opts.mode === 'insert' || opts.mode === 'insertBottom') {
- loadRest = $xeTable.insertAt(data, -1);
- }
- if (opts.mode === 'insertTop') {
- loadRest = $xeTable.insert(data);
- } else {
- loadRest = $xeTable.reloadData(data);
- }
- if (opts.message !== false) {
- // 检测弹窗模块
- if (!_ui.VxeUI.modal) {
- (0, _log.errLog)('vxe.error.reqModule', ['Modal']);
- }
- _ui.VxeUI.modal.message({
- content: getI18n('vxe.table.impSuccess', [rows.length]),
- status: 'success'
- });
- }
- return loadRest.then(() => {
- if (_importResolve) {
- _importResolve({
- status: true
- });
- }
- });
- });
- } else if (opts.message !== false) {
- // 检测弹窗模块
- if (!_ui.VxeUI.modal) {
- (0, _log.errLog)('vxe.error.reqModule', ['Modal']);
- }
- _ui.VxeUI.modal.message({
- content: getI18n('vxe.error.impFields'),
- status: 'error'
- });
- if (_importReject) {
- _importReject({
- status: false
- });
- }
- }
- }
- function handleFileImport($xeTable, file, opts) {
- const internalData = $xeTable;
- const importOpts = $xeTable.computeImportOpts;
- const {
- importMethod,
- afterImportMethod
- } = opts;
- const {
- type,
- filename
- } = (0, _utils.parseFile)(file);
- // 检查类型,如果为自定义导出,则不需要校验类型
- if (!importMethod && !_xeUtils.default.includes(_xeUtils.default.keys(importOpts._typeMaps), type)) {
- if (opts.message !== false) {
- // 检测弹窗模块
- if (!_ui.VxeUI.modal) {
- (0, _log.errLog)('vxe.error.reqModule', ['Modal']);
- }
- _ui.VxeUI.modal.message({
- content: getI18n('vxe.error.notType', [type]),
- status: 'error'
- });
- }
- const params = {
- status: false
- };
- return Promise.reject(params);
- }
- const rest = new Promise((resolve, reject) => {
- const _importResolve = params => {
- resolve(params);
- $xeTable._importResolve = null;
- $xeTable._importReject = null;
- };
- const _importReject = params => {
- reject(params);
- $xeTable._importResolve = null;
- $xeTable._importReject = null;
- };
- $xeTable._importResolve = _importResolve;
- $xeTable._importReject = _importReject;
- if (window.FileReader) {
- const options = Object.assign({
- mode: 'insertTop'
- }, opts, {
- type,
- filename
- });
- if (options.remote) {
- if (importMethod) {
- Promise.resolve(importMethod({
- file,
- options,
- $table: $xeTable
- })).then(() => {
- _importResolve({
- status: true
- });
- }).catch(() => {
- _importResolve({
- status: true
- });
- });
- } else {
- _importResolve({
- status: true
- });
- }
- } else {
- const {
- tableFullColumn
- } = internalData;
- $xeTable.preventEvent(null, 'event.import', {
- file,
- options,
- columns: tableFullColumn
- }, () => {
- const reader = new FileReader();
- reader.onerror = () => {
- (0, _log.errLog)('vxe.error.notType', [type]);
- _importReject({
- status: false
- });
- };
- reader.onload = e => {
- handleImport($xeTable, e.target.result, options);
- };
- reader.readAsText(file, options.encoding || 'UTF-8');
- });
- }
- } else {
- // 不支持的浏览器
- (0, _log.errLog)('vxe.error.notExp');
- _importResolve({
- status: true
- });
- }
- });
- return rest.then(() => {
- if (afterImportMethod) {
- afterImportMethod({
- status: true,
- options: opts,
- $table: $xeTable
- });
- }
- }).catch(e => {
- if (afterImportMethod) {
- afterImportMethod({
- status: false,
- options: opts,
- $table: $xeTable
- });
- }
- return Promise.reject(e);
- });
- }
- function handleCloseExport() {
- if (_ui.VxeUI.modal) {
- return _ui.VxeUI.modal.close('VXE_EXPORT_MODAL');
- }
- return Promise.resolve();
- }
- function handleFilterColumns(exportOpts, column, columns) {
- return columns.some(item => {
- if ((0, _util.isColumnInfo)(item)) {
- return column.id === item.id;
- } else if (_xeUtils.default.isString(item)) {
- return column.field === item;
- } else {
- const colid = item.id || item.colId;
- const type = item.type;
- const field = item.field;
- if (colid) {
- return column.id === colid;
- } else if (field && type) {
- return column.field === field && column.type === type;
- } else if (field) {
- return column.field === field;
- } else if (type) {
- return column.type === type;
- }
- }
- return false;
- });
- }
- function handleFilterFields(exportOpts, column, includeFields, excludeFields) {
- if (excludeFields) {
- if (_xeUtils.default.includes(excludeFields, column.field)) {
- return false;
- }
- }
- if (includeFields) {
- if (_xeUtils.default.includes(includeFields, column.field)) {
- return true;
- }
- return false;
- }
- return exportOpts.original ? !!column.field : defaultFilterExportColumn(column);
- }
- function handleExportAndPrint($xeTable, options, isPrint) {
- const props = $xeTable;
- const reactData = $xeTable;
- const internalData = $xeTable;
- const $xeGrid = $xeTable.$xeGrid;
- const $xeGantt = $xeTable.$xeGantt;
- const $xeGGWrapper = $xeGrid || $xeGantt;
- const {
- treeConfig,
- showHeader,
- showFooter
- } = props;
- const {
- initStore,
- isGroup,
- footerTableData,
- exportStore,
- exportParams
- } = reactData;
- const {
- collectColumn,
- mergeBodyList,
- mergeFooterList
- } = internalData;
- const exportOpts = $xeTable.computeExportOpts;
- const hasTree = treeConfig;
- const customOpts = $xeTable.computeCustomOpts;
- const selectRecords = $xeTable.getCheckboxRecords();
- const proxyOpts = $xeGGWrapper ? $xeGGWrapper.computeProxyOpts : {};
- const hasFooter = !!footerTableData.length;
- const hasMerge = !!(mergeBodyList.length || mergeFooterList.length);
- const defOpts = Object.assign({
- message: true,
- isHeader: showHeader,
- isTitle: showHeader,
- isFooter: showFooter,
- isColgroup: isGroup,
- isMerge: hasMerge,
- useStyle: true,
- current: 'current',
- modes: (proxyOpts.ajax && proxyOpts.ajax.queryAll ? ['all'] : []).concat(['current', 'selected', 'empty'])
- }, options);
- const types = defOpts.types || _xeUtils.default.keys(exportOpts._typeMaps);
- const modes = defOpts.modes || [];
- const checkMethod = customOpts.checkMethod;
- const exportColumns = collectColumn.slice(0);
- const {
- columns,
- excludeFields,
- includeFields
- } = defOpts;
- // 处理类型
- const typeList = types.map(value => {
- return {
- value,
- label: getI18n(`vxe.export.types.${value}`)
- };
- });
- const modeList = modes.map(item => {
- if (item && item.value) {
- return {
- value: item.value,
- label: item.label || item.value
- };
- }
- return {
- value: item,
- label: getI18n(`vxe.export.modes.${item}`)
- };
- });
- // 默认选中
- _xeUtils.default.eachTree(exportColumns, (column, index, items, path, parent) => {
- const isColGroup = column.children && column.children.length > 0;
- let isChecked = false;
- if (columns && columns.length) {
- isChecked = handleFilterColumns(defOpts, column, columns);
- } else if (excludeFields || includeFields) {
- isChecked = handleFilterFields(defOpts, column, includeFields, excludeFields);
- } else {
- isChecked = column.visible && (isColGroup || defaultFilterExportColumn(column));
- }
- column.checked = isChecked;
- column.halfChecked = false;
- column.disabled = parent && parent.disabled || (checkMethod ? !checkMethod({
- $table: $xeTable,
- column
- }) : false);
- });
- // 更新条件
- Object.assign(exportStore, {
- columns: exportColumns,
- typeList,
- modeList,
- hasFooter,
- hasMerge,
- hasTree,
- isPrint,
- hasColgroup: isGroup,
- visible: true
- });
- // 默认参数
- Object.assign(exportParams, {
- mode: selectRecords.length ? 'selected' : 'current'
- }, defOpts);
- const {
- filename,
- sheetName,
- mode,
- type
- } = exportParams;
- if (filename) {
- if (_xeUtils.default.isFunction(filename)) {
- exportParams.filename = filename({
- options: defOpts,
- $table: $xeTable,
- $grid: $xeGrid,
- $gantt: $xeGantt
- });
- } else {
- exportParams.filename = `${filename}`;
- }
- }
- if (sheetName) {
- if (_xeUtils.default.isFunction(sheetName)) {
- exportParams.sheetName = sheetName({
- options: defOpts,
- $table: $xeTable,
- $grid: $xeGrid,
- $gantt: $xeGantt
- });
- } else {
- exportParams.sheetName = `${sheetName}`;
- }
- }
- if (!modeList.some(item => item.value === mode)) {
- exportParams.mode = modeList[0].value;
- }
- if (!typeList.some(item => item.value === type)) {
- exportParams.type = typeList[0].value;
- }
- initStore.export = true;
- return $xeTable.$nextTick();
- }
- const getConvertColumns = columns => {
- const result = [];
- columns.forEach(column => {
- if (column.childNodes && column.childNodes.length) {
- result.push(column);
- result.push(...getConvertColumns(column.childNodes));
- } else {
- result.push(column);
- }
- });
- return result;
- };
- const convertToRows = originColumns => {
- let maxLevel = 1;
- const traverse = (column, parent) => {
- if (parent) {
- column._level = parent._level + 1;
- if (maxLevel < column._level) {
- maxLevel = column._level;
- }
- }
- if (column.childNodes && column.childNodes.length) {
- let colSpan = 0;
- column.childNodes.forEach(subColumn => {
- traverse(subColumn, column);
- colSpan += subColumn._colSpan;
- });
- column._colSpan = colSpan;
- } else {
- column._colSpan = 1;
- }
- };
- originColumns.forEach(column => {
- column._level = 1;
- traverse(column);
- });
- const rows = [];
- for (let i = 0; i < maxLevel; i++) {
- rows.push([]);
- }
- const allColumns = getConvertColumns(originColumns);
- allColumns.forEach(column => {
- if (column.childNodes && column.childNodes.length) {
- column._rowSpan = 1;
- } else {
- column._rowSpan = maxLevel - column._level + 1;
- }
- rows[column._level - 1].push(column);
- });
- return rows;
- };
- var _default = exports.default = {
- methods: {
- /**
- * 导出文件,支持 csv/html/xml/txt
- * 如果是树表格,则默认是导出所有节点
- * 如果是启用了虚拟滚动,则只能导出数据源,可以配合 dataFilterMethod 函数转换数据
- * @param {Object} options 参数
- */
- _exportData(options) {
- const $xeTable = this;
- const props = $xeTable;
- const reactData = $xeTable;
- const internalData = $xeTable;
- const $xeGrid = $xeTable.$xeGrid;
- const $xeGantt = $xeTable.$xeGantt;
- const $xeGGWrapper = $xeGrid || $xeGantt;
- const {
- treeConfig,
- showHeader,
- showFooter
- } = props;
- const {
- isGroup
- } = reactData;
- const {
- tableFullColumn,
- afterFullData,
- afterTreeFullData,
- collectColumn,
- mergeBodyList,
- mergeFooterList
- } = internalData;
- const exportOpts = $xeTable.computeExportOpts;
- const treeOpts = $xeTable.computeTreeOpts;
- const proxyOpts = $xeGGWrapper ? $xeGGWrapper.computeProxyOpts : {};
- const hasMerge = !!(mergeBodyList.length || mergeFooterList.length);
- const opts = Object.assign({
- message: true,
- isHeader: showHeader,
- isTitle: showHeader,
- isFooter: showFooter,
- isColgroup: isGroup,
- isMerge: hasMerge,
- useStyle: true,
- current: 'current',
- modes: (proxyOpts.ajax && proxyOpts.ajax.queryAll ? ['all'] : []).concat(['current', 'selected', 'empty']),
- download: true,
- type: 'csv'
- // filename: '',
- // sheetName: '',
- // original: false,
- // isAllExpand: false,
- // data: null,
- // remote: false,
- // dataFilterMethod: null,
- // footerFilterMethod: null,
- // exportMethod: null,
- // columnFilterMethod: null,
- // beforeExportMethod: null,
- // afterExportMethod: null
- }, exportOpts, options);
- let {
- filename,
- sheetName,
- type,
- mode,
- columns,
- original,
- columnFilterMethod,
- beforeExportMethod,
- includeFields,
- excludeFields
- } = opts;
- let groups = [];
- const selectRecords = $xeTable.getCheckboxRecords();
- if (!mode) {
- mode = selectRecords.length ? 'selected' : 'current';
- }
- let isCustomCol = false;
- let customCols = [];
- if (columns && columns.length) {
- isCustomCol = true;
- customCols = columns;
- } else {
- customCols = _xeUtils.default.searchTree(collectColumn, column => {
- const isColGroup = column.children && column.children.length > 0;
- let isChecked = false;
- if (columns && columns.length) {
- isChecked = handleFilterColumns(opts, column, columns);
- } else if (excludeFields || includeFields) {
- isChecked = handleFilterFields(opts, column, includeFields, excludeFields);
- } else {
- isChecked = column.visible && (isColGroup || defaultFilterExportColumn(column));
- }
- return isChecked;
- }, {
- children: 'children',
- mapChildren: 'childNodes',
- original: true
- });
- }
- const handleOptions = Object.assign({}, opts, {
- filename: '',
- sheetName: ''
- });
- // 如果设置源数据,则默认导出设置了字段的列
- if (!isCustomCol && !columnFilterMethod) {
- columnFilterMethod = ({
- column
- }) => {
- if (excludeFields) {
- if (_xeUtils.default.includes(excludeFields, column.field)) {
- return false;
- }
- }
- if (includeFields) {
- if (_xeUtils.default.includes(includeFields, column.field)) {
- return true;
- }
- return false;
- }
- return original ? !!column.field : defaultFilterExportColumn(column);
- };
- handleOptions.columnFilterMethod = columnFilterMethod;
- }
- if (customCols) {
- handleOptions._isCustomColumn = true;
- groups = _xeUtils.default.searchTree(_xeUtils.default.mapTree(customCols, item => {
- let targetColumn;
- if (item) {
- if ((0, _util.isColumnInfo)(item)) {
- targetColumn = item;
- } else if (_xeUtils.default.isString(item)) {
- targetColumn = $xeTable.getColumnByField(item);
- } else {
- const colid = item.id || item.colId;
- const type = item.type;
- const field = item.field;
- if (colid) {
- targetColumn = $xeTable.getColumnById(colid);
- } else if (field && type) {
- targetColumn = tableFullColumn.find(column => column.field === field && column.type === type);
- } else if (field) {
- targetColumn = $xeTable.getColumnByField(field);
- } else if (type) {
- targetColumn = tableFullColumn.find(column => column.type === type);
- }
- }
- return targetColumn || {};
- }
- }, {
- children: 'childNodes',
- mapChildren: '_children'
- }), (column, index) => (0, _util.isColumnInfo)(column) && (!columnFilterMethod || columnFilterMethod({
- $table: $xeTable,
- $grid: $xeGrid,
- $gantt: $xeGantt,
- column: column,
- $columnIndex: index
- })), {
- children: '_children',
- mapChildren: 'childNodes',
- original: true
- });
- } else {
- groups = _xeUtils.default.searchTree(isGroup ? collectColumn : tableFullColumn, (column, index) => column.visible && (!columnFilterMethod || columnFilterMethod({
- $table: $xeTable,
- $grid: $xeGrid,
- $gantt: $xeGantt,
- column,
- $columnIndex: index
- })), {
- children: 'children',
- mapChildren: 'childNodes',
- original: true
- });
- }
- // 获取所有列
- const cols = [];
- _xeUtils.default.eachTree(groups, column => {
- const isColGroup = column.children && column.children.length;
- if (!isColGroup) {
- cols.push(column);
- }
- }, {
- children: 'childNodes'
- });
- // 构建分组层级
- handleOptions.columns = cols;
- handleOptions.colgroups = convertToRows(groups);
- if (filename) {
- if (_xeUtils.default.isFunction(filename)) {
- handleOptions.filename = filename({
- options: opts,
- $table: $xeTable,
- $grid: $xeGrid,
- $gantt: $xeGantt
- });
- } else {
- handleOptions.filename = `${filename}`;
- }
- }
- if (!handleOptions.filename) {
- handleOptions.filename = getI18n(handleOptions.original ? 'vxe.table.expOriginFilename' : 'vxe.table.expFilename', [_xeUtils.default.toDateString(Date.now(), 'yyyyMMddHHmmss')]);
- }
- if (sheetName) {
- if (_xeUtils.default.isFunction(sheetName)) {
- handleOptions.sheetName = sheetName({
- options: opts,
- $table: $xeTable,
- $grid: $xeGrid,
- $gantt: $xeGantt
- });
- } else {
- handleOptions.sheetName = `${sheetName}`;
- }
- }
- if (!handleOptions.sheetName) {
- handleOptions.sheetName = document.title || '';
- }
- // 检查类型,如果为自定义导出,则不需要校验类型
- if (!handleOptions.exportMethod && !_xeUtils.default.includes(_xeUtils.default.keys(exportOpts._typeMaps), type)) {
- (0, _log.errLog)('vxe.error.notType', [type]);
- if (['xlsx', 'pdf'].includes(type)) {
- (0, _log.warnLog)('vxe.error.reqPlugin', [4, 'plugin-export-xlsx']);
- }
- const params = {
- status: false
- };
- return Promise.reject(params);
- }
- if (!handleOptions.print) {
- if (beforeExportMethod) {
- beforeExportMethod({
- options: handleOptions,
- $table: $xeTable,
- $grid: $xeGrid,
- $gantt: $xeGantt
- });
- }
- }
- if (!handleOptions.data) {
- handleOptions.data = [];
- if (mode === 'selected') {
- if (['html', 'pdf'].indexOf(type) > -1 && treeConfig) {
- handleOptions.data = _xeUtils.default.searchTree($xeTable.getTableData().fullData, item => $xeTable.findRowIndexOf(selectRecords, item) > -1, Object.assign({}, treeOpts, {
- data: '_row'
- }));
- } else {
- handleOptions.data = selectRecords;
- }
- } else if (mode === 'all') {
- if (!$xeGGWrapper) {
- (0, _log.errLog)('vxe.error.errProp', ['all', 'mode=current,selected']);
- }
- if ($xeGGWrapper && !handleOptions.remote) {
- const gridReactData = $xeGGWrapper;
- const proxyOpts = $xeGGWrapper.computeProxyOpts;
- const {
- sortData
- } = gridReactData;
- const {
- beforeQueryAll,
- afterQueryAll,
- ajax = {}
- } = proxyOpts;
- const resConfigs = proxyOpts.response || proxyOpts.props || {};
- const ajaxMethods = ajax.queryAll;
- const queryAllSuccessMethods = ajax.queryAllSuccess;
- const queryAllErrorMethods = ajax.queryAllError;
- if (!ajaxMethods) {
- (0, _log.errLog)('vxe.error.notFunc', ['proxy-config.ajax.queryAll']);
- }
- if (ajaxMethods) {
- const params = {
- $table: $xeTable,
- $grid: $xeGrid,
- $gantt: $xeGantt,
- sort: sortData.length ? sortData[0] : {},
- sorts: sortData,
- filters: gridReactData.filterData,
- form: gridReactData.formData,
- options: handleOptions
- };
- return Promise.resolve((beforeQueryAll || ajaxMethods)(params)).then(rest => {
- const listProp = resConfigs.list;
- handleOptions.data = (listProp ? _xeUtils.default.isFunction(listProp) ? listProp({
- data: rest,
- $table: $xeTable,
- $grid: $xeGrid,
- $gantt: $xeGantt
- }) : _xeUtils.default.get(rest, listProp) : rest) || [];
- if (afterQueryAll) {
- afterQueryAll(params);
- }
- if (queryAllSuccessMethods) {
- queryAllSuccessMethods(Object.assign(Object.assign({}, params), {
- response: rest
- }));
- }
- return handleExport($xeTable, handleOptions);
- }).catch(rest => {
- if (queryAllErrorMethods) {
- queryAllErrorMethods(Object.assign(Object.assign({}, params), {
- response: rest
- }));
- }
- });
- }
- }
- } else if (mode === 'current') {
- handleOptions.data = treeConfig ? afterTreeFullData : afterFullData;
- }
- } else {
- handleOptions._isCustomData = true;
- }
- return handleExport($xeTable, handleOptions);
- },
- _importByFile(file, options) {
- const $xeTable = this;
- const opts = Object.assign({}, options);
- const {
- beforeImportMethod
- } = opts;
- if (beforeImportMethod) {
- beforeImportMethod({
- options: opts,
- $table: $xeTable
- });
- }
- return handleFileImport($xeTable, file, opts);
- },
- _importData(options) {
- const $xeTable = this;
- const {
- importOpts
- } = this;
- const opts = Object.assign({
- types: _xeUtils.default.keys(importOpts._typeMaps)
- // beforeImportMethod: null,
- // afterImportMethod: null
- }, importOpts, options);
- const {
- beforeImportMethod,
- afterImportMethod
- } = opts;
- if (beforeImportMethod) {
- beforeImportMethod({
- options: opts,
- $table: this
- });
- }
- return _ui.VxeUI.readFile(opts).catch(e => {
- if (afterImportMethod) {
- afterImportMethod({
- status: false,
- options: opts,
- $table: $xeTable
- });
- }
- return Promise.reject(e);
- }).then(params => {
- const {
- file
- } = params;
- return handleFileImport($xeTable, file, opts);
- });
- },
- _saveFile(options) {
- return _ui.VxeUI.saveFile(options);
- },
- _readFile(options) {
- return _ui.VxeUI.readFile(options);
- },
- _print(options) {
- const $xeTable = this;
- const $xeGrid = $xeTable.$xeGrid;
- const $xeGantt = $xeTable.$xeGantt;
- const printOpts = $xeTable.computePrintOpts;
- const opts = Object.assign({
- original: false
- // beforePrintMethod
- }, printOpts, options, {
- type: 'html',
- download: false,
- remote: false,
- print: true
- });
- const {
- sheetName
- } = opts;
- let printTitle = '';
- if (sheetName) {
- if (_xeUtils.default.isFunction(sheetName)) {
- printTitle = sheetName({
- options: opts,
- $table: $xeTable,
- $grid: $xeGrid,
- $gantt: $xeGantt
- });
- } else {
- printTitle = `${sheetName}`;
- }
- }
- if (!printTitle) {
- printTitle = document.title || '';
- }
- const beforePrintMethod = opts.beforePrintMethod;
- const tableHtml = opts.html || opts.content;
- return new Promise((resolve, reject) => {
- if (_ui.VxeUI.print) {
- if (tableHtml) {
- resolve(_ui.VxeUI.print({
- title: printTitle,
- html: tableHtml,
- customStyle: opts.style,
- beforeMethod: beforePrintMethod ? ({
- html
- }) => {
- return beforePrintMethod({
- html,
- content: html,
- options: opts,
- $table: $xeTable,
- $grid: $xeGrid,
- $gantt: $xeGantt
- });
- } : undefined
- }));
- } else {
- resolve($xeTable.exportData(opts).then(({
- content
- }) => {
- return _ui.VxeUI.print({
- title: printTitle,
- html: content,
- customStyle: opts.style,
- beforeMethod: beforePrintMethod ? ({
- html
- }) => {
- return beforePrintMethod({
- html,
- content: html,
- options: opts,
- $table: $xeTable,
- $grid: $xeGrid,
- $gantt: $xeGantt
- });
- } : undefined
- });
- }));
- }
- } else {
- const e = {
- status: false
- };
- reject(e);
- }
- });
- },
- _getPrintHtml(options) {
- const $xeTable = this;
- const printOpts = $xeTable.computePrintOpts;
- const opts = Object.assign({
- original: false
- // beforePrintMethod
- }, printOpts, options, {
- type: 'html',
- download: false,
- remote: false,
- print: true
- });
- return $xeTable.exportData(opts).then(({
- content
- }) => {
- return {
- html: content
- };
- });
- },
- _closeImport() {
- if (_ui.VxeUI.modal) {
- return _ui.VxeUI.modal.close('VXE_IMPORT_MODAL');
- }
- return Promise.resolve();
- },
- _openImport(options) {
- const $xeTable = this;
- const props = $xeTable;
- const reactData = $xeTable;
- const {
- treeConfig,
- importConfig
- } = props;
- const {
- initStore,
- importStore,
- importParams
- } = reactData;
- const importOpts = $xeTable.computeImportOpts;
- const defOpts = Object.assign({
- mode: 'insertTop',
- message: true,
- types: _xeUtils.default.keys(importOpts._typeMaps),
- modes: ['insertTop', 'covering']
- }, importOpts, options);
- const types = defOpts.types || [];
- const modes = defOpts.modes || [];
- const isTree = !!treeConfig;
- if (isTree) {
- if (defOpts.message) {
- _ui.VxeUI.modal.message({
- content: getI18n('vxe.error.treeNotImp'),
- status: 'error'
- });
- }
- return;
- }
- if (!importConfig) {
- (0, _log.errLog)('vxe.error.reqProp', ['import-config']);
- }
- // 处理类型
- const typeList = types.map(value => {
- return {
- value,
- label: getI18n(`vxe.export.types.${value}`)
- };
- });
- const modeList = modes.map(item => {
- if (item && item.value) {
- return {
- value: item.value,
- label: item.label || item.value
- };
- }
- return {
- value: item,
- label: getI18n(`vxe.import.modes.${item}`)
- };
- });
- Object.assign(importStore, {
- file: null,
- type: '',
- filename: '',
- modeList,
- typeList,
- visible: true
- });
- Object.assign(importParams, defOpts);
- if (!modeList.some(item => item.value === importParams.mode)) {
- importParams.mode = modeList[0].value;
- }
- initStore.import = true;
- },
- _closeExport: handleCloseExport,
- _openExport(options) {
- const $xeTable = this;
- const props = $xeTable;
- const exportOpts = $xeTable.computeExportOpts;
- const defOpts = Object.assign({
- message: true,
- types: _xeUtils.default.keys(exportOpts._typeMaps)
- }, exportOpts, options);
- if (!props.exportConfig) {
- (0, _log.errLog)('vxe.error.reqProp', ['export-config']);
- }
- return handleExportAndPrint($xeTable, defOpts);
- },
- _closePrint: handleCloseExport,
- _openPrint(options) {
- const $xeTable = this;
- const props = $xeTable;
- const printOpts = $xeTable.computePrintOpts;
- const defOpts = Object.assign({
- message: true
- }, printOpts, options);
- if (!props.printConfig) {
- (0, _log.errLog)('vxe.error.reqProp', ['print-config']);
- }
- return handleExportAndPrint($xeTable, defOpts, true);
- }
- }
- };
|