Skip to content

Commit

Permalink
bugfix(table): table问题合集 #2091 (#2096)
Browse files Browse the repository at this point in the history
* bugfix: 修复column filter默认值回填问题
# Reviewed, transaction id: 15238

* feat(table): 支持跨页全选设置
# Reviewed, transaction id: 15598

* fix(virtual-render): 修复lineHeight fn计算最后一行位置问题
# Reviewed, transaction id: 15633

* Merge branch 'staging' of https://github.com/TencentBlueKing/bkui-vue3 into staging
, # Reviewed, transaction id: 15635

* fix(virtual-render): 计算最后一行行数问题
# Reviewed, transaction id: 15648

* fix(table): 拖拽完毕抛出事件参数问题修复
# Reviewed, transaction id: 15702

* fix(table): 拖拽完毕抛出事件参数问题修复
# Reviewed, transaction id: 15703

* fix(table): 拖拽完毕抛出事件参数问题修复
# Reviewed, transaction id: 15705

* Merge branch 'staging' of https://github.com/TencentBlueKing/bkui-vue3 into staging
, # Reviewed, transaction id: 15706

* bugfix(table): 过滤条件&样式修复

* bugfix(table): 修复已知问题 & 调整虚拟渲染逻辑

* bugfix(table): 修复已知问题 & 调整虚拟渲染逻辑
# Reviewed, transaction id: 16080

* bufix(table): scrollTo定位问题修复
# Reviewed, transaction id: 16154

* Merge branch 'staging' of https://github.com/TencentBlueKing/bkui-vue3 into beta-2.2.3
# Reviewed, transaction id: 16780
  • Loading branch information
xintaoLi authored Aug 29, 2024
1 parent 9766d66 commit 0aaf194
Show file tree
Hide file tree
Showing 13 changed files with 619 additions and 369 deletions.
10 changes: 8 additions & 2 deletions packages/scrollbar/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,14 @@ export default class BkScrollbar {
}

scrollTo({ x, y }) {
this.element.scrollTop = y;
(this.element as HTMLElement).scrollLeft = x;
if (y !== undefined && typeof y === 'number') {
this.element.scrollTop = y;
}

if (x !== undefined && typeof x === 'number') {
(this.element as HTMLElement).scrollLeft = x;
}

updateGeometry(this);
}

Expand Down
38 changes: 34 additions & 4 deletions packages/table/src/components/table-column.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
import { defineComponent, ExtractPropTypes, inject, onUnmounted, watch, toRaw } from 'vue';
import { defineComponent, ExtractPropTypes, inject, onUnmounted, watch, toRaw, h } from 'vue';

import { PropTypes } from '@bkui-vue/shared';
import { isEqual } from 'lodash';

