active-table icon indicating copy to clipboard operation
active-table copied to clipboard

Provide display options for columnType

Open jeffreygaggino opened this issue 1 year ago • 6 comments

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 avatar Feb 21 '24 07:02 jeffreygaggino

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

jeffreygaggino avatar Feb 21 '24 07:02 jeffreygaggino

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 avatar Feb 21 '24 15:02 OvidijusParsiunas

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 avatar Feb 21 '24 19:02 jeffreygaggino

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

jeffreygaggino avatar Feb 21 '24 22:02 jeffreygaggino

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.

OvidijusParsiunas avatar Feb 22 '24 00:02 OvidijusParsiunas

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.

calin-dobos avatar Mar 05 '24 18:03 calin-dobos