TableSearchFrom.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <script setup lang="ts">
  2. import { VbUtil } from "@@/vb-dom"
  3. import symbolKeys from "@@@/table/symbol"
  4. const propOpts = inject(symbolKeys.searchFrom, {
  5. searchFormRowItems: ref([]),
  6. searchFormItems: ref([]),
  7. queryParams: ref({}),
  8. searchFormSpan: 4,
  9. tableSearchBtnSpan: 1.5
  10. })
  11. const searchFormRef = ref<any>()
  12. const { searchFormRowItems, searchFormItems, queryParams } = propOpts
  13. const tableBox = inject(symbolKeys.tableBox) as Ref<HTMLElement>
  14. const formSlotSuffix = inject(symbolKeys.formSlotSuffix, "tool-form_")
  15. const _searchFormItems = computed(() => {
  16. if (propOpts.searchFormItems || propOpts.searchFormRowItems) {
  17. const items = searchFormItems.value ?? []
  18. if (
  19. !items.find((v) => {
  20. return v.field == "searchbtn"
  21. })
  22. )
  23. items.push({
  24. field: "searchbtn",
  25. component: "slot",
  26. span: 1.5
  27. })
  28. return items
  29. }
  30. return propOpts.searchFormItems
  31. })
  32. // form表单的插槽 需要tool-form_开头,并去除tool-form_传递给VbForm组件
  33. function formatterFormSlot(v: string | number): string | undefined {
  34. v = v as string
  35. if (v.search(formSlotSuffix) == 0) {
  36. return v.substring(formSlotSuffix.length)
  37. }
  38. return undefined
  39. }
  40. function query() {
  41. VbUtil.EventHandlerUtil.trigger(tableBox.value, "vbtable.query")
  42. }
  43. function resetForm() {
  44. searchFormRef.value.clearValidate()
  45. searchFormRef.value.resetFields()
  46. VbUtil.EventHandlerUtil.trigger(tableBox.value, "vbtable.reset")
  47. }
  48. </script>
  49. <template>
  50. <VbForm
  51. v-if="searchFormRowItems || searchFormItems"
  52. ref="searchFormRef"
  53. v-model:data="queryParams"
  54. :row-items="searchFormRowItems"
  55. :items="_searchFormItems"
  56. :span="propOpts.searchFormSpan">
  57. <template v-for="(_, name) in $slots" #[`${formatterFormSlot(name)}`]>
  58. <slot v-if="name.toString().search(formSlotSuffix) == 0" :name="name" />
  59. </template>
  60. <template #searchbtn>
  61. <slot v-if="$slots.searchbtn" name="searchbtn"></slot>
  62. <el-col v-else :span="propOpts.tableSearchBtnSpan">
  63. <el-button class="btn btn-primary" @click="query">
  64. <template #icon>
  65. <VbIcon icon-name="magnifier" icon-type="solid"></VbIcon>
  66. </template>
  67. 搜索
  68. </el-button>
  69. <el-button class="btn btn-light-primary" @click="resetForm">
  70. <template #icon>
  71. <VbIcon icon-name="arrows-loop" icon-type="solid"></VbIcon>
  72. </template>
  73. 重置
  74. </el-button>
  75. </el-col>
  76. </template>
  77. </VbForm>
  78. <el-form
  79. v-else-if="$slots['table-search-form']"
  80. ref="searchFormRef"
  81. :inline="true"
  82. :model="propOpts.queryParams"
  83. class="el-row mb-3">
  84. <slot name="table-search-form" />
  85. <el-col :span="propOpts.tableSearchBtnSpan">
  86. <el-form-item>
  87. <el-button class="btn btn-primary" @click="query">
  88. <template #icon>
  89. <VbIcon icon-name="magnifier" icon-type="solid"></VbIcon>
  90. </template>
  91. 搜索
  92. </el-button>
  93. <el-button class="btn btn-light-primary" @click="resetForm">
  94. <template #icon>
  95. <VbIcon icon-name="arrows-loop" icon-type="solid"></VbIcon>
  96. </template>
  97. 重置
  98. </el-button>
  99. </el-form-item>
  100. </el-col>
  101. </el-form>
  102. </template>