vxe-table 3.12.10-beta.21 → 3.12.10-beta.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,7 @@ import { parseFile, formatText, eqEmptyValue } from '../../../ui/src/utils'
5
5
  import { createHtmlPage, getExportBlobByContent } from './util'
6
6
  import { warnLog, errLog } from '../../../ui/src/log'
7
7
 
8
- import type { VxeTablePropTypes, VxeGridConstructor, VxeTableDefines, TableReactData, TableInternalData, GridReactData, VxeColumnPropTypes, VxeTableConstructor } from '../../../../types'
8
+ import type { VxeTablePropTypes, VxeGridConstructor, VxeTableDefines, TableReactData, VxeTablePrivateMethods, TableInternalData, GridReactData, VxeColumnPropTypes, VxeTableConstructor } from '../../../../types'
9
9
 
10
10
  const { getI18n, renderer } = VxeUI
11
11
 
@@ -643,7 +643,7 @@ function handleExport ($xeTable: any, opts: VxeTablePropTypes.ExportHandleOption
643
643
  }
644
644
 
645
645
  function getElementsByTagName (elem: any, qualifiedName: any) {
646
- return elem.getElementsByTagName(qualifiedName)
646
+ return elem.getElementsByTagName(qualifiedName) as HTMLElement[]
647
647
  }
648
648
 
