Skip to content

Commit

Permalink
perf: 翻页后自动滚动到顶部
Browse files Browse the repository at this point in the history
  • Loading branch information
greper committed Sep 2, 2023
1 parent f4d0bcb commit a6e5f67
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/fast-admin/fs-admin-antdv
2 changes: 1 addition & 1 deletion packages/fast-admin/fs-admin-antdv4
2 changes: 1 addition & 1 deletion packages/fast-admin/fs-admin-element
2 changes: 1 addition & 1 deletion packages/fast-admin/fs-admin-naive-ui
11 changes: 10 additions & 1 deletion packages/fast-crud/src/components/crud/fs-table.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
computed,
defineComponent,
getCurrentInstance,
getCurrentScope,
PropType,
ref,
resolveDirective,
Expand Down Expand Up @@ -275,16 +277,23 @@ export default defineComponent({
return cellRef?.getTargetRef();
};

const { ui } = useUi();

const currentRef = getCurrentInstance();
watch(
() => {
return props.data;
},
(value) => {
ui.table.scrollTo({
top: 0,
tableRef,
fsTableRef: currentRef
});
ctx.emit("data-change", { data: value });
}
);

const { ui } = useUi();
const tableComp = resolveDynamicComponent(ui.table.name);
const tableColumnCI = ui.tableColumn;

Expand Down
3 changes: 1 addition & 2 deletions packages/fast-crud/src/use/use-expose.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Ref, toRaw } from "vue";
import { nextTick, Ref, toRaw } from "vue";
import {
CrudExpose,
Editable,
Expand Down Expand Up @@ -385,7 +385,6 @@ export function useExpose(props: UseExposeProps): UseExposeRet {
);
return;
}

crudBinding.value.data = records;
if (crudBinding.value.pagination) {
crudBinding.value.pagination[ui.pagination.currentPage] = currentPage;
Expand Down
8 changes: 8 additions & 0 deletions packages/ui/ui-antdv/src/antdv.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
SwitchCI,
TableCI,
TableColumnCI,
TableScrollReq,
TabPaneCI,
TabsCI,
TagCI,
Expand Down Expand Up @@ -489,6 +490,13 @@ export class Antdv implements UiInterface {
fixedHeaderNeedComputeBodyHeight: true,
headerDomSelector: ".ant-table-thead",
vLoading: false,
scrollTo(req: TableScrollReq) {
try {
req.fsTableRef.vnode.el.querySelector(".ant-table-body").scrollTop = req.top;
} catch (e) {
console.error("scroll to top error:", e);
}
},
onChange({ onSortChange, onFilterChange, onPagination, bubbleUp }) {
return {
onChange: (pagination: any, filters: any, sorter: any, ctx: any) => {
Expand Down
1 change: 1 addition & 0 deletions packages/ui/ui-antdv4/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {
],
rules: {
// "comma-dangle": "off",
"@typescript-eslint/no-unused-vars": "off",
"max-len": [0, 160, 2, { ignoreUrls: true }],
"@typescript-eslint/ban-ts-ignore": "off",
"@typescript-eslint/explicit-function-return-type": "off",
Expand Down
8 changes: 8 additions & 0 deletions packages/ui/ui-antdv4/src/antdv.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
SwitchCI,
TableCI,
TableColumnCI,
TableScrollReq,
TabPaneCI,
TabsCI,
TagCI,
Expand Down Expand Up @@ -489,6 +490,13 @@ export class Antdv implements UiInterface {
fixedHeaderNeedComputeBodyHeight: true,
headerDomSelector: ".ant-table-thead",
vLoading: false,
scrollTo(req: TableScrollReq) {
try {
req.fsTableRef.vnode.el.querySelector(".ant-table-body").scrollTop = req.top;
} catch (e) {
console.error("scroll to top error:", e);
}
},
onChange({ onSortChange, onFilterChange, onPagination, bubbleUp }) {
return {
onChange: (pagination: any, filters: any, sorter: any, ctx: any) => {
Expand Down
6 changes: 5 additions & 1 deletion packages/ui/ui-element/src/element.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ import {
ButtonGroupCI,
ColCI,
RowCI,
CardCI
CardCI,
TableScrollReq
} from "@fast-crud/ui-interface";
// @ts-ignore
import _ from "lodash-es";
Expand Down Expand Up @@ -404,6 +405,9 @@ export class Element implements UiInterface {
rebuildRenderScope: (scope) => {
return scope;
},
scrollTo(req: TableScrollReq) {
req.tableRef.value.setScrollTop(req.top);
},
onChange({ onSortChange, onFilterChange, bubbleUp }) {
return {
onSortChange: (ctx: any) => {
Expand Down
3 changes: 3 additions & 0 deletions packages/ui/ui-interface/src/ui-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ export type TableSorterContext = {
asc: boolean;
};
export type TableBuilderOption = {} & BindBuilderOptions;
export type TableScrollReq = { top: number; tableRef: any; fsTableRef: any };
export interface TableCI extends CI<TableBuilderOption> {
defaultRowKey?: string | ((rowData: any) => any);
data: string;
Expand All @@ -213,6 +214,8 @@ export interface TableCI extends CI<TableBuilderOption> {
* render 方法触发时的参数构建出一个scope
*/
rebuildRenderScope?: (scope: any, prop2?: any, prop3?: any, prop4?: any) => ComponentBinding;

scrollTo(req: TableScrollReq): void;
}

export type CheckboxGroupBuilderOption = {} & BindBuilderOptions;
Expand Down
1 change: 1 addition & 0 deletions packages/ui/ui-naive/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = {
rules: {
// "comma-dangle": "off",
// "max-len": [0, 160, 2, { ignoreUrls: true }],
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/ban-ts-ignore": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-explicit-any": "off",
Expand Down
4 changes: 4 additions & 0 deletions packages/ui/ui-naive/src/naive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
SwitchCI,
TableCI,
TableColumnCI,
TableScrollReq,
TabPaneCI,
TabsCI,
TagCI,
Expand Down Expand Up @@ -532,6 +533,9 @@ export class Naive implements UiInterface {
fixedHeaderNeedComputeBodyHeight: true,
headerDomSelector: ".n-data-table-thead",
vLoading: false,
scrollTo(req: TableScrollReq) {
req.tableRef.value.scrollTo({ top: req.top });
},
onChange({ onSortChange, onFilterChange, onPagination, bubbleUp }) {
return {
"onUpdate:filters": (filters: any, initiatorColumn: any) => {
Expand Down

0 comments on commit a6e5f67

Please sign in to comment.