muonw / muonw-powertable

▦ PowerTable is a Svelte component that turns JSON data into an interactive HTML table. Inspired by DataTables. Powered by Svelte.
https://muonw.github.io/muonw-powertable/
Other
220 stars 13 forks source link

Pass original data to customSort function #31

Closed powellnorma closed 1 year ago

powellnorma commented 1 year ago

Currently the data seems to get converted to lowercased strings, even for e.g. objects. I currently use JSON.parse to restore the object, and then try to remember to use lowercase instead of camelCase (even if the original object used camelCase for its attributes). Or maybe I am missing something? Thank you!

powellnorma commented 1 year ago

Perhaps one could even expose the cell component somehow? In case we use:

parseAs: 'component',
dataComponent: MyComponent,

Then it would be possible to merge the logic for "display" and "sort" in one function. In my case, I calculate the price of something, and for that have to do a lookup in a global dict to find the current price.

muonw-public commented 1 year ago

Currently the data seems to get converted to lowercased strings, even for e.g. objects. I currently use JSON.parse to restore the object, and then try to remember to use lowercase instead of camelCase (even if the original object used camelCase for its attributes). Or maybe I am missing something? Thank you!

You can convert your objects to camelCase strings before passing them to PowerTable. This is unlikely to become a built-in functionality.

powellnorma commented 1 year ago

You can convert your objects to camelCase strings before passing them to PowerTable. This is unlikely to become a built-in functionality.

But wouldn't it make sense to give the customSort function access the original data instead of a transformed version? I mean the original values still exist in memory etc. Or are there technical reasons for why this would be hard to accomplish?

muonw-public commented 1 year ago

The customSort function compares two string values in order to decide the order of rows. If your original data has property values that are not strings (e.g. boolean, number, object, array) they have to be first converted to string. Because without transforming all values to strings, we might face a situation in which one side of the comparison is a nested object and the other side is an array, for example. But, I agree that we should revisit this issue once we find an efficient way to sort the original data and skip the conversion.

powellnorma commented 1 year ago

Ok, two ideas:

  1. What about just passing the index as an additional parameter? That way the (user provided) customSort function could retrieve the data from ptData
  2. Maybe the conversion to a string could be done in the sort function, as opposed to the caller of the sort function? (Not sure if that makes sense, I haven't looked at the architecture of PT too much etc)
muonw-public commented 1 year ago

These ideas are also worth looking further into. Depending on the implementation, the trade-off might be acceptable. If you come up with ways to implement them, please share your solutions here, so that we can discuss the details.