What's the idiotmatic way to add a form filter to many liveview pages?

I have a component that displays two select drop downs. One to filter by date range and one to filter by category. I have several different live views, some show tabular data and others show graphs. How do I reuse these two select fields across drop downs?

I already made them as a component, but I have to duplicate the handle_event function in each live view. If I make a live component instead, to store the state, then I still have to pass the values back to each live view. Either way seems like a lot of duplication and I’m not sure it’s the correct way.


You can attach hooks to all views in a live session.

I don’t have an idiomatic answer but I can point to some solutions that are used.

Some use Flop and Flop.Pheonix components for sorting, filtering and pagination with Phoenix and Ecto.

I have been experimenting using Ash framework so some advanced dynamic filtering with LiveView can be achieved using AshPhoenix.FilterForm.

Depending on your use case you may also have to consider authorisation and visibility of data based on tenant, user role/permissions and resource scopes. That’s one reason why I have been using Ash because of the policy engine.

Thank you both, that’s very helpful!