feat(table): expand内容支持vue组件

This commit is contained in:
roymondchen 2022-11-18 18:10:17 +08:00
parent 813ca55ef6
commit aa9293af55
4 changed files with 44 additions and 3 deletions

View File

@ -5,6 +5,8 @@
v-bind="uiProps" v-bind="uiProps"
@select="selectHandler" @select="selectHandler"
@sort-change="sortChangeHandler" @sort-change="sortChangeHandler"
@expand-change="expandChangeHandler"
@cell-click="cellClickHandler"
> >
<slot></slot> <slot></slot>
</component> </component>
@ -31,7 +33,7 @@ const uiComponent = getConfig('components').table;
const uiProps = computed(() => uiComponent.props(props)); const uiProps = computed(() => uiComponent.props(props));
const emit = defineEmits(['select', 'sort-change']); const emit = defineEmits(['select', 'sort-change', 'expand-change', 'cell-click']);
const table = ref<any>(); const table = ref<any>();
@ -43,6 +45,14 @@ const sortChangeHandler = (...args: any[]) => {
emit('sort-change', ...args); emit('sort-change', ...args);
}; };
const expandChangeHandler = (...args: any[]) => {
emit('expand-change', ...args);
};
const cellClickHandler = (...args: any[]) => {
emit('cell-click', ...args);
};
let $el: HTMLDivElement | undefined; let $el: HTMLDivElement | undefined;
watchEffect(() => { watchEffect(() => {

View File

@ -13,6 +13,7 @@
:init-values="config.values || (config.prop && scope.row[config.prop]) || {}" :init-values="config.values || (config.prop && scope.row[config.prop]) || {}"
></MForm> ></MForm>
<div v-if="config.expandContent" v-html="config.expandContent(scope.row, config.prop)"></div> <div v-if="config.expandContent" v-html="config.expandContent(scope.row, config.prop)"></div>
<component v-if="config.component" :is="config.component" v-bind="componentProps(scope.row)"></component>
</template> </template>
</TMagicTableColumn> </TMagicTableColumn>
</template> </template>
@ -24,7 +25,7 @@ import { MForm } from '@tmagic/form';
import { ColumnConfig } from './schema'; import { ColumnConfig } from './schema';
import MTable from './Table.vue'; import MTable from './Table.vue';
withDefaults( const props = withDefaults(
defineProps<{ defineProps<{
config: ColumnConfig; config: ColumnConfig;
}>(), }>(),
@ -32,4 +33,11 @@ withDefaults(
config: () => ({}), config: () => ({}),
}, },
); );
const componentProps = (row: any) => {
if (typeof props.config.props === 'function') {
return props.config.props(row) || {};
}
return props.config.props || {};
};
</script> </script>

View File

@ -17,6 +17,8 @@
@select="selectHandler" @select="selectHandler"
@select-all="selectAllHandler" @select-all="selectAllHandler"
@selection-change="selectionChangeHandler" @selection-change="selectionChangeHandler"
@cell-click="cellClickHandler"
@expand-change="expandChange"
> >
<template v-for="(item, columnIndex) in columns"> <template v-for="(item, columnIndex) in columns">
<template v-if="item.type === 'expand'"> <template v-if="item.type === 'expand'">
@ -94,7 +96,15 @@ const props = withDefaults(
}, },
); );
const emit = defineEmits(['sort-change', 'afterAction', 'select', 'select-all', 'selection-change']); const emit = defineEmits([
'sort-change',
'afterAction',
'select',
'select-all',
'selection-change',
'expand-change',
'cell-click',
]);
const tMagicTable = ref<InstanceType<typeof TMagicTable>>(); const tMagicTable = ref<InstanceType<typeof TMagicTable>>();
@ -141,6 +151,14 @@ const selectionChangeHandler = (selection: any) => {
emit('selection-change', selection); emit('selection-change', selection);
}; };
const cellClickHandler = (...args: any[]) => {
emit('cell-click', ...args);
};
const expandChange = (...args: any[]) => {
emit('expand-change', ...args);
};
const toggleRowSelection = (row: any, selected: boolean) => { const toggleRowSelection = (row: any, selected: boolean) => {
tMagicTable.value?.toggleRowSelection(row, selected); tMagicTable.value?.toggleRowSelection(row, selected);
}; };

View File

@ -55,5 +55,10 @@ export type ColumnConfig = {
sortable?: boolean | 'custom'; sortable?: boolean | 'custom';
action?: 'tip' | 'actionLink' | 'img' | 'link' | 'tag'; action?: 'tip' | 'actionLink' | 'img' | 'link' | 'tag';
handler?: (row: any) => void; handler?: (row: any) => void;
/** 当type为expand时有效展开为html */
expandContent?: (row: any, prop?: string) => string; expandContent?: (row: any, prop?: string) => string;
/** 当type为expand时有效展开为vue组件 */
component?: any;
/** 当type为expand时有效展开的vue组件props */
props?: any;
}; };