index.vue 12 KB

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