Skip to content
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

Provide display options for columnType #34

Open
jeffreygaggino opened this issue Feb 21, 2024 · 6 comments
Open

Provide display options for columnType #34

jeffreygaggino opened this issue Feb 21, 2024 · 6 comments
Assignees
Labels
advice Information how to use/implement the component

Comments

@jeffreygaggino
Copy link

Currently when you want to display a "$" sign or "%" you've got to change the raw data:

 customColumnTypes='[
  {
    "name": "Custom type",
    "customTextProcessing": {
      "changeTextFunc": "(cellText) => `$${Number(cellText.replace(/[^0-9.-]+/g,\"\"))}`",
      "changeStyleFunc": "(cellText) => {
        const moneyNumber = Number(cellText.replace(/[^0-9.-]+/g,\"\"));
        if (moneyNumber < 20) return {backgroundColor: `#ff3232`};
        if (moneyNumber < 100) return {backgroundColor: `#ffff00`};
        return {backgroundColor: `#00ac00`};
      }"
  }}]'

So for example 0.12 should show as 12%. However to get it working you need to change the actual value to 12%. This is bad when when you have to send it back to a server.

Recommendation for a "changeDisplayFunc" function. It would happen after changeTextFunc (for example cleaning data to only display numbers). In the example 0.12:


customColumnTypes='[
  {
    "name": "Percentage",
    "customTextProcessing": {
      "changeDisplayFunc": (cellText) => `${Number(cellText) * 100 }%`
  }}]'

@jeffreygaggino
Copy link
Author

Love working with the system by the way much easier to use than tanstack tables. Especially for working with simple row data.

@OvidijusParsiunas
Copy link
Owner

Hi @jeffreygaggino.

The changeTextFunc property already serves as a post-processor/cleanup function.

I don't think I fully understand the problem that you are describing, could you elaborate on how does it make it difficult to send data to the server? Or could you perhaps illustrate the step by step process that is causing the difficulty.

Thanks!

@OvidijusParsiunas OvidijusParsiunas self-assigned this Feb 21, 2024
@OvidijusParsiunas OvidijusParsiunas added the advice Information how to use/implement the component label Feb 21, 2024
@jeffreygaggino
Copy link
Author

jeffreygaggino commented Feb 21, 2024

Front end wise I want to show 12% to the user. At least from my understanding with using the table it seems like I've got to change the actual data to "12%". I would rather want to keep the data to be 0.12 and show to the user "12%". So have a function that doesn't mutate the original value of 0.12 but allows regex / text transformations to display 0.12 as "12%". When they edit it reverts back to the "real" value of 0.12

@jeffreygaggino
Copy link
Author

Here's a couple of images to illustrate this:
Editing:
Screenshot 2024-02-22 at 8 38 20 am

Display when not editing:
Screenshot 2024-02-22 at 8 38 36 am

@OvidijusParsiunas
Copy link
Owner

Hey @jeffreygaggino.

Active Table's internal code has been built to only work with actual values from the table cells, hence it is unable to handle pseudo data. For your case, you will unfortunately have to either maintain some sort of an alternative table in your app's state with the help of onCellUpdate or onDataUpdate events where you can postprocess the changed data to your liking or you can alternatively preprocess the data before it goes to your server.

In regards to the two images above, I can see the value is turned into a percentage - except the percentage is processed incorrectly. You will need to upgrade the changeTextFunc function for better processing, e.g (raw JavaScript):

changeTextFunc: (cellText) => {
  if (/^\d*\.?\d+%$/.test(cellText)) {
    return cellText;
  }
  return `${(Number(cellText.replace(/[^0-9.]+/g, '')) * 100).toFixed(2)}%`;
}

This code can be further enhanced for better percentage processing.

@calin-dobos
Copy link

Hi,

From what I understand changeTextFunc is only called when the cell value actually changes. Exposing "cell-focus" and "cell-blur" events might allow two way conversion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
advice Information how to use/implement the component
Projects
None yet
Development

No branches or pull requests

3 participants