Skip to content

Commit

Permalink
feat: ✨引入table的列合并功能
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyjone committed Apr 17, 2023
1 parent c383283 commit 6f48a20
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 12 deletions.
11 changes: 9 additions & 2 deletions demo/demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@
<x-gantt-column label="group1">
<x-gantt-column prop="id" width="200px"></x-gantt-column>
<x-gantt-column label="group2">
<x-gantt-column prop="name"></x-gantt-column>
<x-gantt-column prop="name2"></x-gantt-column>
<x-gantt-column
prop="name"
:merge="(scope: any) => scope.$index % 3 === 0"
></x-gantt-column>
<x-gantt-column
prop="name2"
:merge="(scope: any) => scope.$index % 2 === 0"
>n1</x-gantt-column
>
</x-gantt-column>
</x-gantt-column>

Expand Down
26 changes: 24 additions & 2 deletions src/components/column/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div
class="xg-table-cell"
:style="{
width: getColumnWidth(props?.width ?? Variables.default.tableColumnWidth),
width: `${realWidth}px`,
...$styleBox.getBorderColor()
}"
>
Expand All @@ -32,9 +32,10 @@
<script lang="ts">
import Variables from '@/constants/vars';
import columnProps from './props';
import { defineComponent, useSlots } from 'vue';
import { defineComponent, useSlots, computed } from 'vue';
import { getColumnWidth } from './util';
import useStyle from '@/composables/useStyle';
import useSlotsBox from '@/composables/useSlotsBox';
export default defineComponent({
name: Variables.name.column
Expand All @@ -47,6 +48,27 @@ const slots = useSlots();
const { $styleBox, rowHeight } = useStyle();
const { $slotsBox, isMerge } = useSlotsBox();
const realWidth = computed(() => {
let curWidth = getColumnWidth(
props?.width ?? Variables.default.tableColumnWidth
);
for (let i = (props.__index ?? 1) + 1; i < $slotsBox.cols.length; i++) {
const col = $slotsBox.cols[i];
if (isMerge(col.props?.merge, props.data!)) {
curWidth += getColumnWidth(
col.props?.width ?? Variables.default.tableColumnWidth
);
} else {
break;
}
}
return curWidth;
});
// TODO: 合并列通过这个实现
// const colspan = ref(1);
</script>
Expand Down
12 changes: 11 additions & 1 deletion src/components/column/props.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Variables from '@/constants/vars';
import RowItem from '@/models/data/row';
import { type PropType } from 'vue';

export default {
/**
Expand All @@ -20,6 +21,15 @@ export default {
*/
label: String,

/**
* 是否合并,一个函数,抛出当前数据,接收true / false,true为合并当前行,与前置列合并
*/
merge: {
type: [Function, Boolean] as PropType<boolean | ((data: any) => boolean)>,
default: () => false
},

// ********* 内部参数 ********* //
data: RowItem
data: RowItem,
__index: Number
};
2 changes: 1 addition & 1 deletion src/components/column/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const getColumnWidth = (width: number | string) => {
return `${typeof width === 'number' ? width : Number.parseInt(width)}px`;
return typeof width === 'number' ? width : Number.parseInt(width);
};
11 changes: 8 additions & 3 deletions src/components/common/TableBody.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@
...$styleBox.getBorderColor()
}"
>
<template v-for="(c, i) in $slotsBox.cols" :key="i">
<component :is="c" :data="d" />
<template
v-for="(c, i) in $slotsBox.cols.filter(
v => !isMerge(v.props?.merge, d as any)
)"
:key="i"
>
<component :is="c" :data="d" :__index="i" />
</template>
</div>
</div>
Expand All @@ -64,7 +69,7 @@ import useStyle from '@/composables/useStyle';
const props = defineProps<{ gap: number }>();
const { $slotsBox } = useSlotsBox();
const { $slotsBox, isMerge } = useSlotsBox();
const { bodyHeight, rowHeight, $styleBox } = useStyle();
const { inView } = useInView();
</script>
Expand Down
4 changes: 3 additions & 1 deletion src/components/common/TableHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
<template v-for="(c, i) in $slotsBox.cols" :key="i">
<col
:width="
getColumnWidth(c.props?.width ?? Variables.default.tableColumnWidth)
getColumnWidth(
c.props?.width ?? Variables.default.tableColumnWidth
) + 'px'
"
/>
</template>
Expand Down
11 changes: 10 additions & 1 deletion src/composables/useData.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type RowItem from '@/models/data/row';
import { useStore } from '@/store';
import { computed, watch, type Ref } from 'vue';
import useGanttWidth from './useGanttWidth';
Expand Down Expand Up @@ -36,10 +37,18 @@ export default () => {
);
}

function toRowData(data: RowItem) {
return {
row: data.data,
$index: data.flatIndex
};
}

return {
$data: store.$data,
initData,
dateList: computed(() => store.ganttHeader.headers),
setGanttHeaders
setGanttHeaders,
toRowData
};
};
13 changes: 12 additions & 1 deletion src/composables/useSlotsBox.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type RowItem from '@/models/data/row';
import { useStore } from '@/store';
import { type Slots } from 'vue';
import useData from './useData';
import useTableWidth from './useTableWidth';

export default () => {
Expand All @@ -14,5 +16,14 @@ export default () => {
setTableWidth(store.$slotsBox.cols);
}

return { $slotsBox: store.$slotsBox, setSlots };
const { toRowData } = useData();

function isMerge(
m: boolean | undefined | ((data: any) => boolean),
data: RowItem
) {
return typeof m === 'function' ? m(toRowData(data)) : !!m;
}

return { $slotsBox: store.$slotsBox, setSlots, isMerge };
};

0 comments on commit 6f48a20

Please sign in to comment.