浏览代码

fix: 修复Search组件无法默认值

kailong321200875 2 年之前
父节点
当前提交
3368fda251

+ 8 - 2
src/components/Search/src/Search.vue

@@ -33,7 +33,11 @@ const props = defineProps({
   expand: propTypes.bool.def(false),
   // 伸缩的界限字段
   expandField: propTypes.string.def(''),
-  inline: propTypes.bool.def(true)
+  inline: propTypes.bool.def(true),
+  model: {
+    type: Object as PropType<Recordable>,
+    default: () => ({})
+  }
 })
 
 const emit = defineEmits(['search', 'reset'])
@@ -62,7 +66,9 @@ const newSchema = computed(() => {
   return schema
 })
 
-const { register, elFormRef, methods } = useForm()
+const { register, elFormRef, methods } = useForm({
+  model: props.model || {}
+})
 
 const search = async () => {
   await unref(elFormRef)?.validate(async (isValid) => {

+ 3 - 0
src/hooks/web/useForm.ts

@@ -41,6 +41,9 @@ export const useForm = (props?: FormProps) => {
     setProps: async (props: FormProps = {}) => {
       const form = await getForm()
       form?.setProps(props)
+      if (props.model) {
+        form?.setValues(props.model)
+      }
     },
 
     setValues: async (data: Recordable) => {

+ 5 - 1
src/hooks/web/useTable.ts

@@ -23,6 +23,8 @@ interface UseTableConfig<T = any> {
     list: string
     total?: string
   }
+  // 默认传递的参数
+  defaultParams?: Recordable
   props?: TableProps
 }
 
@@ -47,7 +49,9 @@ export const useTable = <T = any>(config?: UseTableConfig<T>) => {
     // 表格数据
     tableList: [],
     // AxiosConfig 配置
-    params: {},
+    params: {
+      ...(config?.defaultParams || {})
+    },
     // 加载中
     loading: true,
     // 当前行的数据

+ 8 - 2
src/views/Components/Table/DefaultTable.vue

@@ -31,7 +31,8 @@ const columns: TableColumn[] = [
   },
   {
     field: 'display_time',
-    label: t('tableDemo.displayTime')
+    label: t('tableDemo.displayTime'),
+    sortable: true
   },
   {
     field: 'importance',
@@ -90,7 +91,12 @@ const actionFn = (data: TableSlotDefault) => {
 
 <template>
   <ContentWrap :title="t('tableDemo.table')" :message="t('tableDemo.tableDes')">
-    <Table :columns="columns" :data="tableDataList" :loading="loading">
+    <Table
+      :columns="columns"
+      :data="tableDataList"
+      :loading="loading"
+      :defaultSort="{ prop: 'display_time', order: 'descending' }"
+    >
       <template #action="data">
         <ElButton type="primary" @click="actionFn(data as TableSlotDefault)">
           {{ t('tableDemo.action') }}

+ 9 - 1
src/views/Example/Dialog/ExampleDialog.vue

@@ -20,6 +20,9 @@ const { register, tableObject, methods } = useTable<TableData>({
   response: {
     list: 'list',
     total: 'total'
+  },
+  defaultParams: {
+    title: 's'
   }
 })
 
@@ -212,7 +215,12 @@ const save = async () => {
 
 <template>
   <ContentWrap>
-    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
+    <Search
+      :model="{ title: 's' }"
+      :schema="allSchemas.searchSchema"
+      @search="setSearchParams"
+      @reset="setSearchParams"
+    />
 
     <div class="mb-10px">
       <ElButton type="primary" @click="AddAction">{{ t('exampleDemo.add') }}</ElButton>