TableSearchFrom.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <script setup lang="ts">
  2. import { VbUtil } from "@@/vb-dom"
  3. import { symbolKeys } from "./../../models"
  4. // 从统一上下文获取数据
  5. const context = inject(symbolKeys.tableContext)!
  6. // 解构配置和状态
  7. const {
  8. showSearchBtn,
  9. showSearchBtnText,
  10. searchLabelWidth,
  11. closeAdvancedSearchAfterQuery,
  12. advancedSearchWidth
  13. } = context.config
  14. // 从 context 获取高级搜索面板显示状态
  15. const advancedSearchVisible = context.state.advancedSearchVisible
  16. // 处理宽度值,如果是数字则添加 px 单位
  17. const panelWidth = computed(() => {
  18. if (typeof advancedSearchWidth === "number") {
  19. return `${advancedSearchWidth}px`
  20. }
  21. return advancedSearchWidth
  22. })
  23. // searchFormRowItems 和 searchFormItems 现在是响应式计算属性
  24. const searchFormRowItems = context.config.searchFormRowItems
  25. const searchFormItems = context.config.searchFormItems
  26. const queryParams = context.state.innerQueryParams
  27. const tableBox = context.refs.tableBox
  28. const formSlotSuffix = context.constants.formSlotSuffix
  29. const searchFormRef = ref<any>()
  30. // 关闭面板
  31. function closePanel() {
  32. advancedSearchVisible.value = false
  33. }
  34. const _searchFormItems = computed(() => {
  35. if (searchFormItems.value || searchFormRowItems.value) {
  36. const items = searchFormItems.value || []
  37. items.forEach((v) => {
  38. // 纵向布局:每个字段独占一行
  39. v.span = 24
  40. })
  41. return items as any
  42. }
  43. return searchFormItems.value as any
  44. })
  45. // form表单的插槽 需要tool-form_开头,并去除tool-form_传递给VbForm组件
  46. function formatterFormSlot(v: string | number): string | undefined {
  47. v = v as string
  48. if (v.search(formSlotSuffix) == 0) {
  49. return v.substring(formSlotSuffix.length)
  50. }
  51. return undefined
  52. }
  53. function query() {
  54. VbUtil.EventHandlerUtil.trigger(tableBox.value, "vbtable.query")
  55. if (closeAdvancedSearchAfterQuery) {
  56. setTimeout(() => closePanel(), 300) // 延迟关闭,让用户看到反馈
  57. }
  58. }
  59. function resetForm() {
  60. searchFormRef.value.clearValidate()
  61. searchFormRef.value.resetFields()
  62. VbUtil.EventHandlerUtil.trigger(tableBox.value, "vbtable.reset")
  63. }
  64. </script>
  65. <template>
  66. <div
  67. v-if="showSearchBtn"
  68. ref="panelRef"
  69. class="advanced-search-panel"
  70. :class="{ 'panel-hidden': !advancedSearchVisible }"
  71. :style="{ width: panelWidth }">
  72. <div class="panel-header">
  73. <span class="panel-title">高级搜索</span>
  74. <vb-tooltip class="item" content="关闭" placement="top" :delay="1000">
  75. <div
  76. class="btn btn-sm btn-light-primary w-25px h-25px p-0 d-flex align-items-center justify-content-center"
  77. @click="closePanel">
  78. <span class="bi bi-x-circle fs-5"></span>
  79. </div>
  80. </vb-tooltip>
  81. </div>
  82. <div class="panel-content">
  83. <div class="w-100">
  84. <VbForm
  85. v-if="searchFormRowItems || searchFormItems"
  86. ref="searchFormRef"
  87. v-model:data="queryParams"
  88. :row-items="searchFormRowItems"
  89. :items="_searchFormItems"
  90. :label-width="searchLabelWidth">
  91. <template v-for="(_, name) in $slots" #[`${formatterFormSlot(name)}`]>
  92. <slot v-if="name.toString().search(formSlotSuffix) == 0" :name="name" />
  93. </template>
  94. </VbForm>
  95. <el-form
  96. v-else-if="$slots['table-search-form']"
  97. ref="searchFormRef"
  98. :inline="false"
  99. :model="queryParams"
  100. :label-width="searchLabelWidth"
  101. class="el-form-vertical">
  102. <slot name="table-search-form" />
  103. </el-form>
  104. </div>
  105. <div class="search-buttons d-flex mt-3">
  106. <slot v-if="$slots.searchbtn" name="searchbtn"></slot>
  107. <template v-else>
  108. <el-button class="btn btn-primary" @click="query">
  109. <VbIcon icon-name="magnifier" icon-type="solid"></VbIcon>
  110. <span class="ms-2" v-if="showSearchBtnText">搜索</span>
  111. </el-button>
  112. <el-button class="btn btn-light-primary" @click="resetForm">
  113. <VbIcon icon-name="arrows-loop" icon-type="solid"></VbIcon>
  114. <span class="ms-2" v-if="showSearchBtnText">重置</span>
  115. </el-button>
  116. </template>
  117. </div>
  118. </div>
  119. </div>
  120. </template>