import { COL_MIN_WIDTH, PROVIDE_KEY_INIT_COL } from '../const';
import {
Expand All @@ -42,6 +41,7 @@ import {
StringNumberType,
TableAlign,
} from '../props';
import { isEqual } from 'lodash';

const TableColumnProp = {
label: LabelFunctionStringType,
Expand Down Expand Up @@ -73,10 +73,26 @@ export default defineComponent({
const initTableColumns = inject(PROVIDE_KEY_INIT_COL, () => {});
const lastPropsVal = {};

const isPropsEqual = (sorce: Record<string, unknown>, target: Record<string, unknown>) => {
const rawProps = toRaw(target);
const keys = Object.keys(rawProps);
return keys.every(key => {
if (typeof rawProps[key] === 'function') {
return sorce[key] !== undefined;
}

if (key === 'children') {
return true;
}

return isEqual(sorce[key], target[key]);
});
};

watch(
() => [props],
() => {
if (!isEqual(lastPropsVal, toRaw(props))) {
if (!isPropsEqual(lastPropsVal, props)) {
initTableColumns();
Object.assign(lastPropsVal, toRaw(props));
}
Expand All @@ -87,7 +103,21 @@ export default defineComponent({
onUnmounted(() => {
initTableColumns();
});
},

render() {
try {
const renderDefault = this.$slots.default?.({
row: {},
column: {},
$index: -1,
});
const children = [renderDefault];

return () => {};
const vnode = h('div', children);
return vnode;
} catch {
return h('div', []);
}
},
});
14 changes: 12 additions & 2 deletions packages/table/src/hooks/use-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,17 @@ export default (props: TablePropTypes, ctx) => {
return null;
});

const setBodyHeight = (height: number) => {
bodyHeight.value = height - headHeight.value - fixedBottomHeight.value - footHeight.value;
const getBodyHeight = (height) => {
return height - headHeight.value - fixedBottomHeight.value - footHeight.value;
}

const setBodyHeight = (height: number, withHeadFoot = true) => {
if (withHeadFoot) {
bodyHeight.value = getBodyHeight(height);
return;
}

bodyHeight.value = height;
};

const setVirtualBodyHeight = (height: number) => {
Expand Down Expand Up @@ -376,6 +385,7 @@ export default (props: TablePropTypes, ctx) => {
renderBody,
renderFooter,
renderFixedBottom,
getBodyHeight,
setBodyHeight,
setVirtualBodyHeight,
setFootHeight,
Expand Down
9 changes: 8 additions & 1 deletion packages/table/src/hooks/use-rows.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ const useRows = (props: TablePropTypes) => {
tableRowList.value.forEach(row => setRowAttribute(row, TABLE_ROW_ATTRIBUTE.ROW_HEIGHT, height));
};

const getRowHeight = (row?, index?, type?) => {
const getRowHeight = (row?, index?, type?): number => {
if (row) {
return getRowAttribute(row, TABLE_ROW_ATTRIBUTE.ROW_HEIGHT);
}
Expand Down Expand Up @@ -355,6 +355,12 @@ const useRows = (props: TablePropTypes) => {
formatDataSchema();
};

const getCurrentPageRowsHeight = () => {
return pageRowList.reduce((out, row) => {
return out + getRowHeight(row);
}, 0);
};

return {
setRowIndex,
setRowExpand,
Expand All @@ -373,6 +379,7 @@ const useRows = (props: TablePropTypes) => {
getRowSelection,
getRowIndeterminate,
getRowCheckedAllValue,
getCurrentPageRowsHeight,
changePageRowIndex,
toggleAllSelection,
getRowHeight,
Expand Down
5 changes: 3 additions & 2 deletions packages/table/src/plugins/head-filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,17 @@ export default defineComponent({

const state = reactive({
isOpen: false,
checked: checked.value,
checked: [],
});

state.checked.push(...checked.value);

watch(
() => checked,
() => {
state.checked.length = 0;
state.checked = [];
state.checked.push(...checked.value);
// handleBtnSaveClick();
},
{ deep: true },
);
Expand Down
12 changes: 8 additions & 4 deletions packages/table/src/table.less
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,14 @@

tbody {
tr {

&.stripe-row {
background: #fafbfd;
td {
background-color: #fafbfd;
}
}

td {
background-color: #fff;
border-top: 1px solid @table-bg-color;
Expand Down Expand Up @@ -598,10 +606,6 @@
margin-left: auto;
}
}

.stripe-row {
background: #fafbfd;
}
}

.@{bk-prefix}-across-page-popover {
Expand Down
48 changes: 45 additions & 3 deletions packages/table/src/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export default defineComponent({
setOffsetRight,
setHeaderRowCount,
setLineHeight,
getBodyHeight,
refBody,
refRoot,
} = useLayout(props, ctx);
Expand Down Expand Up @@ -192,6 +193,8 @@ export default defineComponent({
}
};

const scrollTo00 = ref(false);

const setTableData = debounce((resetScroll = true) => {
const filterOrderList = getFilterAndSortList();
if (!props.remotePagination) {
Expand All @@ -200,12 +203,17 @@ export default defineComponent({

const renderList = getRenderRowList(filterOrderList);
rows.setPageRowList(renderList);
if (resetScroll) {
scrollTo00.value = true;
}

nextTick(() => {
setOffsetRight();
setRowsBodyHeight();

if (resetScroll) {
if (scrollTo00.value) {
scrollTo(0, 0);
scrollTo00.value = false;
}
});
}, 64);
Expand Down Expand Up @@ -240,6 +248,21 @@ export default defineComponent({
});
});

const setRowsBodyHeight = () => {
if (props.height === '100%' || props.height === 'auto') {
const rowsHeight = rows.getCurrentPageRowsHeight();
let bodyHeight = rowsHeight;
if (/^\d+\.?\d*(px)?$/.test(`${props.maxHeight}`)) {
const maxHeight = getBodyHeight(Number(`${props.maxHeight}`.replace('px', '')));
if (bodyHeight > maxHeight) {
setBodyHeight(maxHeight, false);
return;
}
}
setBodyHeight(bodyHeight, false);
}
};

watch(
() => [props.columns],
() => {
Expand All @@ -266,13 +289,21 @@ export default defineComponent({
);

watch(
() => [columns.sortColumns, columns.filterColumns],
() => [columns.filterColumns],
() => {
setTableData();
},
{ deep: true },
);

watch(
() => [columns.sortColumns],
() => {
setTableData(false);
},
{ deep: true },
);

watch(
() => [pagination.isShowPagination.value],
() => {
Expand All @@ -293,11 +324,22 @@ export default defineComponent({
watch(
() => [pagination.options.count, pagination.options.limit, pagination.options.current],
() => {
setTableData();
setTableData(false);
},
{ immediate: true },
);

const pageListLength = computed(() => rows.pageRowList.length);

watch(pageListLength,
(val, old) => {
if (val < old) {
refBody?.value?.updateScroll?.();
scrollTo(undefined, 0);
}
},
);

ctx.expose({
setRowExpand: rows.setRowExpand,
setAllRowExpand: rows.setAllRowExpand,
Expand Down
7 changes: 4 additions & 3 deletions packages/virtual-render/src/use-scrollbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,21 @@ export default (props: VirtualRenderProps) => {
};

const scrollTo = (x, y) => {
instance.scrollTo({ x, y });
instance?.scrollTo({ x, y });
};

const updateScrollHeight = (height: number) => {
if (instance?.element) {
(instance.element as VirtualElement).scrollHeight = height;
instance.update();
(instance?.element as VirtualElement).scrollHeight = height;
instance?.update();
}
};

return {
init,
instance,
scrollTo,
update: () => instance?.update(),
updateScrollHeight,
};
};
18 changes: 16 additions & 2 deletions packages/virtual-render/src/v-virtual-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,33 @@ export class VisibleRender {
private binding;
private wrapper;
private delay;
private delegateWrapper;
constructor(binding, el) {
this.binding = binding;
this.wrapper = el;
const { throttleDelay } = binding.value;
this.delay = throttleDelay;
this.delegateWrapper = undefined;
}

get scrollHeight() {
return this.delegateWrapper?.scrollHeight ?? this.wrapper?.scrollHeight;
}

get offsetHeight() {
return this.delegateWrapper?.offsetHeight ?? this.wrapper?.offsetHeight;
}

public setDelegateWrapper(el) {
this.delegateWrapper = el;
}

public render(e) {
const { lineHeight = 30, handleScrollCallback, pagination = {}, onlyScroll } = this.binding.value;
if (onlyScroll) {
const elScrollTop = e.offset?.y;
const elScrollLeft = e.offset?.x ?? 0;
const bottom = this.wrapper.scrollHeight - this.wrapper.offsetHeight - elScrollTop;
const bottom = this.scrollHeight - this.offsetHeight - elScrollTop;
handleScrollCallback(e, null, null, elScrollTop, elScrollTop, elScrollLeft, {
bottom: bottom >= 0 ? bottom : 0,
scrollbar: e,
Expand All @@ -127,7 +141,7 @@ export class VisibleRender {
lineHeight,
handleScrollCallback,
{ scrollTop, startIndex, endIndex, groupItemCount, count, scrollLeft },
this.wrapper,
this.delegateWrapper ?? this.wrapper,
e,
);
}
Expand Down
Loading

0 comments on commit 0aaf194

Please sign in to comment.