Skip to content

Added formatter to column object #303

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 9 additions & 2 deletions packages/coreui-vue/src/components/table/CTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ const CTable = defineComponent({
default: () => [
columnNames.value &&
columnNames.value.map(
(colName: string) =>
(colName: string, idx: number) =>
item[colName] !== undefined &&
h(
CTableDataCell,
Expand All @@ -251,7 +251,14 @@ const CTable = defineComponent({
}),
},
{
default: () => item[colName],
default: () => {
const column = props.columns![idx];
const value = item[colName];

return typeof column === "object" && column.formatter
? column.formatter(value)
: value;
Comment on lines +255 to +260
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that there may be better ways, but that would need quite a bit of code-change.
This was tested with a rather big table, and no perf-problems did occur, so I'm fine with this approach.

}
},
),
),
Expand Down
3 changes: 2 additions & 1 deletion packages/coreui-vue/src/components/table/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export type Column = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_style?: any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_props?: any
_props?: any,
formatter?: (value: unknown) => string
}

export type FooterItem = {
Expand Down