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

Conversation

gfoidl
Copy link
Contributor

@gfoidl gfoidl commented Jun 27, 2025

The table data items are displayed by simple toString (internally).

This PR adds the optional property formatter, so you can do something like

function formatTimeStamp(value: Date): string {
    return value.toLocaleString();
}

function formatNumber(value: number): string {
    return value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 1 });
}

const columns = [
    { key: "timeStampUtc", label: "Timestamp", formatter: formatTimeStamp},
    { key: "mean"        , label: "Mean"     , formatter: formatNumber   },
    { key: "median"      , label: "Median"   , formatter: formatNumber   },
    { key: "sigma"       , label: "Sigma"    , formatter: formatNumber   }
];

in order to specify a formatter, that will convert each item's property according the given formatter-function.

Note

It's optional to specify the formatter

Comment on lines +255 to +260
const column = props.columns![idx];
const value = item[colName];

return typeof column === "object" && column.formatter
? column.formatter(value)
: value;
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant