index.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <template>
  2. <el-select
  3. :key="config.code"
  4. v-model="selectValue"
  5. :popper-class="'basic-component-select selct-popper-' + config.code"
  6. :class="['basic-component-select', `selct-${config.code}`]"
  7. :placeholder="`请选择${placeholder}`"
  8. filterable
  9. clearable
  10. @visible-change="visibleChange"
  11. @change="selectChange"
  12. >
  13. <el-option
  14. v-for="(option, key) in config.option.data"
  15. :key="key"
  16. :label="option[config.dataSource.dimensionField]"
  17. :value="option[config.dataSource.metricField]"
  18. />
  19. </el-select>
  20. </template>
  21. <script>
  22. import { EventBus } from 'data-room-ui/js/utils/eventBus'
  23. import commonMixins from 'data-room-ui/js/mixins/commonMixins'
  24. import linkageMixins from 'data-room-ui/js/mixins/linkageMixins'
  25. import { getDataSetDetails } from 'data-room-ui/js/api/bigScreenApi'
  26. window.dataSetFields = []
  27. export default {
  28. name: 'BasicComponentSelect',
  29. components: {},
  30. mixins: [commonMixins, linkageMixins],
  31. props: {
  32. // 组件配置
  33. config: {
  34. type: Object,
  35. default: () => ({})
  36. }
  37. },
  38. data () {
  39. return {
  40. selectValue: '',
  41. innerConfig: {}
  42. }
  43. },
  44. computed: {
  45. placeholder () {
  46. return window.dataSetFields.find(field => field.value === this.config.dataSource.dimensionField)?.label || ''
  47. }
  48. },
  49. watch: { },
  50. created () { },
  51. mounted () {
  52. this.changeStyle(this.config)
  53. EventBus.$on('changeBusinessKey', () => {
  54. window.dataSetFields = []
  55. })
  56. },
  57. beforeDestroy () {
  58. EventBus.$off('changeBusinessKey')
  59. },
  60. methods: {
  61. dataFormatting (config, data) {
  62. // 数据返回成功则赋值
  63. if (data.success) {
  64. data = data.data
  65. // 获取到后端返回的数据,有则赋值
  66. if (config.dataHandler) {
  67. try {
  68. // 此处函数处理data
  69. eval(config.dataHandler)
  70. } catch (e) {
  71. console.info(e)
  72. }
  73. }
  74. config.option.data = data
  75. config.customize.title = config.option.data[config.dataSource.dimensionField] || config.customize.title
  76. if (window.dataSetFields.length === 0) {
  77. getDataSetDetails(this.config.dataSource.businessKey).then(res => {
  78. window.dataSetFields = res.fields.map(field => {
  79. return {
  80. label: field.comment || field.fieldDesc,
  81. value: field.name || field.fieldName
  82. }
  83. })
  84. })
  85. }
  86. // 语音播报
  87. } else {
  88. // 数据返回失败则赋前端的模拟数据
  89. config.option.data = []
  90. }
  91. return config
  92. },
  93. changeStyle (config) {
  94. this.innerConfig = config
  95. // 选择器元素
  96. const selectInputEl = document.querySelector(`.selct-${config.code} .el-input__inner`)
  97. // 背景颜色
  98. selectInputEl.style.backgroundColor = config.customize.backgroundColor
  99. // 字体大小
  100. selectInputEl.style.fontSize = config.customize.fontSize + 'px'
  101. // 字体颜色
  102. selectInputEl.style.color = config.customize.fontColor
  103. // 选择器下拉框元素
  104. const selectDropdownEl = document.querySelector(`.selct-${config.code} .el-select-dropdown`)
  105. // 箭头背景颜色和下拉框背景颜色一致
  106. if (selectDropdownEl) {
  107. // 下拉框无边框
  108. selectDropdownEl.style.border = 'none'
  109. // 背景颜色
  110. selectDropdownEl.style.backgroundColor = config.customize.dropDownBackgroundColor
  111. // 下拉项hover颜色
  112. const selectDropdownItemEl = document.querySelectorAll(`.selct-${this.innerConfig.code} .el-select-dropdown__item`)
  113. selectDropdownItemEl.forEach(item => {
  114. // 下拉项字体颜色
  115. item.style.color = this.innerConfig.customize.dropDownFontColor
  116. item.addEventListener('mouseenter', () => {
  117. item.style.color = this.innerConfig.customize.dropDownHoverFontColor
  118. item.style.backgroundColor = this.innerConfig.customize.dropDownHoverBackgroundColor
  119. })
  120. item.addEventListener('mouseleave', () => {
  121. item.style.color = this.innerConfig.customize.dropDownFontColor
  122. item.style.backgroundColor = this.innerConfig.customize.dropDownBackgroundColor
  123. })
  124. })
  125. }
  126. },
  127. // 组件联动
  128. selectChange (val) {
  129. this.linkage(this.config.option.data.find(item => item[this.config.dataSource.metricField] === val))
  130. },
  131. visibleChange (val) {
  132. if (val) {
  133. // 修改下拉框背景颜色,让下拉框背景颜色和箭头背景颜色一致
  134. const selectDropdownEl = document.querySelector(`.selct-popper-${this.innerConfig.code}`)
  135. selectDropdownEl.style.color = this.innerConfig.customize.dropDownBackgroundColor
  136. // 空状态
  137. const selectDropdownEmptyEl = document.querySelector(`.selct-popper-${this.innerConfig.code} .el-select-dropdown__empty`)
  138. if (selectDropdownEmptyEl) {
  139. selectDropdownEmptyEl.style.backgroundColor = this.innerConfig.customize.dropDownBackgroundColor
  140. }
  141. // 激活项
  142. const selectDropdownItemSelectedEl = document.querySelectorAll(`.selct-popper-${this.innerConfig.code} .el-select-dropdown__item.selected`)
  143. selectDropdownItemSelectedEl.forEach(item => {
  144. item.style.color = this.innerConfig.customize.activeFontColor
  145. item.style.backgroundColor = this.innerConfig.customize.activeBackgroundColor
  146. })
  147. }
  148. // 不是激活项的还是使用背景颜色
  149. const selectDropdownItemEl = document.querySelectorAll(`.selct-popper-${this.innerConfig.code} .el-select-dropdown__item`)
  150. selectDropdownItemEl.forEach(item => {
  151. // 检查是否是激活项,不是则使用背景颜色
  152. if (!item.classList.contains('selected')) {
  153. item.style.color = this.innerConfig.customize.dropDownFontColor
  154. item.style.backgroundColor = this.innerConfig.customize.dropDownBackgroundColor
  155. }
  156. })
  157. }
  158. }
  159. }
  160. </script>
  161. <style lang="scss">
  162. .basic-component-select{
  163. color: '';
  164. .el-select-dropdown__wrap {
  165. margin-bottom: 0px !important;
  166. }
  167. .el-select-group__wrap:not(:last-of-type)::after {
  168. background-color: transparent !important;
  169. }
  170. .popper__arrow{
  171. border-bottom-color:var(--color) !important;
  172. &::after{
  173. border-bottom-color:var(--color) !important;
  174. }
  175. }
  176. }
  177. </style>
  178. <style lang="scss" scoped>
  179. .basic-component-select {
  180. width: 100%;
  181. height: 100%;
  182. ::v-deep .el-input {
  183. height: 100% !important;
  184. // 选择器输入框样式
  185. .el-input__inner {
  186. height: 100% !important;
  187. border-color: var(--bs-el-border) !important;
  188. }
  189. }
  190. .el-tag.el-tag--info {
  191. color: var(--bs-el-text) !important;
  192. }
  193. .popper__arrow {
  194. bottom: 0 !important;
  195. &:after {
  196. bottom: 0 !important;
  197. }
  198. }
  199. }
  200. </style>