649
649
  function getTxtCellKey (now: any) {
@@ -659,32 +659,50 @@ function getTxtCellValue (val: any, vMaps: any) {
659
659
  return rest.replace(/^"+$/g, (qVal: any) => '"'.repeat(Math.ceil(qVal.length / 2)))
660
660
  }
661
661
 
662
- function parseCsvAndTxt (columns: any, content: any, cellSeparator: any) {
662
+ function toExportField (tableConf: {
663
+ fieldMaps: Record<string, VxeTableDefines.ColumnInfo>
664
+ titleMaps: Record<string, VxeTableDefines.ColumnInfo>
665
+ }, field: string) {
666
+ const { fieldMaps, titleMaps } = tableConf
667
+ // title 转 field
668
+ if (!fieldMaps[field]) {
669
+ const teCol = titleMaps[field]
670
+ if (teCol && teCol.field) {
671
+ field = teCol.field
672
+ }
673
+ }
674
+ return field
675
+ }
676
+
677
+ function parseCsvAndTxt (tableConf: {
678
+ fieldMaps: Record<string, VxeTableDefines.ColumnInfo>
679
+ titleMaps: Record<string, VxeTableDefines.ColumnInfo>
680
+ }, content: string, cellSeparator: string) {
663
681
  const list = content.split(enterSymbol)
664
682
  const rows: any[] = []
665
- let fields : any[] = []
683
+ let fields: string[] = []
666
684
  if (list.length) {
667
685
  const vMaps: any = {}
668
686
  const now = Date.now()
669
- list.forEach((rVal: any) => {
687
+ list.forEach((rVal) => {
670
688
  if (rVal) {
671
689
  const item: any = {}
672
- rVal = rVal.replace(/("")|(\n)/g, (text: any, dVal: any) => {
690
+ rVal = rVal.replace(/("")|(\n)/g, (text: string, dVal: string) => {
673
691
  const key = getTxtCellKey(now)
674
692
  vMaps[key] = dVal ? '"' : '\n'
675
693
  return key
676
- }).replace(/"(.*?)"/g, (text: any, cVal: any) => {
694
+ }).replace(/"(.*?)"/g, (text: string, cVal: string) => {
677
695
  const key = getTxtCellKey(now)
678
696
  vMaps[key] = replaceTxtCell(cVal, vMaps)
679
697
  return key
680
698
  })
681
- const cells = rVal.split(cellSeparator)
699
+ const cells: string[] = rVal.split(cellSeparator)
682
700
  if (!fields.length) {
683
- fields = cells.map((val: any) => getTxtCellValue(val.trim(), vMaps))
701
+ fields = cells.map((val: string) => toExportField(tableConf, getTxtCellValue(val.trim(), vMaps)))
684
702
  } else {
685
- cells.forEach((val: any, colIndex: any) => {
703
+ cells.forEach((val, colIndex) => {
686
704
  if (colIndex < fields.length) {
687
- item[fields[colIndex]] = getTxtCellValue(val, vMaps)
705
+ item[fields[colIndex]] = getTxtCellValue(val.trim(), vMaps)
688
706
  }
689
707
  })
690
708
  rows.push(item)
@@ -695,35 +713,44 @@ function parseCsvAndTxt (columns: any, content: any, cellSeparator: any) {
695
713
  return { fields, rows }
696
714
  }
697
715
 
698
- function parseCsv (columns: any, content: any) {
699
- return parseCsvAndTxt(columns, content, ',')
716
+ function parseCsv (tableConf: {
717
+ fieldMaps: Record<string, VxeTableDefines.ColumnInfo>
718
+ titleMaps: Record<string, VxeTableDefines.ColumnInfo>
719
+ }, content: string) {
720
+ return parseCsvAndTxt(tableConf, content, ',')
700
721
  }
701
722
 
702
- function parseTxt (columns: any, content: any) {
703
- return parseCsvAndTxt(columns, content, '\t')
723
+ function parseTxt (tableConf: {
724
+ fieldMaps: Record<string, VxeTableDefines.ColumnInfo>
725
+ titleMaps: Record<string, VxeTableDefines.ColumnInfo>
726
+ }, content: string) {
727
+ return parseCsvAndTxt(tableConf, content, '\t')
704
728
  }
705
729
 
706
- function parseHTML (columns: any, content: any) {
730
+ function parseHTML (tableConf: {
731
+ fieldMaps: Record<string, VxeTableDefines.ColumnInfo>
732
+ titleMaps: Record<string, VxeTableDefines.ColumnInfo>
733
+ }, content: string) {
707
734
  const domParser = new DOMParser()
708
735
  const xmlDoc = domParser.parseFromString(content, 'text/html')
709
736
  const bodyNodes = getElementsByTagName(xmlDoc, 'body')
710
- const rows : any[] = []
711
- const fields: any[] = []
737
+ const rows: any[] = []
738
+ const fields: string[] = []
712
739
  if (bodyNodes.length) {
713
740
  const tableNodes = getElementsByTagName(bodyNodes[0], 'table')
714
741
  if (tableNodes.length) {
715
742
  const theadNodes = getElementsByTagName(tableNodes[0], 'thead')
716
743
  if (theadNodes.length) {
717
- XEUtils.arrayEach(getElementsByTagName(theadNodes[0], 'tr'), (rowNode: any) => {
718
- XEUtils.arrayEach(getElementsByTagName(rowNode, 'th'), (cellNode: any) => {
719
- fields.push(cellNode.textContent)
744
+ XEUtils.arrayEach(getElementsByTagName(theadNodes[0], 'tr'), rowNode => {
745
+ XEUtils.arrayEach(getElementsByTagName(rowNode, 'th'), cellNode => {
746
+ fields.push(toExportField(tableConf, cellNode.textContent || ''))
720
747
  })
721
748
  })
722
749
  const tbodyNodes = getElementsByTagName(tableNodes[0], 'tbody')
723
750
  if (tbodyNodes.length) {
724
751
  XEUtils.arrayEach(getElementsByTagName(tbodyNodes[0], 'tr'), rowNode => {
725
- const item : any = {}
726
- XEUtils.arrayEach(getElementsByTagName(rowNode, 'td'), (cellNode: any, colIndex) => {
752
+ const item: any = {}
753
+ XEUtils.arrayEach(getElementsByTagName(rowNode, 'td'), (cellNode, colIndex) => {
727
754
  if (fields[colIndex]) {
728
755
  item[fields[colIndex]] = cellNode.textContent || ''
729
756
  }
@@ -737,25 +764,28 @@ function parseHTML (columns: any, content: any) {
737
764
  return { fields, rows }
738
765
  }
739
766
 
740
- function parseXML (columns: any, content: any) {
767
+ function parseXML (tableConf: {
768
+ fieldMaps: Record<string, VxeTableDefines.ColumnInfo>
769
+ titleMaps: Record<string, VxeTableDefines.ColumnInfo>
770
+ }, content: string) {
741
771
  const domParser = new DOMParser()
742
772
  const xmlDoc = domParser.parseFromString(content, 'application/xml')
743
773
  const sheetNodes = getElementsByTagName(xmlDoc, 'Worksheet')
744
774
  const rows: any[] = []
745
- const fields : any[] = []
775
+ const fields: string[] = []
746
776
  if (sheetNodes.length) {
747
777
  const tableNodes = getElementsByTagName(sheetNodes[0], 'Table')
748
778
  if (tableNodes.length) {
749
779
  const rowNodes = getElementsByTagName(tableNodes[0], 'Row')
750
780
  if (rowNodes.length) {
751
- XEUtils.arrayEach(getElementsByTagName(rowNodes[0], 'Cell'), (cellNode: any) => {
752
- fields.push(cellNode.textContent)
781
+ XEUtils.arrayEach(getElementsByTagName(rowNodes[0], 'Cell'), cellNode => {
782
+ fields.push(toExportField(tableConf, cellNode.textContent || ''))
753
783
  })
754
784
  XEUtils.arrayEach(rowNodes, (rowNode, index) => {
755
785
  if (index) {
756
- const item : any = {}
786
+ const item: any = {}
757
787
  const cellNodes = getElementsByTagName(rowNode, 'Cell')
758
- XEUtils.arrayEach(cellNodes, (cellNode: any, colIndex: any) => {
788
+ XEUtils.arrayEach(cellNodes, (cellNode, colIndex) => {
759
789
  if (fields[colIndex]) {
760
790
  item[fields[colIndex]] = cellNode.textContent
761
791
  }
@@ -769,51 +799,56 @@ function parseXML (columns: any, content: any) {
769
799
  return { fields, rows }
770
800
  }
771
801
 
772
- /**
773
- * 检查导入的列是否完整
774
- * @param {Array} fields 字段名列表
775
- * @param {Array} rows 数据列表
776
- */
777
- function checkImportData (columns: any[], fields: any[]) {
778
- const tableFields: any[] = []
779
- columns.forEach((column) => {
802
+ function handleImport ($xeTable: VxeTableConstructor & VxeTablePrivateMethods, content: any, opts: any) {
803
+ const internalData = $xeTable as unknown as TableInternalData
804
+
805
+ const { tableFullColumn, _importResolve, _importReject } = internalData
806
+ let rest: {
807
+ fields: string[];
808
+ rows: any[];
809
+ } = { fields: [], rows: [] }
810
+ const tableFieldMaps: Record<string, VxeTableDefines.ColumnInfo> = {}
811
+ const tableTitleMaps: Record<string, VxeTableDefines.ColumnInfo> = {}
812
+ tableFullColumn.forEach((column) => {
780
813
  const field = column.field
814
+ const title = column.getTitle()
781
815
  if (field) {
782
- tableFields.push(field)
816
+ tableFieldMaps[field] = column
817
+ }
818
+ if (title) {
819
+ tableTitleMaps[column.getTitle()] = column
783
820
  }
784
821
  })
785
- return fields.some(field => tableFields.indexOf(field) > -1)
786
- }
787
-
788
- function handleImport ($xetable: any, content: any, opts: any) {
789
- const { tableFullColumn, _importResolve, _importReject } = $xetable
790
- let rest: any = { fields: [], rows: [] }
822
+ const tableConf = {
823
+ fieldMaps: tableFieldMaps,
824
+ titleMaps: tableTitleMaps
825
+ }
791
826
  switch (opts.type) {
792
827
  case 'csv':
793
- rest = parseCsv(tableFullColumn, content)
828
+ rest = parseCsv(tableConf, content)
794
829
  break
795
830
  case 'txt':
796
- rest = parseTxt(tableFullColumn, content)
831
+ rest = parseTxt(tableConf, content)
797
832
  break
798
833
  case 'html':
799
- rest = parseHTML(tableFullColumn, content)
834
+ rest = parseHTML(tableConf, content)
800
835
  break
801
836
  case 'xml':
802
- rest = parseXML(tableFullColumn, content)
837
+ rest = parseXML(tableConf, content)
803
838
  break
804
839
  }
805
840
  const { fields, rows } = rest
806
- const status = checkImportData(tableFullColumn, fields)
841
+ const status = fields.some(field => tableFieldMaps[field] || tableTitleMaps[field])
807
842
  if (status) {
808
- $xetable.createData(rows)
843
+ $xeTable.createData(rows)
809
844
  .then((data: any) => {
810
845
  let loadRest
811
846
  if (opts.mode === 'insert' || opts.mode === 'insertBottom') {
812
- loadRest = $xetable.insertAt(data, -1)
847
+ loadRest = $xeTable.insertAt(data, -1)
813
848
  } if (opts.mode === 'insertTop') {
814
- loadRest = $xetable.insert(data)
849
+ loadRest = $xeTable.insert(data)
815
850
  } else {
816
- loadRest = $xetable.reloadData(data)
851
+ loadRest = $xeTable.reloadData(data)
817
852
  }
818
853
  if (opts.message !== false) {
819
854
  // 检测弹窗模块