utils.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import XEUtils from 'xe-utils'
  2. import GlobalConfig from '../v-x-e-table/src/conf'
  3. import { warnLog, errLog } from '../tools/log'
  4. let zindexIndex = 0
  5. let lastZindex = 1
  6. export function isEnableConf (conf) {
  7. return conf && conf.enabled !== false
  8. }
  9. /**
  10. * 判断值为:'' | null | undefined 时都属于空值
  11. */
  12. export function eqEmptyValue (cellValue) {
  13. return cellValue === '' || XEUtils.eqNull(cellValue)
  14. }
  15. export function getFuncText (content) {
  16. return XEUtils.isFunction(content) ? content() : (GlobalConfig.translate ? GlobalConfig.translate(content) : content)
  17. }
  18. // 获取所有的列,排除分组
  19. export function getColumnList (columns) {
  20. const result = []
  21. columns.forEach(column => {
  22. result.push(...(column.children && column.children.length ? getColumnList(column.children) : [column]))
  23. })
  24. return result
  25. }
  26. export const UtilTools = {
  27. nextZIndex () {
  28. lastZindex = GlobalConfig.zIndex + zindexIndex++
  29. return lastZindex
  30. },
  31. getLastZIndex () {
  32. return lastZindex
  33. },
  34. getColumnList,
  35. getClass (property, params) {
  36. return property ? XEUtils.isFunction(property) ? property(params) : property : ''
  37. },
  38. formatText (value, placeholder) {
  39. return '' + (value === '' || value === null || value === undefined ? (placeholder ? GlobalConfig.emptyCell : '') : value)
  40. },
  41. getCellValue (row, column) {
  42. return XEUtils.get(row, column.field)
  43. },
  44. setCellValue (row, column, value) {
  45. return XEUtils.set(row, column.field, value)
  46. },
  47. // 组装列配置
  48. assemColumn (_vm) {
  49. const { $el, $xetable, $xecolumn, columnConfig } = _vm
  50. const groupConfig = $xecolumn ? $xecolumn.columnConfig : null
  51. columnConfig.slots = _vm.$scopedSlots
  52. if (groupConfig) {
  53. if (process.env.VUE_APP_VXE_TABLE_ENV === 'development') {
  54. if ($xecolumn.$options._componentTag === 'vxe-table-column') {
  55. errLog('vxe.error.groupTag', [`<vxe-table-colgroup title=${$xecolumn.title} ...>`, `<vxe-table-column title=${$xecolumn.title} ...>`])
  56. } else if ($xecolumn.$options._componentTag === 'vxe-column') {
  57. warnLog('vxe.error.groupTag', [`<vxe-colgroup title=${$xecolumn.title} ...>`, `<vxe-column title=${$xecolumn.title} ...>`])
  58. }
  59. }
  60. if (!groupConfig.children) {
  61. groupConfig.children = []
  62. }
  63. groupConfig.children.splice([].indexOf.call($xecolumn.$el.children, $el), 0, columnConfig)
  64. } else {
  65. $xetable.staticColumns.splice([].indexOf.call($xetable.$refs.hideColumn.children, $el), 0, columnConfig)
  66. }
  67. },
  68. // 销毁列
  69. destroyColumn (_vm) {
  70. const { $xetable, columnConfig } = _vm
  71. const matchObj = XEUtils.findTree($xetable.staticColumns, column => column === columnConfig)
  72. if (matchObj) {
  73. matchObj.items.splice(matchObj.index, 1)
  74. }
  75. },
  76. hasChildrenList (item) {
  77. return item && item.children && item.children.length > 0
  78. },
  79. parseFile (file) {
  80. const name = file.name
  81. const tIndex = XEUtils.lastIndexOf(name, '.')
  82. const type = name.substring(tIndex + 1, name.length)
  83. const filename = name.substring(0, tIndex)
  84. return { filename, type }
  85. },
  86. isNumVal (num) {
  87. return !isNaN(parseFloat('' + num))
  88. }
  89. }
  90. export default UtilTools