index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. return config
  171. },
  172. dataFormatting (config, data) {
  173. config.option.tableData = data?.data && data.data.length > 0 ? data.data : []
  174. const filteredData = {}
  175. const columnData = data?.columnData || {}
  176. const dimensionFieldList = config.dataSource.dimensionFieldList || []
  177. if (config.dataSource.dimensionFieldList && config.dataSource.dimensionFieldList.length > 0) {
  178. // 根据config.dataSource.dimensionFieldList 数据的顺序将表格列顺序调整,使其初始化的时候,顺序和组件配置面板中的一致
  179. const sortedColumnData = {}
  180. dimensionFieldList.forEach((item, index) => {
  181. sortedColumnData[item] = columnData[item]
  182. })
  183. Object?.keys(sortedColumnData).forEach(key => {
  184. if (
  185. config.dataSource.dimensionFieldList.includes(sortedColumnData[key]?.alias)
  186. ) {
  187. filteredData[key] = sortedColumnData[key]
  188. }
  189. })
  190. config.option.columnData = filteredData
  191. } else {
  192. config.option.columnData = columnData
  193. }
  194. this.columnData = cloneDeep(config.option.columnData)
  195. this.updateKey = new Date().getTime()
  196. return config
  197. },
  198. // 将样式字符串转成对象, 用于自定义主题,表格头部样式
  199. headerCellStyleToObj () {
  200. const str = this.themeJson.themeCss
  201. // 匹配包含header-cell-style的样式字符串
  202. // 匹配包含header-cell-style的样式字符串
  203. const regex = /\.header-cell-style\{([^{}]+)\}/
  204. const match = str.match(regex)
  205. if (match) {
  206. // 将样式字符串转成对象
  207. const styleStr = match[1].trim().replace(/\s*;\s*$/, '') // 去掉末尾的空格和分号
  208. // const styleObj = {};
  209. styleStr.split(';').forEach(s => {
  210. const [key, value] = s.split(':')
  211. if (key && value) {
  212. // 判断是否为空字符串
  213. this.headerCellStyleObj[key.trim()] = value.trim()
  214. }
  215. })
  216. } else {
  217. this.headerCellStyleObj = {}
  218. }
  219. },
  220. // 将样式字符串转成对象, 用于自定义主题,表格主体样式
  221. cellStyleToObj () {
  222. const str = this.themeJson.themeCss
  223. // 匹配包含header-cell-style的样式字符串
  224. // 匹配包含header-cell-style的样式字符串
  225. const regex = /\.cell-style\{([^{}]+)\}/
  226. const match = str.match(regex)
  227. if (match) {
  228. // 将样式字符串转成对象
  229. const styleStr = match[1].trim().replace(/\s*;\s*$/, '') // 去掉末尾的空格和分号
  230. styleStr.split(';').forEach(s => {
  231. const [key, value] = s.split(':')
  232. if (key && value) {
  233. // 判断是否为空字符串
  234. this.cellStyleObj[key.trim()] = value.trim()
  235. }
  236. })
  237. } else {
  238. this.cellStyleObj = {}
  239. }
  240. },
  241. getLabel (data) {
  242. return data.remark || data.originalColumn
  243. }
  244. }
  245. }
  246. </script>
  247. <style lang="scss" scoped>
  248. .bs-design-wrap {
  249. position: relative;
  250. width: 100%;
  251. height: 100%;
  252. background-color: transparent;
  253. border-radius: 4px;
  254. box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.1);
  255. box-sizing: border-box;
  256. }
  257. ::v-deep .el-table {
  258. // height: 100%;
  259. background-color: transparent;
  260. }
  261. ::v-deep .el-table tr {
  262. background-color: transparent;
  263. }
  264. // ::v-deep .el-table th.gutter {
  265. // border-bottom: 2px solid var(--bs-el-color-primary) !important;
  266. // }
  267. ::v-deep .el-table__body {
  268. height: 100%;
  269. }
  270. ::v-deep .el-table .el-table__header tr {
  271. background-color: transparent;
  272. }
  273. ::v-deep tr.el-table__row--striped {
  274. z-index: 1;
  275. /*将容器的 z-index 设为 1,以便其在蒙版之上*/
  276. position: relative;
  277. /*设置容器为相对定位*/
  278. opacity: 0.9;
  279. }
  280. ::v-deep tr.el-table__row--striped::before {
  281. position: absolute;
  282. /*设置蒙版为绝对定位*/
  283. top: 0;
  284. left: 0;
  285. width: 100%;
  286. height: 100%;
  287. background-color: rgba(0, 0, 0, 0.2);
  288. /*设置半透明的灰色背景色*/
  289. z-index: 2;
  290. /*将蒙版的 z-index 设为 2,以便其覆盖在容器之上*/
  291. }
  292. ::v-deep .overlay {
  293. position: absolute;
  294. /*设置蒙版为绝对定位*/
  295. top: 0;
  296. left: 0;
  297. width: 100%;
  298. height: 100%;
  299. background-color: rgba(0, 0, 0, 0.2) !important;
  300. /*设置半透明的灰色背景色*/
  301. z-index: 2;
  302. /*将蒙版的 z-index 设为 2,以便其覆盖在容器之上*/
  303. }
  304. ::v-deep .cell.el-tooltip {
  305. z-index: 3;
  306. min-width: 50px;
  307. white-space: nowrap;
  308. position: inherit;
  309. }
  310. ::v-deep .el-table {
  311. .el-table__cell {
  312. border-bottom: none !important;
  313. }
  314. &:before {
  315. display: none !important;
  316. }
  317. th.gutter,
  318. colgroup.gutter {
  319. width: 0px !important; //此处的宽度值,对应你自定义滚动条的宽度即可
  320. }
  321. }
  322. // 关键css代码
  323. ::v-deep .el-table__header colgroup col[name="gutter"] {
  324. display: table-cell !important;
  325. }
  326. ::v-deep .el-table__body-wrapper::-webkit-scrollbar {
  327. width: 10px; // 横向滚动条
  328. height: 10px; // 纵向滚动条 必写
  329. background-color: transparent;
  330. }
  331. // 滚动条的滑块
  332. ::v-deep .el-table__body-wrapper::-webkit-scrollbar-thumb {
  333. background-color: #9093994D;
  334. border-radius: 5px;
  335. &:hover {
  336. background-color: #90939980;
  337. }
  338. }
  339. </style>