Skip to content

Commit

Permalink
feat(table): formatter添加index信息
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Feb 18, 2025
1 parent 6f2c6b1 commit 4d77b66
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
4 changes: 3 additions & 1 deletion packages/table/src/PopoverColumn.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
:data="(config.prop && scope.row[config.prop]) || []"
></MTable>
<template #reference>
<TMagicButton link type="primary"> {{ config.text || formatter(config, scope.row) }}</TMagicButton>
<TMagicButton link type="primary">
{{ config.text || formatter(config, scope.row, { index: scope.$index }) }}</TMagicButton
>
</template>
</TMagicPopover>
</template>
Expand Down
8 changes: 4 additions & 4 deletions packages/table/src/TextColumn.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
type="primary"
@click="config.handler?.(scope.row)"
>
<span v-html="formatter(config, scope.row)"></span>
<span v-html="formatter(config, scope.row, { index: scope.$index })"></span>
</TMagicButton>

<a v-else-if="config.action === 'img' && config.prop" target="_blank" :href="scope.row[config.prop]"
Expand All @@ -46,7 +46,7 @@

<el-tooltip v-else-if="config.action === 'tip'" placement="left">
<template #content>
<div>{{ formatter(config, scope.row) }}</div>
<div>{{ formatter(config, scope.row, { index: scope.$index }) }}</div>
</template>
<TMagicButton link type="primary">{{ config.buttonText || '扩展配置' }}</TMagicButton>
</el-tooltip>
Expand All @@ -55,9 +55,9 @@
v-else-if="config.action === 'tag' && config.prop"
:type="typeof config.type === 'function' ? config.type(scope.row[config.prop], scope.row) : config.type"
close-transition
>{{ formatter(config, scope.row) }}</TMagicTag
>{{ formatter(config, scope.row, { index: scope.$index }) }}</TMagicTag
>
<div v-else v-html="formatter(config, scope.row)"></div>
<div v-else v-html="formatter(config, scope.row, { index: scope.$index })"></div>
</template>
</TMagicTableColumn>
</template>
Expand Down
2 changes: 1 addition & 1 deletion packages/table/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface ColumnConfig<T = any> {
name?: string;
showHeader?: boolean;
table?: ColumnConfig[];
formatter?: 'datetime' | ((item: any, row: T) => any);
formatter?: 'datetime' | ((item: any, row: T, data: { index: number }) => any);
popover?: {
placement?:
| 'auto'
Expand Down
4 changes: 2 additions & 2 deletions packages/table/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { datetimeFormatter } from '@tmagic/form';

import type { ColumnConfig } from './schema';

export const formatter = (item: ColumnConfig, row: any) => {
export const formatter = (item: ColumnConfig, row: any, data: { index: number }) => {
if (!item.prop) return '';

if (item.formatter) {
Expand All @@ -29,7 +29,7 @@ export const formatter = (item: ColumnConfig, row: any) => {
item.formatter = (value: string) => datetimeFormatter(value);
}
try {
return item.formatter(row[item.prop], row);
return item.formatter(row[item.prop], row, data);
} catch (e) {
return row[item.prop];
}
Expand Down

0 comments on commit 4d77b66

Please sign in to comment.