BodyTr.vue 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <script setup lang="ts">
  2. import { VbUtil } from "@@/vb-dom"
  3. import symbolKeys from "@@@/table/symbol"
  4. import BodyTds from "@@@/table/partials/body/BodyTds.vue"
  5. const props = defineProps<{
  6. row: any
  7. index: number
  8. orgh: number
  9. }>()
  10. const propOpts = inject(symbolKeys.bodyTr, {
  11. selectedIds: ref([]),
  12. isTree: false,
  13. keyField: "",
  14. hasCheckbox: false,
  15. isMultipleCheck: false,
  16. parentField: "parent_id",
  17. childrenField: "children",
  18. iconField: "name",
  19. leafField: "isLeaf",
  20. expandDepth: 0,
  21. intervalLeft: 10,
  22. isLazy: false,
  23. lazySearch: false,
  24. tdTrClass: "text-center fs-7"
  25. })
  26. const selectedIds = toRef(propOpts.selectedIds)
  27. const tableBox = inject(symbolKeys.tableBox) as Ref<HTMLElement>
  28. const isExpand = computed(() => {
  29. return propOpts.isLazy ? true : propOpts.expandDepth > props.orgh
  30. })
  31. const hasChildren = computed(() => {
  32. return props.row[propOpts.childrenField] && props.row[propOpts.childrenField].length > 0
  33. })
  34. const getTrClass = computed(() => {
  35. let _trClass = ` ${propOpts.tdTrClass} `
  36. if (hasChildren.value) {
  37. _trClass += `hasChildren ${isExpand.value ? "tr-expand" : "tr-collapse"} `
  38. }
  39. _trClass += !propOpts.isLazy && props.orgh > propOpts.expandDepth ? "hide" : "show"
  40. return _trClass
  41. })
  42. function getParent(row: any) {
  43. return `${propOpts.isTree ? `tr_${row[propOpts.parentField]}` : ""}`
  44. }
  45. const onChange = (event: Event) => {
  46. const target = event.target as HTMLInputElement
  47. VbUtil.EventHandlerUtil.trigger(tableBox.value, "vbtable.select", {
  48. isChecked: target.checked,
  49. row: props.row
  50. })
  51. }
  52. const onRowClick = () => {
  53. VbUtil.EventHandlerUtil.trigger(tableBox.value, "vbtable.row.click", props.row)
  54. }
  55. const onDbRowClick = () => {
  56. VbUtil.EventHandlerUtil.trigger(tableBox.value, "vbtable.row.dbclick", props.row)
  57. }
  58. </script>
  59. <template>
  60. <tr
  61. :class="getTrClass"
  62. :id="`tr_${row[propOpts.keyField]}`"
  63. :data-parent="getParent(row)"
  64. @click="onRowClick"
  65. @dblclick="onDbRowClick">
  66. <td v-if="propOpts.hasCheckbox" class="check-box">
  67. <div class="form-check form-check-sm form-check-custom form-check-solid">
  68. <input
  69. class="form-check-input"
  70. type="checkbox"
  71. :checked="selectedIds.includes(row[propOpts.keyField])"
  72. :value="row[propOpts.keyField]"
  73. @change="onChange" />
  74. </div>
  75. </td>
  76. <BodyTds
  77. :row="row"
  78. :index="index"
  79. :is-tree="propOpts.isTree"
  80. :hasChildren="hasChildren"
  81. :isExpand="isExpand"
  82. :orgh="orgh">
  83. <template v-for="(_, name) in $slots" #[name]="{ row }">
  84. <slot :name="name" :row="Object.assign({}, row)" />
  85. </template>
  86. </BodyTds>
  87. </tr>
  88. </template>