ElementsMapping.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <div>
  3. <el-button v-for="(i,index) in getCellList" @click="i.click()" :key="index" v-bind="i.attrs" v-show="index < 2">{{ i.attrs.label }}</el-button>
  4. <el-dropdown>
  5. <span class="el-dropdown-link">
  6. <span class="more-btn">更多</span>
  7. </span>
  8. <template #dropdown>
  9. <el-dropdown-menu>
  10. <el-dropdown-item
  11. :class="index > 1 ? '' : 'dn'"
  12. @click="i.click()" v-for="(i,index) in getCellList"
  13. :key="index" >
  14. <span :style="i.attrs.type == 'danger' ? 'color:red' : 'color:#409eff'">{{ i.attrs.label}}</span>
  15. </el-dropdown-item>
  16. </el-dropdown-menu>
  17. </template>
  18. </el-dropdown>
  19. </div>
  20. </template>
  21. <script>
  22. import {
  23. defineComponent,
  24. getCurrentInstance,
  25. computed,
  26. ref,
  27. h,
  28. withDirectives,
  29. resolveDirective,
  30. resolveComponent
  31. } from 'vue'
  32. export default defineComponent({
  33. props: {
  34. parent: {
  35. type: Object,
  36. default () {
  37. return {}
  38. }
  39. },
  40. row: {
  41. type: Object,
  42. default () {
  43. return {}
  44. }
  45. },
  46. cellList: {
  47. type: Array,
  48. default () {
  49. return []
  50. }
  51. }
  52. },
  53. setup (props) {
  54. const { proxy } = getCurrentInstance()
  55. proxy.$.components = props.parent.$options.components
  56. proxy.$.directives = props.parent.$options.directives
  57. const elementsMapping = ref({
  58. button: 'el-button'
  59. })
  60. const getCellList = computed(() => {
  61. return props.cellList.filter((cell) => cell && Object.keys(cell).length)
  62. })
  63. const getAttrsValue = (item) => {
  64. if (!item.attrs) {
  65. item.attrs = {}
  66. }
  67. const {
  68. class: className = null,
  69. style = null,
  70. directives = null,
  71. ...attrs
  72. } = item.attrs
  73. return {
  74. class: className,
  75. style,
  76. directives,
  77. props: attrs
  78. }
  79. }
  80. if(getCellList.value.length > 3) {
  81. console.log(getCellList)
  82. return {
  83. getCellList
  84. }
  85. }
  86. return () => {
  87. return h(
  88. 'div',
  89. getCellList.value.map((cellItem) => {
  90. const comp = resolveComponent(
  91. elementsMapping.value[cellItem.el] ||
  92. cellItem.el
  93. )
  94. const attributes = getAttrsValue(cellItem)
  95. const { label, ...others } = attributes.props
  96. const { directives } = attributes
  97. let onClick
  98. if (cellItem.click) {
  99. onClick = cellItem.click.bind(props.parent, props.row)
  100. }
  101. let resultVNode = h(
  102. comp,
  103. {
  104. onClick,
  105. innerHTML: label,
  106. ...others
  107. }
  108. )
  109. if (directives) {
  110. resultVNode = withDirectives(
  111. resultVNode,
  112. [
  113. ...directives.map((directiveItem) => {
  114. return [
  115. resolveDirective(directiveItem.name),
  116. directiveItem.value,
  117. directiveItem.arg
  118. ]
  119. })
  120. ]
  121. )
  122. }
  123. return resultVNode
  124. })
  125. )
  126. }
  127. }
  128. })
  129. </script>
  130. <style>
  131. .more-btn{
  132. line-height: 34px;
  133. font-size: 14px;
  134. color:#FF9315;
  135. }
  136. .dn{
  137. display: none!important;
  138. }
  139. </style>