Skip to content

Commit

Permalink
feat: BasicTable 组件 HeaderCell 新增(类customRender) customHeaderRender 方…
Browse files Browse the repository at this point in the history
…法 自定义渲染支持 (#3030)

示例见: /comp/table/multipleHeader
  • Loading branch information
LanceJiang committed Sep 15, 2023
1 parent bd024cc commit bf2f639
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/components/Table/src/BasicTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
<slot :name="item" v-bind="data || {}"></slot>
</template>
<template #headerCell="{ column }">
<HeaderCell :column="column" />
<slot name="headerCell" v-bind="{ column }">
<HeaderCell :column="column" />
</slot>
</template>
<!-- 增加对antdv3.x兼容 -->
<template #bodyCell="data">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<span>
<span class="edit-header-cell">
<slot></slot>
{{ title }}
<FormOutlined />
Expand Down
32 changes: 22 additions & 10 deletions src/components/Table/src/components/HeaderCell.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
<template>
<EditTableHeaderCell v-if="getIsEdit">
{{ getTitle }}
</EditTableHeaderCell>
<span v-else>{{ getTitle }}</span>
<BasicHelp v-if="getHelpMessage" :text="getHelpMessage" :class="`${prefixCls}__help`" />
</template>
<script lang="ts">
<script lang="tsx">
import type { PropType } from 'vue';
import type { BasicColumn } from '../types/table';
import { defineComponent, computed } from 'vue';
Expand All @@ -29,10 +22,29 @@
const { prefixCls } = useDesign('basic-table-header-cell');
const getIsEdit = computed(() => !!props.column?.edit);
const getTitle = computed(() => props.column?.customTitle || props.column?.title);
const getTitle = computed(() => {
const column = props.column;
if (typeof column.customHeaderRender === 'function') {
return column.customHeaderRender(props.column);
}
return props.column?.customTitle || props.column?.title;
});
const getHelpMessage = computed(() => props.column?.helpMessage);
return { prefixCls, getIsEdit, getTitle, getHelpMessage };
return () => {
return (
<div>
{getIsEdit.value ? (
<EditTableHeaderCell>{getTitle.value}</EditTableHeaderCell>
) : (
<span class="default-header-cell">{getTitle.value}</span>
)}
{getHelpMessage.value && (
<BasicHelp text={getHelpMessage.value} class={`${prefixCls}__help`} />
)}
</div>
);
};
},
});
</script>
Expand Down
2 changes: 2 additions & 0 deletions src/components/Table/src/types/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ export interface BasicColumn extends ColumnProps<Recordable> {

slots?: Recordable;

// 自定义header渲染
customHeaderRender?: (column: BasicColumn) => string | VNodeChild | JSX.Element;
// Whether to hide the column by default, it can be displayed in the column configuration
defaultHidden?: boolean;

Expand Down
17 changes: 17 additions & 0 deletions src/views/demo/table/tableData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { optionsListApi } from '/@/api/demo/select';
import { FormProps, FormSchema } from '/@/components/Table';
import { BasicColumn } from '/@/components/Table/src/types/table';
import { VxeFormItemProps, VxeGridPropTypes } from '/@/components/VxeTable';
import { ref } from 'vue';
import { Input } from 'ant-design-vue';

export function getBasicColumns(): BasicColumn[] {
return [
Expand Down Expand Up @@ -73,6 +75,7 @@ export function getBasicShortColumns(): BasicColumn[] {
}

export function getMultipleHeaderColumns(): BasicColumn[] {
const testRef = ref('姓名:');
return [
{
title: 'ID',
Expand All @@ -81,6 +84,11 @@ export function getMultipleHeaderColumns(): BasicColumn[] {
},
{
title: '姓名',
customHeaderRender() {
return (
<Input placeholder="输入值 更新 自定义title" size="small" v-model:value={testRef.value} />
);
},
dataIndex: 'name',
width: 120,
},
Expand All @@ -91,6 +99,15 @@ export function getMultipleHeaderColumns(): BasicColumn[] {
children: [
{
title: '编号',
customHeaderRender(column) {
// 【自定义渲染的】
return (
<div>
_ <span style="background: #f00; color: #fff;">{testRef.value}</span> _
{column.customTitle}
</div>
);
},
dataIndex: 'no',
width: 120,
filters: [
Expand Down

0 comments on commit bf2f639

Please sign in to comment.