anvilistas / anvil-extras

Other
76 stars 21 forks source link

Why Aren't Pivot Table Properties Applied When Configured via Client-Side Code in Anvil? #531

Open jrperassoli opened 1 week ago

jrperassoli commented 1 week ago

Hi,

I'm attempting to configure the properties of the pivot table using client-side code, but the settings are not applied when the table is loaded. However, these properties work perfectly when adjusted directly from the Anvil IDE. Why don't these settings take effect when made from the client-side code?

Example of code that is not working:

class Form1(Form1Template):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)
    json_data = anvil.server.call('df')
    # Convert JSON string to list of dictionaries
    items = json.loads(json_data)
    # Set the items for the pivot table
    self.pivot_1.items = items
    # Configure your pivot table here
    self.pivot_1.rows = ["sectors"]
s-cork commented 1 week ago

Looking at the code it's just not implemented. Only options passed as properties during instantiation have an effect. And code to update those options doesn't exist. Are you interested in making a PR? Or do you want this to be a feature request for someone else to implement?

If you want to look at writing a PR we can point you in the right directions.

meatballs commented 1 week ago

Yep, that's not implemented

jrperassoli commented 1 week ago

Hello, thank you for your quick reply. Yes, I'm interested in making a PR. Could you please tell me which folder I should modify the code in? Thank you.

s-cork commented 1 week ago

It'll be in the client_code for the pivot component.

You'll need to add a property for rows. Then you'll need to re-init the jquery pivot table.

If you search jquery pivot table online you'll find documentation for the pivot table. But i think all you need is to call the init table method with the new options.

I looked at some docs and the call to pivotUi will need a third argument set to True which is the override argument. pivotUI(dom, options, True)

Note the pivot component doesn't store the properties it was given so you'll need to store them.

Some other components store these as self._props. So if you look at examples that do this then you should also find some properties that update the _props.

That might not have been clear enough so shout if you need more details.

jrperassoli commented 1 week ago

Does it make sense to include this function?

def update_properties_and_refresh(self, **new_properties):
        for prop, value in new_properties.items():
            if prop in self.option_names:
                self.pivot_options[prop] = value
        self._init_pivot()  # Re-initialize the pivot table with updated properties

To later call it with:

self.pivot_1.update_properties_and_refresh(rows=["new"], columns=["new"])

s-cork commented 1 week ago

I would just call it update. And I would simplify the code as

self.pivot_options.update(new_options)

jrperassoli commented 1 week ago

The code works!

s-cork commented 1 week ago

great.

If you need help with creating the PR let us know.