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

fix(pro:table): resize one column to make the other columns too narrow #1276

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion packages/components/table/src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default defineComponent({
const sortableContext = useSortable(columnsContext.flattedColumns)
const filterableContext = useFilterable(columnsContext.flattedColumns)
const expandableContext = useExpandable(props, columnsContext.flattedColumns)
const tableLayout = useTableLayout(props, columnsContext, scrollContext, stickyContext.isSticky)
const tableLayout = useTableLayout(props, columnsContext, scrollContext, stickyContext.isSticky, mergedAutoHeight)

const { activeSorters } = sortableContext
const { activeFilters } = filterableContext
Expand Down
2 changes: 1 addition & 1 deletion packages/components/table/src/composables/useColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ function useColumnWidths(
return { columnWidths, columnWidthsWithScrollBar, changeColumnWidth }
}

function useColumnOffsets(columnWidths: ComputedRef<number[]>, columnWidthsWithScrollBar: ComputedRef<number[]>) {
function useColumnOffsets(columnWidths: Ref<number[]>, columnWidthsWithScrollBar: ComputedRef<number[]>) {
const columnOffsets = computed(() => calculateOffsets(columnWidths.value))
const columnOffsetsWithScrollBar = computed(() => calculateOffsets(columnWidthsWithScrollBar.value))
return { columnOffsets, columnOffsetsWithScrollBar }
Expand Down
3 changes: 2 additions & 1 deletion packages/components/table/src/composables/useTableLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function useTableLayout(
{ hasEllipsis, hasFixed }: ColumnsContext,
{ scrollWidth, scrollHeight }: ScrollContext,
isSticky: ComputedRef<boolean>,
mergedAutoHeight: ComputedRef<boolean>,
): ComputedRef<'auto' | 'fixed'> {
return computed(() => {
if (props.tableLayout) {
Expand All @@ -24,7 +25,7 @@ export function useTableLayout(
if (scrollWidth.value && hasFixed.value) {
return scrollWidth.value === 'max-content' ? 'auto' : 'fixed'
}
if (scrollHeight.value || isSticky.value || hasEllipsis.value || props.virtual) {
if (scrollHeight.value || mergedAutoHeight.value || isSticky.value || hasEllipsis.value || props.virtual) {
return 'fixed'
}
return 'auto'
Expand Down
17 changes: 16 additions & 1 deletion packages/components/table/src/main/ColGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,22 @@ export default defineComponent({
[`${prefixCls}-col-with-dropdown`]: type === 'selectable' && mergedSelectableMenus.value.length > 0,
})
: undefined
const style = mergedWidth ? `width: ${convertCssPixel(mergedWidth)}` : undefined
let style: string | Record<string, string> | undefined
if (isFixedHolder) {
style = mergedWidth ? `width: ${convertCssPixel(mergedWidth)}` : undefined
} else {
style = {
width: convertCssPixel(mergedWidth),
// for proTable: resizable, minWidth and maxWidth
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
minWidth: convertCssPixel(column.minWidth),
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
maxWidth: convertCssPixel(column.maxWidth),
}
}

return <col key={key} class={className} style={style}></col>
})

Expand Down
10 changes: 5 additions & 5 deletions packages/components/table/src/main/MainTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ export default defineComponent({
}

return () => {
const children = slots.default ? slots.default() : []

const prefixCls = mergedPrefixCls.value
const autoHeight = mergedAutoHeight.value
const children = slots.default ? slots.default() : []

if (scrollHeight.value || isSticky.value) {
if (autoHeight || scrollHeight.value || isSticky.value) {
const { offsetTop } = mergedSticky.value
if (!props.headless) {
children.push(
Expand All @@ -164,7 +164,7 @@ export default defineComponent({
)
}

if (props.virtual && (props.scroll || mergedAutoHeight.value)) {
if (props.virtual && (props.scroll || autoHeight)) {
const itemRender: VirtualItemRenderFn<FlattedData> = ({ item, index }) =>
renderBodyRow(item, index, slots, expandable.value, prefixCls)

Expand All @@ -179,7 +179,7 @@ export default defineComponent({
}
const { scroll, onScrolledBottom } = props

if (__DEV__ && !props.autoHeight && !isNumber(scroll?.height)) {
if (__DEV__ && !autoHeight && !isNumber(scroll?.height)) {
Logger.warn('components/table', '`scroll.height` must is a valid number when enable virtual scroll')
}

Expand Down
5 changes: 4 additions & 1 deletion packages/pro/table/demo/Resizable.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ title:

## zh

可以通过配置 `resizable`,开启拖拽调整列宽,还可以配置 `maxWidth` `minWidth` 来限制列宽的范围。
可以通过配置 `resizable`,开启拖拽调整列宽,还可以配置 `maxWidth` `minWidth` 来限制列宽的范围。
如果发现设置 `scroll.width` 后无法出现横向滚动条,拖拽一列后导致其他列变得过窄,请对未设置 `width` 的列设置一个 `minWidth`.

## en

Column width can be adjusted by configuring `resizable`, enabling drag and drop, and column width can be limited by configuring `maxWidth` and `minWidth`.

If you find that setting `scroll.width` does not allow horizontal scrollbar to appear, and dragging one column causes the other columns to become too narrow, set a `minWidth` for the column that is not set `width`.
15 changes: 11 additions & 4 deletions packages/pro/table/demo/Resizable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
:columns="columns"
:dataSource="data"
header="Pro Table"
:scroll="scroll"
:toolbar="toolbar"
@columnsChange="onColumnsChange"
>
Expand Down Expand Up @@ -49,27 +50,30 @@ const columns: ProTableColumn<Data>[] = [
dataKey: 'name',
changeFixed: false,
customCell: 'name',
resizable: true,
width: 150,
minWidth: 100,
maxWidth: 300,
resizable: true,
},
{
title: 'Age',
dataKey: 'age',
changeFixed: false,
width: 100,
resizable: true,
maxWidth: 120,
},
{
title: 'Address',
dataKey: 'address',
changeFixed: false,
width: 400,
minWidth: 200,
resizable: true,
maxWidth: 300,
minWidth: 150,
},
{
title: 'Tags',
dataKey: 'tags',
minWidth: 200,
customCell: ({ value }) =>
value.map((tag: string) => {
let color = tag.length > 5 ? 'warning' : 'success'
Expand All @@ -84,6 +88,7 @@ const columns: ProTableColumn<Data>[] = [
key: 'action',
changeIndex: false,
customCell: 'action',
width: 200,
},
]

Expand All @@ -100,4 +105,6 @@ for (let index = 0; index < 100; index++) {
} years old, living in London Park no. ${index}.`,
})
}

const scroll = { width: 1200 }
</script>
4 changes: 3 additions & 1 deletion packages/pro/table/src/ProTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ export default defineComponent({
}

return () => {
const { editable, customAdditional, customTag, layoutTool, toolbar, ...restProps } = props
const { editable, customAdditional, customTag, layoutTool, toolbar, tableLayout, ...restProps } = props
const resizable = hasResizable.value
const mergedTableLayout = tableLayout ? tableLayout : resizable ? 'fixed' : undefined

const mergedCustomAdditional: TableCustomAdditional = {
...customAdditional,
Expand All @@ -116,6 +117,7 @@ export default defineComponent({
customAdditional={mergedCustomAdditional}
customTag={mergedCustomTag}
size={mergedSize.value}
tableLayout={mergedTableLayout}
/>
)
}
Expand Down
16 changes: 8 additions & 8 deletions packages/pro/table/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,14 @@
height: calc(@table-head-row-height-sm / 2);
}
}
}

.cdk-resizable {
&-preview {
position: absolute;
top: 0;
left: 0;
z-index: 8;
border-right: @border-width-sm @border-style #d1d1d1;
.cdk-resizable {
&-preview {
position: absolute;
top: 0;
left: 0;
z-index: 8;
border-right: 2px @border-style #d1d1d1;
}
}
}