UNPKG

fastlion-amis

Version:

一种MIS页面生成工具

112 lines (111 loc) 5.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var react_1 = (0, tslib_1.__importDefault)(require("react")); var XLSX = tslib_1.__importStar(require("xlsx")); var style_1 = require("./style"); var message_1 = (0, tslib_1.__importDefault)(require("antd/lib/message")); var Modal_1 = (0, tslib_1.__importDefault)(require("antd/lib/Modal")); var XlsPreview = /** @class */ (function (_super) { (0, tslib_1.__extends)(XlsPreview, _super); function XlsPreview() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.state = { cols: null, csv: null }; _this.renderTable = function (rows) { return rows.map(function (row, idx) { var tag = 0; var columns = row.split(','); columns.forEach(function (item) { if (item !== '') { tag++; } }); if (tag === 0) { return null; } ; return react_1.default.createElement("tr", null, columns.map(function (column) { return react_1.default.createElement("td", null, react_1.default.createElement("span", { className: "td-cell", style: { width: '100px' } }, column)); })); }); }; _this.renderCols = function (columns) { return columns.map(function (column, index) { var width = column.width || 50; return (react_1.default.createElement("col", { key: index, style: { width: width, minWidth: width } })); }); }; _this.readWorkbook = function (wookbook) { var sheetNames = wookbook.SheetNames; var worksheet = wookbook.Sheets[sheetNames[0]]; var csv = XLSX.utils.sheet_to_csv(worksheet); var rows = csv.split("\n"); rows.pop(); _this.setState({ cols: _this.renderCols(rows), csv: _this.renderTable(rows) }); }; _this.getXlsxData = function (url) { var env = _this.props.env; if (url.length === 0) { return; } env.fetcher({ url: url, method: "get", data: {}, responseType: "arraybuffer" }).then(function (res) { var workbook = XLSX.read(new Uint8Array(res.data), { type: 'array' }); _this.readWorkbook(workbook); }); }; return _this; } XlsPreview.prototype.componentDidMount = function () { var current = this.props.current; var url = (current === null || current === void 0 ? void 0 : current.url) ? current.url : ((current === null || current === void 0 ? void 0 : current.addr) ? current === null || current === void 0 ? void 0 : current.addr : ''); try { this.getXlsxData(url); } catch (err) { message_1.default.error('数据导入有误'); } }; XlsPreview.prototype.render = function () { var current = this.props.current; return react_1.default.createElement(react_1.default.Fragment, null, current && (current === null || current === void 0 ? void 0 : current.name) && ((current === null || current === void 0 ? void 0 : current.addr) || (current === null || current === void 0 ? void 0 : current.url)) ? react_1.default.createElement(Modal_1.default, { wrapClassName: "file-preview", title: current === null || current === void 0 ? void 0 : current.name, visible: this.props.visible, footer: null, onCancel: this.props.setVisible, closable: true }, react_1.default.createElement("div", { className: 'view', style: { display: 'flex', justifyContent: 'center', /* padding: 50px 0; */ overflow: 'auto', /* width: 200px; */ margin: 'auto', height: '400px', width: "100%" } }, react_1.default.createElement("div", { className: 'pageContainer', style: { width: '100vw', maxWidth: '100%', height: "inherit" } }, react_1.default.createElement(style_1.Wrapper, null, react_1.default.createElement("div", { className: "table-container", style: { width: "100%" } }, react_1.default.createElement("div", { style: { maxHeight: "350px", overflow: "auto scroll" } }, react_1.default.createElement("table", null, react_1.default.createElement("colgroup", null, this.state.cols ? this.state.cols : null), react_1.default.createElement("tbody", null, this.state.csv ? this.state.csv : null)))))))) : null); }; return XlsPreview; }(react_1.default.PureComponent)); exports.default = XlsPreview; //# sourceMappingURL=./renderers/Lion/Table/LionCellFile/XlsPreview/index.js.map