index.vue 12 KB

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