Commit ca2a9918ce979fb4b96e4a7ce252cd9d6f46dcaa
1 parent
129fcf9b
feat: support import csv; !#zh: 支持导入 csv
Showing
5 changed files
with
132 additions
and
26 deletions
front-end/h5/package.json
| ... | ... | @@ -26,6 +26,7 @@ |
| 26 | 26 | "font-awesome": "4.7.0", |
| 27 | 27 | "hotkeys-js": "^3.7.6", |
| 28 | 28 | "html2canvas": "^1.0.0-rc.3", |
| 29 | + "papaparse": "^5.2.0", | |
| 29 | 30 | "proxy-agent": "^3.1.0", |
| 30 | 31 | "qrcode": "^1.4.1", |
| 31 | 32 | "register-service-worker": "^1.6.2", |
| ... | ... | @@ -57,6 +58,7 @@ |
| 57 | 58 | "cross-env": "^6.0.3", |
| 58 | 59 | "dart-sass": "^1.23.7", |
| 59 | 60 | "eslint": "^5.16.0", |
| 61 | + "eslint-plugin-cypress": "^2.11.1", | |
| 60 | 62 | "eslint-plugin-vue": "^5.0.0", |
| 61 | 63 | "less-loader": "^6.1.1", |
| 62 | 64 | "sass": "^1.18.0", | ... | ... |
front-end/h5/src/components/core/support/csv-import.js
0 → 100644
| 1 | +import Papa from 'papaparse' | |
| 2 | + | |
| 3 | +const validFileMimeTypes = ['text/csv', 'text/x-csv', 'application/vnd.ms-excel', 'text/plain'] | |
| 4 | +export default { | |
| 5 | + name: 'lbs-csv-import', | |
| 6 | + methods: { | |
| 7 | + checkMimeType (type) { | |
| 8 | + return validFileMimeTypes.indexOf(type) > -1 | |
| 9 | + }, | |
| 10 | + validFileMimeType (e) { | |
| 11 | + e.preventDefault() | |
| 12 | + let file = this.$refs.csv.files[0] | |
| 13 | + const isValidFileMimeType = this.checkMimeType(file.type) | |
| 14 | + if (isValidFileMimeType) this.loadFile() | |
| 15 | + }, | |
| 16 | + loadFile () { | |
| 17 | + /** | |
| 18 | + * output {String} | |
| 19 | + "columnA,columnB,columnC | |
| 20 | + "Susan",41,a | |
| 21 | + "Mike",5,b | |
| 22 | + "Jake",33,c | |
| 23 | + "Jill",30,d | |
| 24 | + " | |
| 25 | + * csv {Object} | |
| 26 | + { | |
| 27 | + "data": [ | |
| 28 | + ["columnA", "columnB", "columnC"], | |
| 29 | + ["Susan", "41", "a"], | |
| 30 | + ["Mike", "5", "b"], | |
| 31 | + ["Jake", "33", "c"], | |
| 32 | + ["Jill", "30", "d"], | |
| 33 | + ], | |
| 34 | + "errors": [], | |
| 35 | + "meta": { | |
| 36 | + "delimiter": ",", | |
| 37 | + "linebreak": "\n", | |
| 38 | + "aborted": false, | |
| 39 | + "truncated": false, | |
| 40 | + "cursor": 72 | |
| 41 | + } | |
| 42 | + } | |
| 43 | + */ | |
| 44 | + this.readFile((output) => { | |
| 45 | + // const sample = Papa.parse(output, { preview: 2, skipEmptyLines: true }) | |
| 46 | + const csv = Papa.parse(output, { skipEmptyLines: true }) | |
| 47 | + this.$emit('parse', csv) | |
| 48 | + this.$refs.input.value = '' | |
| 49 | + }) | |
| 50 | + }, | |
| 51 | + readFile (callback) { | |
| 52 | + let file = this.$refs.csv.files[0] | |
| 53 | + if (file) { | |
| 54 | + let reader = new FileReader() | |
| 55 | + reader.readAsText(file, 'UTF-8') | |
| 56 | + reader.onload = function (evt) { | |
| 57 | + callback(evt.target.result) | |
| 58 | + } | |
| 59 | + reader.onerror = function () { | |
| 60 | + } | |
| 61 | + } | |
| 62 | + } | |
| 63 | + }, | |
| 64 | + render () { | |
| 65 | + const randomId = +new Date() | |
| 66 | + return <div style="height: 50px;"> | |
| 67 | + <label for={randomId} class="ant-btn ant-btn-primary ant-btn-sm">请选择CSV文件</label> | |
| 68 | + {/* <input id={randomId} style="visibility:hidden;" type="file"></input> */} | |
| 69 | + <input ref="input" id={randomId} ref="csv" type="file" onChange={this.validFileMimeType} style="visibility:hidden;" name="csv">xxxx</input> | |
| 70 | + </div> | |
| 71 | + } | |
| 72 | +} | ... | ... |
front-end/h5/src/components/core/support/excel.js
| ... | ... | @@ -4,7 +4,7 @@ |
| 4 | 4 | |
| 5 | 5 | import Spreadsheet from 'x-data-spreadsheet' |
| 6 | 6 | import Parser from '../../../utils/excel-parser' |
| 7 | - | |
| 7 | +import CsvImport from './csv-import' | |
| 8 | 8 | // const getDefaultTableMatrix = () => [ |
| 9 | 9 | // [1, 2, 3, 4], |
| 10 | 10 | // [5, 6, 7, 8], |
| ... | ... | @@ -34,8 +34,24 @@ export default { |
| 34 | 34 | } |
| 35 | 35 | } |
| 36 | 36 | }, |
| 37 | + methods: { | |
| 38 | + parseCSV (csv) { | |
| 39 | + const sheetData = Parser.binaryMatrix2excel(csv.data) | |
| 40 | + this.$emit('change', csv.data) | |
| 41 | + this.refreshSheet({ rows: sheetData }) | |
| 42 | + }, | |
| 43 | + refreshSheet (data) { | |
| 44 | + this.sheet.loadData(data) | |
| 45 | + this.sheet.reRender() | |
| 46 | + } | |
| 47 | + }, | |
| 37 | 48 | render () { |
| 38 | - return <div id="excel-wrapper" ref="excel" style="margin-right: 12px;width: 100%;overflow: scroll"></div> | |
| 49 | + return <div> | |
| 50 | + <span>方案1: 选择导入 csv 文件</span> | |
| 51 | + <CsvImport onParse={this.parseCSV} /> | |
| 52 | + <span>方案2: 直接编辑 Excel</span> | |
| 53 | + <div id="excel-wrapper" ref="excel" style="margin-right: 12px;width: 100%;overflow: scroll"></div> | |
| 54 | + </div> | |
| 39 | 55 | }, |
| 40 | 56 | mounted () { |
| 41 | 57 | const ele = this.$refs.excel |
| ... | ... | @@ -48,17 +64,17 @@ export default { |
| 48 | 64 | // width: () => ele.getBoundingClientRect().width |
| 49 | 65 | // } |
| 50 | 66 | } |
| 51 | - new Spreadsheet(ele, options) | |
| 52 | - .loadData({ | |
| 53 | - rows: this.innerItems | |
| 54 | - }) // load data | |
| 55 | - .change(excelData => { | |
| 56 | - // console.log('----------') | |
| 57 | - // console.log(excelData) | |
| 58 | - // console.log(this.formatter(excelData)) | |
| 59 | - // console.log('----------') | |
| 60 | - this.$emit('change', this.formatter(excelData) /** BinaryMatrix */) | |
| 61 | - // save data to db | |
| 62 | - }) | |
| 67 | + this.sheet = new Spreadsheet(ele, options) | |
| 68 | + this.sheet.loadData({ | |
| 69 | + rows: this.innerItems | |
| 70 | + }).change(excelData => { | |
| 71 | + debugger | |
| 72 | + // console.log('----------') | |
| 73 | + // console.log(excelData) | |
| 74 | + // console.log(this.formatter(excelData)) | |
| 75 | + // console.log('----------') | |
| 76 | + this.$emit('change', this.formatter(excelData) /** BinaryMatrix */) | |
| 77 | + // save data to db | |
| 78 | + }) | |
| 63 | 79 | } |
| 64 | 80 | } | ... | ... |
front-end/h5/src/components/plugins/lbp-table.js
| 1 | 1 | // https://github.com/luban-h5-components/plugin-common-props |
| 2 | 2 | import PropTypes from '@luban-h5/plugin-common-props' |
| 3 | -import { addListener as addResizeListener, removeListener } from 'resize-detector' | |
| 3 | +import { addListener as addResizeListener, removeListener as removeResizeListener } from 'resize-detector' | |
| 4 | 4 | import './styles/table.scss' |
| 5 | 5 | |
| 6 | 6 | function sum (arr = [], key) { |
| ... | ... | @@ -27,10 +27,10 @@ export default { |
| 27 | 27 | freezeCount: PropTypes.number({ label: '冻结列数(px)', defaultValue: 0 }), |
| 28 | 28 | dataset: PropTypes.excel({ |
| 29 | 29 | defaultValue: () => [ |
| 30 | - [ '列A', '列B', '列C'], | |
| 31 | - [ '————', '————', '————'], | |
| 32 | - [ '————', '————', '————'], | |
| 33 | - [ '————', '————', '————'] | |
| 30 | + ['列A', '列B', '列C'], | |
| 31 | + ['————', '————', '————'], | |
| 32 | + ['————', '————', '————'], | |
| 33 | + ['————', '————', '————'] | |
| 34 | 34 | ] |
| 35 | 35 | }) |
| 36 | 36 | }, |
| ... | ... | @@ -94,6 +94,12 @@ export default { |
| 94 | 94 | this.mainTableEle = root.querySelector('.main-table-wrapper > table') |
| 95 | 95 | this.fixedTableWrapperEle = root.querySelector('.fixed-table-wrapper') |
| 96 | 96 | this.fixedTableEle = root.querySelector('.left-table') |
| 97 | + }, | |
| 98 | + __resizeHandler () { | |
| 99 | + this.setTableWidth() | |
| 100 | + if (this.freezeCount) { | |
| 101 | + this.setFixedTableStyle() | |
| 102 | + } | |
| 97 | 103 | } |
| 98 | 104 | }, |
| 99 | 105 | |
| ... | ... | @@ -101,11 +107,9 @@ export default { |
| 101 | 107 | this.initElements() |
| 102 | 108 | this.setTableWidth() |
| 103 | 109 | this.setFixedTableStyle() |
| 104 | - addResizeListener(this.$refs.lbpTable, () => { | |
| 105 | - this.setTableWidth() | |
| 106 | - if (this.freezeCount) { | |
| 107 | - this.setFixedTableStyle() | |
| 108 | - } | |
| 109 | - }) | |
| 110 | + addResizeListener(this.$refs.lbpTable, this.__resizeHandler) | |
| 111 | + }, | |
| 112 | + destroy () { | |
| 113 | + removeResizeListener(this.$el, this.__resizeHandler) | |
| 110 | 114 | } |
| 111 | 115 | } | ... | ... |
front-end/h5/yarn.lock
| ... | ... | @@ -3926,6 +3926,13 @@ eslint-module-utils@^2.4.0: |
| 3926 | 3926 | debug "^2.6.8" |
| 3927 | 3927 | pkg-dir "^2.0.0" |
| 3928 | 3928 | |
| 3929 | +eslint-plugin-cypress@^2.11.1: | |
| 3930 | + version "2.11.1" | |
| 3931 | + resolved "https://registry.npm.taobao.org/eslint-plugin-cypress/download/eslint-plugin-cypress-2.11.1.tgz#a945e2774b88211e2c706a059d431e262b5c2862" | |
| 3932 | + integrity sha1-qUXid0uIIR4scGoFnUMeJitcKGI= | |
| 3933 | + dependencies: | |
| 3934 | + globals "^11.12.0" | |
| 3935 | + | |
| 3929 | 3936 | eslint-plugin-es@^1.3.1: |
| 3930 | 3937 | version "1.4.1" |
| 3931 | 3938 | resolved "https://registry.npm.taobao.org/eslint-plugin-es/download/eslint-plugin-es-1.4.1.tgz#12acae0f4953e76ba444bfd1b2271081ac620998" |
| ... | ... | @@ -4922,7 +4929,7 @@ glob@^7.0.3, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4: |
| 4922 | 4929 | once "^1.3.0" |
| 4923 | 4930 | path-is-absolute "^1.0.0" |
| 4924 | 4931 | |
| 4925 | -globals@^11.0.1, globals@^11.1.0, globals@^11.7.0: | |
| 4932 | +globals@^11.0.1, globals@^11.1.0, globals@^11.12.0, globals@^11.7.0: | |
| 4926 | 4933 | version "11.12.0" |
| 4927 | 4934 | resolved "https://registry.npm.taobao.org/globals/download/globals-11.12.0.tgz#ab8795338868a0babd8525758018c2a7eb95c42e" |
| 4928 | 4935 | integrity sha1-q4eVM4hooLq9hSV1gBjCp+uVxC4= |
| ... | ... | @@ -7937,6 +7944,11 @@ pako@~1.0.5: |
| 7937 | 7944 | resolved "https://registry.npm.taobao.org/pako/download/pako-1.0.10.tgz#4328badb5086a426aa90f541977d4955da5c9732" |
| 7938 | 7945 | integrity sha1-Qyi621CGpCaqkPVBl31JVdpclzI= |
| 7939 | 7946 | |
| 7947 | +papaparse@^5.2.0: | |
| 7948 | + version "5.2.0" | |
| 7949 | + resolved "https://registry.npm.taobao.org/papaparse/download/papaparse-5.2.0.tgz#97976a1b135c46612773029153dc64995caa3b7b" | |
| 7950 | + integrity sha1-l5dqGxNcRmEncwKRU9xkmVyqO3s= | |
| 7951 | + | |
| 7940 | 7952 | parallel-transform@^1.1.0: |
| 7941 | 7953 | version "1.2.0" |
| 7942 | 7954 | resolved "https://registry.npm.taobao.org/parallel-transform/download/parallel-transform-1.2.0.tgz#9049ca37d6cb2182c3b1d2c720be94d14a5814fc" | ... | ... |