Skip to content

Commit 6f5f962

Browse files
committed
add scrollToRowKey instance method
1 parent 7f22cfc commit 6f5f962

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

packages/ve-table/src/index.jsx

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
isFunction,
1010
isNumber,
1111
scrollTo,
12+
isEmptyValue,
1213
} from "../../src/utils/index.js";
1314
import emitter from "../../src/mixins/emitter";
1415
import {
@@ -22,7 +23,6 @@ import Header from "./header";
2223
import Body from "./body";
2324
import Footer from "./footer";
2425
import { KEY_CODES } from "../../src/utils/constant";
25-
import { isEmptyValue } from "../../src/utils/index";
2626
import clickoutside from "../../src/directives/clickoutside";
2727
import { mutations } from "./util/store";
2828
import VueDomResizeObserver from "../../src/comps/resize-observer";
@@ -1052,6 +1052,47 @@ export default {
10521052
[INSTANCE_METHODS.SCROLL_TO](option) {
10531053
scrollTo(this.$refs[this.tableContainerRef], option);
10541054
},
1055+
// table scroll to rowKey
1056+
[INSTANCE_METHODS.SCROLL_TO_ROW_KEY]({ rowKey }) {
1057+
if (isEmptyValue(rowKey)) {
1058+
console.warn("Row key can't be empty!");
1059+
return false;
1060+
}
1061+
1062+
let scrollTop = 0;
1063+
1064+
const { isVirtualScroll, headerRows } = this;
1065+
1066+
const tableContainerRef = this.$refs[this.tableContainerRef];
1067+
1068+
if (isVirtualScroll) {
1069+
const position = this.virtualScrollPositions.find(
1070+
(x) => x.rowKey === rowKey,
1071+
);
1072+
1073+
if (position) {
1074+
scrollTop = position.top;
1075+
}
1076+
} else {
1077+
const rowEl = this.$el.querySelector(
1078+
`tbody tr[${COMPS_CUSTOM_ATTRS.BODY_ROW_KEY}="${rowKey}"]`,
1079+
);
1080+
1081+
const totalHeaderHeight = headerRows.reduce(
1082+
(total, currentVal) => {
1083+
return currentVal.rowHeight + total;
1084+
},
1085+
0,
1086+
);
1087+
1088+
scrollTop = rowEl.offsetTop - totalHeaderHeight;
1089+
}
1090+
1091+
scrollTo(tableContainerRef, {
1092+
top: scrollTop,
1093+
behavior: "smooth",
1094+
});
1095+
},
10551096
},
10561097
mounted() {
10571098
// receive row selected change

packages/ve-table/src/util/constant.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,8 @@ export const COMPS_CUSTOM_ATTRS = {
8686

8787
// instance methods
8888
export const INSTANCE_METHODS = {
89+
// scroll to pixels
8990
SCROLL_TO: "scrollTo",
91+
// scroll to rowKey
92+
SCROLL_TO_ROW_KEY: "scrollToRowKey",
9093
};

0 commit comments

Comments
 (0)