index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <template>
  2. <div
  3. v-loading="config.loading"
  4. element-loading-text="数据加载中"
  5. :element-loading-background="loadingBackground"
  6. style="width: 100%;height: 100%"
  7. class="bs-design-wrap "
  8. >
  9. <!-- <span style="color: aliceblue;font-size: 40px;">
  10. {{ columnData }}
  11. </span> -->
  12. <!-- :border="this.config.customize.border" -->
  13. <el-table
  14. :id="config.code"
  15. ref="table"
  16. :key="updateKey"
  17. class="custom-table"
  18. height="100%"
  19. :stripe="config.customize.stripe"
  20. :data="config.option.tableData"
  21. :header-cell-style="headerCellStyle"
  22. :cell-style="cellStyle"
  23. :row-style="rowStyle"
  24. @row-click="rowClick"
  25. >
  26. <el-table-column
  27. v-for="(col,index) in columnData"
  28. :key="index"
  29. show-overflow-tooltip
  30. :prop="col.alias"
  31. :label="getLabel(col)"
  32. align="center"
  33. />
  34. </el-table>
  35. </div>
  36. </template>
  37. <script>
  38. import { EventBus } from 'data-room-ui/js/utils/eventBus'
  39. import commonMixins from 'data-room-ui/js/mixins/commonMixins'
  40. import paramsMixins from 'data-room-ui/js/mixins/paramsMixins'
  41. import linkageMixins from 'data-room-ui/js/mixins/linkageMixins'
  42. import cloneDeep from 'lodash/cloneDeep'
  43. import { settingToTheme } from 'data-room-ui/js/utils/themeFormatting'
  44. export default {
  45. name: 'TableChart',
  46. mixins: [paramsMixins, commonMixins, linkageMixins],
  47. props: {
  48. id: {
  49. type: String,
  50. default: ''
  51. },
  52. config: {
  53. type: Object,
  54. default: () => ({})
  55. }
  56. },
  57. data () {
  58. return {
  59. updateKey: 0,
  60. headerCellStyleObj: {
  61. backgroundColor: 'transparent'
  62. },
  63. cellStyleObj: {
  64. backgroundColor: 'transparent'
  65. },
  66. columnData: {},
  67. // 第一次获取
  68. isInit: true
  69. }
  70. },
  71. computed: {
  72. headerCellStyle () {
  73. const headerBackgroundColor = {
  74. light: '#ffffff',
  75. dark: 'transparent'
  76. }
  77. if (document.getElementById(this.config.code)?.querySelector('tr')) {
  78. document
  79. .getElementById(this.config.code)
  80. .querySelector('tr').style.backgroundColor =
  81. this.customTheme !== 'custom'
  82. ? this.config.customize.headerBackgroundColor || headerBackgroundColor[this.customTheme]
  83. : this.headerCellStyleObj.backgroundColor
  84. }
  85. const style = {
  86. height: '48px',
  87. borderBottom: 'solid 2px #007aff',
  88. backgroundColor:
  89. this.customTheme !== 'custom'
  90. ? this.config.customize.headerBackgroundColor || headerBackgroundColor[this.customTheme]
  91. : this.headerCellStyleObj.backgroundColor,
  92. color:
  93. this.customTheme === 'light'
  94. ? '#000000'
  95. : this.config.customize.headerFontColor || '#000000',
  96. fontSize: this.config.customize.headerFontSize + 'px' || '14px'
  97. }
  98. return style
  99. }
  100. },
  101. watch: {
  102. activeItemConfig (val) {
  103. console.dir(val)
  104. }
  105. },
  106. created () { },
  107. mounted () {
  108. this.chartInit()
  109. // this.config.option?.columnData 对象的key 根据 list 对应的key 来排序
  110. EventBus.$on('dragSelectChange', (val) => {
  111. if (val.length > 0) {
  112. const sortedColumnData = {}
  113. const columnData = cloneDeep(this.config.option?.columnData)
  114. val.forEach((item, index) => {
  115. sortedColumnData[item] = columnData[item]
  116. })
  117. this.columnData = sortedColumnData
  118. this.updateKey = new Date().getTime()
  119. }
  120. })
  121. },
  122. beforeDestroy () {
  123. EventBus.$off('dragSelectChange')
  124. },
  125. methods: {
  126. cellStyle ({ row, column, rowIndex, columnIndex }) {
  127. const bodyBackgroundColor = {
  128. light: '#ffffff',
  129. dark: 'transparent'
  130. }
  131. const initColor = this.customTheme === 'light' ? '#000000' : '#ffffff'
  132. const style = {
  133. backgroundColor: '',
  134. color: this.config.customize.bodyFontColor || initColor,
  135. fontSize: this.config.customize.bodyFontSize + 'px' || '14px'
  136. }
  137. // 如果设置了奇偶行的背景颜色,则以奇偶行的背景颜色为主
  138. if (rowIndex % 2 && this.config.customize.evenRowBackgroundColor) {
  139. style.backgroundColor = this.config.customize.evenRowBackgroundColor
  140. } else if (!(rowIndex % 2) && this.config.customize.oddRowBackgroundColor) {
  141. style.backgroundColor = this.config.customize.oddRowBackgroundColor
  142. } else {
  143. style.backgroundColor = this.config.customize.bodyBackgroundColor || bodyBackgroundColor[this.customTheme]
  144. }
  145. return style
  146. },
  147. rowStyle ({ row, rowIndex }) {
  148. if (rowIndex % 2) {
  149. return {
  150. backgroundColor: this.config.customize.evenRowBackgroundColor
  151. }
  152. } else {
  153. return {
  154. backgroundColor: this.config.customize.oddRowBackgroundColor
  155. }
  156. }
  157. },
  158. // 表格点击事件
  159. rowClick (row) {
  160. this.linkage(row)
  161. },
  162. changeStyle (config) {
  163. config = { ...this.config, ...config }
  164. // 样式改变时更新主题配置
  165. config.theme = settingToTheme(cloneDeep(config), this.customTheme)
  166. this.changeChartConfig(config)
  167. if (config.code === this.activeCode) {
  168. this.changeActiveItemConfig(config)
  169. }
  170. // const config = cloneDeep(oldConfig)
  171. // if (this.customTheme === 'custom') {
  172. // this.headerCellStyleToObj()
  173. // this.cellStyleToObj()
  174. // }
  175. // if (this.customTheme === 'custom') {
  176. // this.headerCellStyleToObj()
  177. // this.cellStyleToObj()
  178. // }
  179. // if (config.customize.stripe) {
  180. // const trs = document
  181. // .getElementById(this.config.code)
  182. // ?.querySelectorAll('tr.el-table__row--striped')
  183. // if (trs) {
  184. // trs.forEach(tr => {
  185. // tr.style.opacity = '0.9'
  186. // // 透明度
  187. // // const overlay = document.createElement("div");
  188. // // overlay.classList.add("overlay");
  189. // // // 将蒙版添加到容器中
  190. // // tr.appendChild(overlay);
  191. // })
  192. // }
  193. // } else {
  194. // const trs = document
  195. // .getElementById(config.code)
  196. // ?.querySelectorAll('tr.el-table__row--striped')
  197. // if (trs) {
  198. // trs.forEach(tr => {
  199. // tr.style.opacity = '1'
  200. // // 透明度
  201. // // const overlay = document.createElement("div");
  202. // // overlay.classList.add("overlay");
  203. // // // 将蒙版添加到容器中
  204. // // tr.appendChild(overlay);
  205. // })
  206. // }
  207. // // document.querySelectorAll(".overlay").forEach(overlay => {
  208. // // overlay.remove();
  209. // // });
  210. // }
  211. // this.chartInit();
  212. // if (config.customize.evenRowBackgroundColor && !config.customize.oddRowBackgroundColor) {
  213. // config.customize.oddRowBackgroundColor = config.customize.bodyBackgroundColor
  214. // } else if (!config.customize.evenRowBackgroundColor && config.customize.oddRowBackgroundColor) {
  215. // config.customize.evenRowBackgroundColor = config.customize.bodyBackgroundColor
  216. // } else if (!(!config.customize.evenRowBackgroundColor && !config.customize.oddRowBackgroundColor)) {
  217. // config.customize.bodyBackgroundColor = ''
  218. // }
  219. // this.updateKey = new Date().getTime()
  220. return config
  221. },
  222. dataFormatting (config, data) {
  223. config.option.tableData = data?.data
  224. const filteredData = {}
  225. const columnData = data?.columnData || {}
  226. const dimensionFieldList = config.dataSource.dimensionFieldList || []
  227. if (config.dataSource.dimensionFieldList && config.dataSource.dimensionFieldList.length > 0) {
  228. // 根据config.dataSource.dimensionFieldList 数据的顺序将表格列顺序调整,使其初始化的时候,顺序和组件配置面板中的一致
  229. const sortedColumnData = {}
  230. dimensionFieldList.forEach((item, index) => {
  231. sortedColumnData[item] = columnData[item]
  232. })
  233. Object?.keys(sortedColumnData).forEach(key => {
  234. if (
  235. config.dataSource.dimensionFieldList.includes(sortedColumnData[key]?.alias)
  236. ) {
  237. filteredData[key] = sortedColumnData[key]
  238. }
  239. })
  240. config.option.columnData = filteredData
  241. } else {
  242. config.option.columnData = columnData
  243. }
  244. this.columnData = cloneDeep(config.option.columnData)
  245. this.updateKey = new Date().getTime()
  246. return config
  247. },
  248. // 将样式字符串转成对象, 用于自定义主题,表格头部样式
  249. headerCellStyleToObj () {
  250. const str = this.themeJson.themeCss
  251. // 匹配包含header-cell-style的样式字符串
  252. // 匹配包含header-cell-style的样式字符串
  253. const regex = /\.header-cell-style\{([^{}]+)\}/
  254. const match = str.match(regex)
  255. if (match) {
  256. // 将样式字符串转成对象
  257. const styleStr = match[1].trim().replace(/\s*;\s*$/, '') // 去掉末尾的空格和分号
  258. // const styleObj = {};
  259. styleStr.split(';').forEach(s => {
  260. const [key, value] = s.split(':')
  261. if (key && value) {
  262. // 判断是否为空字符串
  263. this.headerCellStyleObj[key.trim()] = value.trim()
  264. }
  265. })
  266. } else {
  267. this.headerCellStyleObj = {}
  268. }
  269. },
  270. // 将样式字符串转成对象, 用于自定义主题,表格主体样式
  271. cellStyleToObj () {
  272. const str = this.themeJson.themeCss
  273. // 匹配包含header-cell-style的样式字符串
  274. // 匹配包含header-cell-style的样式字符串
  275. const regex = /\.cell-style\{([^{}]+)\}/
  276. const match = str.match(regex)
  277. if (match) {
  278. // 将样式字符串转成对象
  279. const styleStr = match[1].trim().replace(/\s*;\s*$/, '') // 去掉末尾的空格和分号
  280. styleStr.split(';').forEach(s => {
  281. const [key, value] = s.split(':')
  282. if (key && value) {
  283. // 判断是否为空字符串
  284. this.cellStyleObj[key.trim()] = value.trim()
  285. }
  286. })
  287. } else {
  288. this.cellStyleObj = {}
  289. }
  290. },
  291. getLabel (data) {
  292. return data.remark || data.originalColumn
  293. }
  294. }
  295. }
  296. </script>
  297. <style lang="scss" scoped>
  298. .bs-design-wrap {
  299. position: relative;
  300. width: 100%;
  301. padding: 0 16px;
  302. height: 100%;
  303. background-color: transparent;
  304. border-radius: 4px;
  305. box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.1);
  306. box-sizing: border-box;
  307. }
  308. ::v-deep .el-table {
  309. // height: 100%;
  310. background-color: transparent;
  311. }
  312. ::v-deep .el-table tr {
  313. background-color: transparent;
  314. }
  315. // ::v-deep .el-table th.gutter {
  316. // border-bottom: 2px solid var(--bs-el-color-primary) !important;
  317. // }
  318. ::v-deep .el-table__body {
  319. height: 100%;
  320. }
  321. ::v-deep .el-table .el-table__header tr {
  322. background-color: transparent;
  323. }
  324. ::v-deep tr.el-table__row--striped {
  325. z-index: 1;
  326. /*将容器的 z-index 设为 1,以便其在蒙版之上*/
  327. position: relative;
  328. /*设置容器为相对定位*/
  329. opacity: 0.9;
  330. }
  331. ::v-deep tr.el-table__row--striped::before {
  332. position: absolute;
  333. /*设置蒙版为绝对定位*/
  334. top: 0;
  335. left: 0;
  336. width: 100%;
  337. height: 100%;
  338. background-color: rgba(0, 0, 0, 0.2);
  339. /*设置半透明的灰色背景色*/
  340. z-index: 2;
  341. /*将蒙版的 z-index 设为 2,以便其覆盖在容器之上*/
  342. }
  343. ::v-deep .overlay {
  344. position: absolute;
  345. /*设置蒙版为绝对定位*/
  346. top: 0;
  347. left: 0;
  348. width: 100%;
  349. height: 100%;
  350. background-color: rgba(0, 0, 0, 0.2) !important;
  351. /*设置半透明的灰色背景色*/
  352. z-index: 2;
  353. /*将蒙版的 z-index 设为 2,以便其覆盖在容器之上*/
  354. }
  355. ::v-deep .cell.el-tooltip {
  356. z-index: 3;
  357. min-width: 50px;
  358. white-space: nowrap;
  359. position: inherit;
  360. }
  361. ::v-deep .el-table {
  362. .el-table__cell {
  363. border-bottom: none !important;
  364. }
  365. &:before {
  366. display: none !important;
  367. }
  368. th.gutter,
  369. colgroup.gutter {
  370. width: 0px !important; //此处的宽度值,对应你自定义滚动条的宽度即可
  371. }
  372. }
  373. // 关键css代码
  374. ::v-deep .el-table__header colgroup col[name="gutter"] {
  375. display: table-cell !important;
  376. }
  377. ::v-deep .el-table__body-wrapper::-webkit-scrollbar {
  378. width: 10px; // 横向滚动条
  379. height: 10px; // 纵向滚动条 必写
  380. background-color: transparent;
  381. }
  382. // 滚动条的滑块
  383. ::v-deep .el-table__body-wrapper::-webkit-scrollbar-thumb {
  384. background-color: #9093994D;
  385. border-radius: 5px;
  386. &:hover {
  387. background-color: #90939980;
  388. }
  389. }
  390. </style>