index.vue 10 KB

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