search.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <div class="table-search-container" v-if="props.search.length > 0">
  3. <el-form ref="tableSearchRef" :model="state.form" label-width="100px" class="table-form">
  4. <el-row :gutter="20">
  5. <!-- <el-col :xs="12" :sm="8" :md="8" :lg="6" :xl="4" class="mb20"></el-col> -->
  6. <el-col :xs="12" :sm="5" :md="5" :lg="6" :xl="4" class="mb20" v-for="(val, key) in search" :key="key" v-show="key < 3 || state.isToggle">
  7. <template v-if="val.type !== ''">
  8. <el-form-item
  9. label-width="auto"
  10. :label="val.label"
  11. :prop="val.prop"
  12. :rules="[{ required: val.required, message: `${val.label}不能为空`, trigger: val.type === 'input' ? 'blur' : 'change' }]"
  13. >
  14. <el-input
  15. v-model="state.form[val.prop]"
  16. v-bind="val.comProps"
  17. :placeholder="val.placeholder"
  18. :clearable="!val.required"
  19. v-if="val.type === 'input'"
  20. @keyup.enter="onSearch(tableSearchRef)"
  21. @change="val.change"
  22. class="w100"
  23. />
  24. <el-date-picker
  25. v-model="state.form[val.prop]"
  26. v-bind="val.comProps"
  27. type="date"
  28. :placeholder="val.placeholder"
  29. :clearable="!val.required"
  30. v-else-if="val.type === 'date'"
  31. @change="val.change"
  32. class="w100"
  33. />
  34. <el-select
  35. v-model="state.form[val.prop]"
  36. v-bind="val.comProps"
  37. :clearable="!val.required"
  38. :placeholder="val.placeholder"
  39. v-else-if="val.type === 'select'"
  40. @change="val.change"
  41. class="w100"
  42. >
  43. <el-option v-for="item in val.options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
  44. </el-select>
  45. <el-cascader
  46. v-else-if="val.type === 'cascader' && val.cascaderData"
  47. :options="val.cascaderData"
  48. :clearable="!val.required"
  49. filterable
  50. :props="val.cascaderProps ? val.cascaderProps : state.cascaderProps"
  51. :placeholder="val.placeholder"
  52. @change="val.change"
  53. class="w100"
  54. v-bind="val.comProps"
  55. v-model="state.form[val.prop]"
  56. >
  57. </el-cascader>
  58. </el-form-item>
  59. </template>
  60. </el-col>
  61. <el-col :xs="12" :sm="9" :md="9" :lg="6" :xl="4" class="mb20">
  62. <el-form-item class="table-form-btn" label-width="auto">
  63. <template #label>
  64. <div v-if="search.length > 3">
  65. <div class="table-form-btn-toggle" @click="state.isToggle = !state.isToggle">
  66. <span>{{ state.isToggle ? '收起' : '展开' }}</span>
  67. <SvgIcon :name="state.isToggle ? 'ele-ArrowUp' : 'ele-ArrowDown'" />
  68. </div>
  69. </div>
  70. </template>
  71. <div>
  72. <!-- 使用el-button-group会导致具有type属性的按钮的右边框无法显示 -->
  73. <!-- <el-button-group> -->
  74. <el-button plain type="primary" icon="ele-Search" @click="onSearch(tableSearchRef)"> 查询 </el-button>
  75. <el-button icon="ele-Refresh" @click="onReset(tableSearchRef)" style="margin-left: 12px;"> 重置 </el-button>
  76. <!-- </el-button-group> -->
  77. </div>
  78. </el-form-item>
  79. </el-col>
  80. </el-row>
  81. </el-form>
  82. </div>
  83. </template>
  84. <script setup lang="ts" name="makeTableDemoSearch">
  85. import { reactive, ref, onMounted, watch } from 'vue';
  86. import type { FormInstance } from 'element-plus';
  87. // 定义父组件传过来的值
  88. const props = defineProps({
  89. // 搜索表单,type-控件类型(input,select,cascader,date),options-type为selct时需传值,cascaderData,cascaderProps-type为cascader时需传值,属性同elementUI,cascaderProps不传则使用state默认。
  90. // 可带入comProps属性,和使用的控件属性对应
  91. search: {
  92. type: Array<TableSearchType>,
  93. default: () => [],
  94. },
  95. reset: {
  96. type: Object,
  97. default: () => {},
  98. },
  99. param: {
  100. type: Object,
  101. default: () => {},
  102. },
  103. });
  104. // 定义子组件向父组件传值/事件
  105. const emit = defineEmits(['search', 'reset']);
  106. // 定义变量内容
  107. const tableSearchRef = ref<FormInstance>();
  108. const state = reactive({
  109. form: {} as any,
  110. isToggle: false,
  111. cascaderProps: { checkStrictly: true, emitPath: false, value: 'id', label: 'name', expandTrigger: 'hover' },
  112. });
  113. watch(
  114. () => props.param,
  115. (value) => {
  116. if (value) {
  117. state.form = Object.assign({}, { ...value });
  118. }
  119. },
  120. {
  121. immediate: true,
  122. deep: true,
  123. }
  124. );
  125. // 查询
  126. const onSearch = (formEl: FormInstance | undefined) => {
  127. if (!formEl) return;
  128. formEl.validate((valid: boolean) => {
  129. if (valid) {
  130. emit('search', state.form);
  131. } else {
  132. return false;
  133. }
  134. });
  135. };
  136. // 重置
  137. const onReset = (formEl: FormInstance | undefined) => {
  138. if (!formEl) return;
  139. formEl.resetFields();
  140. emit('reset', state.form);
  141. emit('search', state.form);
  142. };
  143. // // 初始化 form 字段,取自父组件 search.prop
  144. // const initFormField = () => {
  145. // if (props.search.length <= 0) return false;
  146. // props.search.forEach((v) => (state.form[v.prop] = ''));
  147. // };
  148. // 页面加载时
  149. onMounted(() => {
  150. // initFormField();
  151. });
  152. </script>
  153. <style scoped lang="scss">
  154. .table-search-container {
  155. display: flex;
  156. .table-form {
  157. flex: 1;
  158. .table-form-btn-toggle {
  159. white-space: nowrap;
  160. user-select: none;
  161. display: flex;
  162. align-items: center;
  163. color: var(--el-color-primary);
  164. }
  165. }
  166. }
  167. </style>