Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Table] 树形结构:1. 展开全部缺陷修复 2. 动态设置选中行时,禁用状态失效 #902

Merged
merged 18 commits into from
May 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
49072ba
docs(notification): 插件调用与函数式调用
Feb 11, 2022
8e5badf
Merge remote-tracking branch 'upstream/develop' into develop
Feb 14, 2022
5d3dd2a
Merge remote-tracking branch 'upstream/develop' into develop
Feb 15, 2022
df1f39a
Merge remote-tracking branch 'upstream/develop' into develop
chaishi Mar 23, 2022
e9a6f2e
Merge remote-tracking branch 'upstream/develop' into develop
chaishi Mar 28, 2022
993a2a4
Merge remote-tracking branch 'upstream/develop' into develop
chaishi Mar 29, 2022
bbe4c10
fix(table): merged cells border style
chaishi Mar 29, 2022
51a9b4b
feat: merge from upstream/develop
chaishi Mar 31, 2022
93a15e0
Merge remote-tracking branch 'upstream/develop' into develop
chaishi Apr 21, 2022
e57dd2d
Merge remote-tracking branch 'upstream/develop' into develop
chaishi Apr 25, 2022
36d3778
Merge remote-tracking branch 'upstream/develop' into develop
chaishi Apr 30, 2022
1db46ab
Merge remote-tracking branch 'upstream/develop' into develop
chaishi May 10, 2022
edd9e3b
Merge remote-tracking branch 'upstream/develop' into develop
chaishi May 10, 2022
9b4b001
Merge remote-tracking branch 'upstream/develop' into develop
chaishi May 13, 2022
267b4ea
Merge remote-tracking branch 'upstream/develop' into develop
chaishi May 16, 2022
95996fe
Merge remote-tracking branch 'upstream/develop' into develop
chaishi May 21, 2022
871f1b1
fix(table): toggleData and FoldAll
chaishi May 21, 2022
f5bb2e9
fix(table): 树形结构中,动态设置行选中列时,禁用功能失效
chaishi May 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/table/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ tree | Object | - | 树形结构相关配置。具体属性文档查看 `TableTr
treeExpandAndFoldIcon | Function | - | 自定义树形结构展开图标,支持全局配置 `GlobalConfigProvider`。TS 类型:`TNode<{ type: 'expand' | 'fold' }>`。[通用类型定义](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts) | N
`PrimaryTableProps<T>` | \- | - | 继承 `PrimaryTableProps<T>` 中的全部 API | N
onAbnormalDragSort | Function | | TS 类型:`(context: TableAbnormalDragSortContext<T>) => void`<br/>异常拖拽排序时触发,如:树形结构中,非同层级之间的交换。`context.code` 指交换异常错误码,固定值;`context.reason` 指交换异常的原因。[详细类型定义](https://github.com/Tencent/tdesign-vue/tree/develop/src/table/type.ts)。<br/>`interface TableAbnormalDragSortContext<T> { code: number; reason: string }`<br/> | N
onTreeExpandChange | Function | | TS 类型:`(context: TableTreeExpandChangeContext<T>) => void`<br/>树形结构,用户操作引起节点展开或收起时触发,代码操作不会触发。[详细类型定义](https://github.com/Tencent/tdesign-vue/tree/develop/src/table/type.ts)。<br/>`interface TableTreeExpandChangeContext<T> { row: T; rowIndex: number; rowState: TableRowState<T> }`<br/> | N
onTreeExpandChange | Function | | TS 类型:`(context: TableTreeExpandChangeContext<T>) => void`<br/>树形结构,用户操作引起节点展开或收起时触发,代码操作不会触发。[详细类型定义](https://github.com/Tencent/tdesign-vue/tree/develop/src/table/type.ts)。<br/>`interface TableTreeExpandChangeContext<T> { row: T; rowIndex: number; rowState: TableRowState<T>; trigger?: 'expand-fold-icon' }`<br/> | N

### EnhancedTable Events

名称 | 参数 | 描述
-- | -- | --
abnormal-drag-sort | `(context: TableAbnormalDragSortContext<T>)` | 异常拖拽排序时触发,如:树形结构中,非同层级之间的交换。`context.code` 指交换异常错误码,固定值;`context.reason` 指交换异常的原因。[详细类型定义](https://github.com/Tencent/tdesign-vue/tree/develop/src/table/type.ts)。<br/>`interface TableAbnormalDragSortContext<T> { code: number; reason: string }`<br/>
tree-expand-change | `(context: TableTreeExpandChangeContext<T>)` | 树形结构,用户操作引起节点展开或收起时触发,代码操作不会触发。[详细类型定义](https://github.com/Tencent/tdesign-vue/tree/develop/src/table/type.ts)。<br/>`interface TableTreeExpandChangeContext<T> { row: T; rowIndex: number; rowState: TableRowState<T> }`<br/>
tree-expand-change | `(context: TableTreeExpandChangeContext<T>)` | 树形结构,用户操作引起节点展开或收起时触发,代码操作不会触发。[详细类型定义](https://github.com/Tencent/tdesign-vue/tree/develop/src/table/type.ts)。<br/>`interface TableTreeExpandChangeContext<T> { row: T; rowIndex: number; rowState: TableRowState<T>; trigger?: 'expand-fold-icon' }`<br/>

### EnhancedTableInstanceFunctions 组件实例方法

Expand Down
6 changes: 3 additions & 3 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** Vue2 特有全局变量 */
/** Vue2 特有全局类型 */

export type TNodeReturnValue = import('vue/types/vnode').ScopedSlotReturnValue;
export type TNode<T = undefined> = T extends undefined
Expand All @@ -10,7 +10,7 @@ export type JsxNode = TNodeReturnValue;
export type AttachNodeReturnValue = HTMLElement | Element | Document;
export type AttachNode = CSSSelector | ((triggerNode?: HTMLElement) => AttachNodeReturnValue);

// 与滚动相关的容器类型,因为 document 上没有 scroll 相关属性, 因此排除document
// 与滚动相关的容器类型,因为 document 上没有 scroll 相关属性, 因此排除 document
export type ScrollContainerElement = Window | HTMLElement;
export type ScrollContainer = (() => ScrollContainerElement) | CSSSelector;

Expand All @@ -21,7 +21,7 @@ export type FormSubmitEvent = Event;
export interface Styles {
[css: string]: string | number;
}
/** 通用全局变量 */
/** 通用全局类型 */

export type OptionData = {
label?: string;
Expand Down
25 changes: 23 additions & 2 deletions src/table/hooks/tree-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,13 @@ class TableTreeStore<T extends TableRowData = TableRowData> {
if (!parentExpanded) {
newData.push(item);
}
this.treeDataMap.set(rowValue, state);
if (children?.length && !originalExpanded) {
// 同步更新父元素的展开数量
let tmpParent = parent;
while (tmpParent) {
while (tmpParent?.row) {
tmpParent.expandChildrenLength += children.length;
this.treeDataMap.set(tmpParent.id, tmpParent);
tmpParent = tmpParent.parent;
}
// 继续子元素
Expand Down Expand Up @@ -419,7 +421,7 @@ class TableTreeStore<T extends TableRowData = TableRowData> {
}
const children = get(item, keys.childrenKey);
if (children?.length) {
this.expandAll(children, keys);
this.foldAll(children, keys);
}
}
return newData;
Expand Down Expand Up @@ -469,6 +471,25 @@ class TableTreeStore<T extends TableRowData = TableRowData> {
}
}

// column.checkProps 和 column.disabled 会影响行的禁用状态,因此当列发生变化时,需要重置禁用状态
updateDisabledState(dataSource: T[], column: PrimaryTableCol, keys: KeysType) {
for (let i = 0, len = dataSource.length; i < len; i++) {
const item = dataSource[i];
const rowValue = get(item, keys.rowKey);
if (rowValue === undefined) {
log.error('EnhancedTable', '`rowKey` could be wrong, can not get rowValue from `data` by `rowKey`.');
return;
}
const state = this.treeDataMap.get(rowValue);
state.disabled = isRowSelectedDisabled(column, item, i);
this.treeDataMap.set(rowValue, state);
const children = get(item, keys.childrenKey);
if (children?.length) {
this.updateDisabledState(children, column, keys);
}
}
}

/**
* 校验数据合法性
*/
Expand Down
33 changes: 20 additions & 13 deletions src/table/hooks/useTreeData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ export default function useTreeData(props: TdEnhancedTableProps, context: SetupC
childrenKey: props.tree?.childrenKey || 'children',
}));

const checkedColumn = computed(() => columns.value.find((col) => col.colKey === 'row-select'));

watch(checkedColumn, (column) => {
if (!store.value) return;
store.value.updateDisabledState(dataSource.value, column, rowDataKeys.value);
});

function getFoldIcon(h: CreateElement) {
const params = { type: 'fold' };
const defaultFoldIcon = t(global.value.treeExpandAndFoldIcon, h, params) || <MinusRectangleIcon />;
Expand Down Expand Up @@ -95,19 +102,19 @@ export default function useTreeData(props: TdEnhancedTableProps, context: SetupC
* 组件实例方法,展开或收起某一行
* @param p 行数据
*/
function toggleExpandData(p: { row: TableRowData; rowIndex: number; trigger?: 'inner' }) {
function toggleExpandData(p: { row: TableRowData; rowIndex: number }, trigger?: 'expand-fold-icon') {
dataSource.value = store.value.toggleExpandData(p, dataSource.value, rowDataKeys.value);
if (p?.trigger === 'inner') {
const rowValue = get(p.row, rowDataKeys.value.rowKey);
const params = {
row: p.row,
rowIndex: p.rowIndex,
rowState: store.value?.treeDataMap?.get(rowValue),
};
props.onTreeExpandChange?.(params);
// Vue3 ignore next line
context.emit('tree-expand-change', params);
}
const rowValue = get(p.row, rowDataKeys.value.rowKey);
const rowState = store.value?.treeDataMap?.get(rowValue);
const params = {
row: p.row,
rowIndex: p.rowIndex,
rowState,
trigger,
};
props.onTreeExpandChange?.(params);
// Vue3 ignore next line
context.emit('tree-expand-change', params);
}

function getTreeNodeColumnCol() {
Expand Down Expand Up @@ -139,7 +146,7 @@ export default function useTreeData(props: TdEnhancedTableProps, context: SetupC
return (
<div class={[tableTreeClasses.col, classes]} style={colStyle}>
{!!childrenNodes.length && (
<span class={tableTreeClasses.icon} onClick={() => toggleExpandData({ ...p, trigger: 'inner' })}>
<span class={tableTreeClasses.icon} onClick={() => toggleExpandData(p, 'expand-fold-icon')}>
{iconNode}
</span>
)}
Expand Down
1 change: 1 addition & 0 deletions src/table/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ export interface TableTreeExpandChangeContext<T> {
row: T;
rowIndex: number;
rowState: TableRowState<T>;
trigger?: 'expand-fold-icon';
}

export type TableRowValue = string | number;
Expand Down