Skip to content

Commit

Permalink
refactor: meTable 类型提示增强,导出字段 elTable改为elTableRef
Browse files Browse the repository at this point in the history
  • Loading branch information
yuntian001 committed Sep 21, 2022
1 parent 452760d commit 0ca430a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/components/meTable/hooks/exportTable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Defaults, TableExport } from 'tableexport';
type ExportType = 'csv' | 'txt' | 'xlsx';
export default (elTable: ELTable, type: ExportType, filename?: string, options?: Defaults) => {
export default (elTable: ELTableInstance, type: ExportType, filename?: string, options?: Defaults) => {
const selectionIndexs = elTable.getSelectionIndexs();
const ignoreRows = [] as number[];
if (selectionIndexs.length) {
Expand Down
17 changes: 11 additions & 6 deletions src/components/meTable/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
</template>
<script lang="ts">
import { ElTable } from 'element-plus';
import { ComponentOptionsMixin, ExtractPropTypes, PropType } from 'vue';
import { ComponentOptionsMixin, ExtractPropTypes, PropType, Ref } from 'vue';
import customColumn from './hooks/customColumn';
import exportTable from './hooks/exportTable';
import printTable from './hooks/print';
Expand All @@ -100,7 +100,7 @@ const props = {
{
label: string;
filename?: string;
handle: (elTable: ELTable, filename: string) => void | 'xlsx' | 'csv' | 'txt';
handle: (elTable: ELTableInstance, filename: string) => void | 'xlsx' | 'csv' | 'txt';
}[]
>,
default: () => [
Expand Down Expand Up @@ -140,7 +140,12 @@ const emits = {
};
export default defineComponent<
ComponentProps<typeof ElTable> & Partial<ExtractPropTypes<typeof props>>,
Record<string, any>,
{
[k: string]: any;
elTableRef: Ref<ELTableInstance | undefined>;
customColumnProps: Ref<ReturnType<typeof customColumn> | undefined>;
searchText: Ref<string>;
},
Record<string, any>,
Record<string, any>,
Record<string, any>,
Expand Down Expand Up @@ -182,7 +187,7 @@ export default defineComponent<
customColumnProps.value!.checkedLabels.delete(data.value);
}
};
const elTableRef = ref<ELTable>();
const elTableRef = ref<ELTableInstance>();
onMounted(() => {
elTableRef.value!.getSelectionIndexs = function () {
const index = [] as number[];
Expand All @@ -194,7 +199,7 @@ export default defineComponent<
return index;
};
});
expose({ elTable: elTableRef, customColumnProps });
expose({ elTableRef, customColumnProps, searchText });
return {
showSearch,
searchText,
Expand All @@ -205,7 +210,7 @@ export default defineComponent<
exportTable,
printTable,
handleExport: (
handle: (elTable: ELTable, filename: string) => void | 'xls' | 'txt' | 'csv',
handle: (elTable: ELTableInstance, filename: string) => void | 'xls' | 'txt' | 'csv',
filename: string,
) => {
if (typeof handle === 'string') {
Expand Down
5 changes: 2 additions & 3 deletions src/views/components/table/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<el-button @click="canDel = !canDel">{{ t('删除切换') }}</el-button>
<el-button @click="customColumn = !customColumn">{{ t('自定义列') }}</el-button>
<el-button @click="data = []">{{ t('清空') }}</el-button>
<el-button @click="meTableRef!.elTable.toggleAllSelection()">{{ t('全选') }}</el-button>
<el-button @click="meTableRef!.elTableRef!.toggleAllSelection()">{{ t('全选') }}</el-button>
</template>
<el-table-column type="selection" label="选择" width="55" />
<el-table-column prop="date" :label="t('日期')"> </el-table-column>
Expand All @@ -83,8 +83,7 @@
import { listApi } from '@/api/table';
import { useLocalesI18n } from '@/locales/i18n';
import { FormInstance } from 'element-plus';
import meTable from '@/components/meTable/index.vue';
const meTableRef = ref<InstanceType<typeof meTable>>();
const meTableRef = ref<MeTableInstance>();
const customColumn = ref(true);
const { t } = useLocalesI18n({}, [(locale: string) => import(`./lang/${locale}.json`), 'tableLang']);
const canDel = ref(true);
Expand Down

0 comments on commit 0ca430a

Please sign in to comment.