BodyTd.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <script setup lang="ts">
  2. import { VbUtil } from "@@/vb-dom"
  3. import symbolKeys from "@@@/table/symbol"
  4. import configs from "@/core/config"
  5. import type { Header } from "@@@/table/models"
  6. const props = withDefaults(
  7. defineProps<{
  8. row: any
  9. column: Header
  10. index?: number
  11. rowspan?: string
  12. isTree?: boolean
  13. depth?: number
  14. }>(),
  15. {
  16. isTree: false,
  17. iconField: "name",
  18. leafField: "isLeaf",
  19. intervalLeft: 10,
  20. depth: 0
  21. }
  22. )
  23. const propsOpts = inject(symbolKeys.bodyTd, {
  24. isExpand: false,
  25. hasChildren: false,
  26. parentField: "parent_id",
  27. childrenField: "children",
  28. iconField: "name",
  29. leafField: "isLeaf",
  30. expandDepth: 0,
  31. intervalLeft: 10,
  32. isLazy: false
  33. })
  34. const tableBox = inject(symbolKeys.tableBox) as Ref<HTMLElement>
  35. const row = toRef(props.row)
  36. const isLeaf = computed(() => {
  37. return propsOpts.isLazy
  38. ? toBoolean(row.value[propsOpts.leafField])
  39. : !(row.value[propsOpts.childrenField] && row.value[propsOpts.childrenField].length > 0)
  40. })
  41. const tooltip = computed(() => {
  42. if (typeof props.column.tooltip == "string") {
  43. return props.column.tooltip
  44. } else if (props.column.tooltip) {
  45. return "primary"
  46. }
  47. return undefined
  48. })
  49. const tdStyle = computed(() => {
  50. const style: any = {}
  51. if (props.column.align) {
  52. style.textAlign = props.column.align
  53. }
  54. if (props.isTree && props.column.field == propsOpts.iconField) {
  55. style.textAlign = "left"
  56. }
  57. return style
  58. })
  59. const tdClass = computed(() => {
  60. let classStr = ""
  61. if (props.column.tdClass) {
  62. classStr += props.column.tdClass
  63. }
  64. if (props.isTree && props.column.field == propsOpts.iconField) {
  65. classStr += " tree-icon"
  66. }
  67. return classStr
  68. })
  69. const onToggle = () => {
  70. if (!isLeaf.value) {
  71. VbUtil.EventHandlerUtil.trigger(tableBox.value, "vbtable.row.tree-toggle", row.value)
  72. }
  73. }
  74. function init() {
  75. //
  76. }
  77. onMounted(init)
  78. </script>
  79. <template>
  80. <td :style="tdStyle" :class="tdClass" :rowspan="rowspan" :name="`td-${column.field}`">
  81. <span
  82. v-if="isTree && column.field == propsOpts.iconField"
  83. @click="onToggle"
  84. class="me-2"
  85. :style="`margin-left: ${propsOpts.intervalLeft * props.depth}px!important`">
  86. <template v-if="propsOpts.hasChildren || (!isLeaf && propsOpts.isLazy)">
  87. <VbIcon
  88. v-if="propsOpts.isLazy ? false : propsOpts.isExpand"
  89. icon-name="minus-folder"
  90. icon-type="duotone"
  91. class="fs-2 text-primary icon"
  92. style="transform: translateY(3px)"></VbIcon>
  93. <VbIcon
  94. v-else
  95. icon-name="add-folder"
  96. icon-type="duotone"
  97. class="fs-2 text-primary icon"
  98. style="transform: translateY(3px)"></VbIcon>
  99. </template>
  100. <template v-else>
  101. <VbIcon
  102. icon-name="folder"
  103. icon-type="duotone"
  104. class="fs-2 text-primary"
  105. style="transform: translateY(3px)"></VbIcon>
  106. </template>
  107. </span>
  108. <template v-if="$slots[column.field]">
  109. <slot :name="`${column.field}`" :row="row"></slot>
  110. </template>
  111. <template v-else-if="column.field == configs.TABLE_INDEX_FIELD">
  112. {{ index }}
  113. </template>
  114. <template v-else-if="tooltip">
  115. <vb-tooltip
  116. :effect="tooltip"
  117. :content="row[column.field]"
  118. :auto-hide="true"
  119. :delay="500"></vb-tooltip>
  120. </template>
  121. <template v-else>
  122. {{ row[column.field] }}
  123. </template>
  124. </td>
  125. </template>