TNG / ngqp

Declaratively synchronize form controls with the URL
https://tng.github.io/ngqp
MIT License
81 stars 8 forks source link

Sync URL with nested QueryParamGroup #180

Closed verfault closed 3 years ago

verfault commented 3 years ago

Hello again!

Example: StackBlitz Version of "@ngqp/core": "1.0.0" Version of "@angular/core": "9.1.0"

Question Could you help me with one deal:

I need to sync nested FormGroups with URI by transforming it into QueryFormGroups but I've no idea how to handle it properly. Is there a way to add QueryFormGroups in existing QueryFormGroups and still to make it work?

I can't get it how to force my addControlToForm to use QueryParamGroup instead of regular FormGroup


Perhaps, the main things I've already explained by the code and in the code, so that would be quite difficult to explain the problem by text. So I'm sorry that I've done the description of the problem that short.


Thank you!

Airblader commented 3 years ago

Is there a way to add QueryFormGroups in existing QueryFormGroups and still to make it work?

QueryParamGroup does not support nesting groups into each other, because ultimately that doesn't make sense: the URL is one flat object.

I can't get it how to force my addControlToForm to use QueryParamGroup instead of regular FormGroup

This isn't possible. ngqp models its API after Angular Forms, but they are in no way "interchangeable". In fact, using both an Angular form and ngqp is not really what ngqp was originally intended to be used for (you either want to synchronize to the URL as you type, or have a submit-able form, but not both). See Limitations as well.


It is possible to combine Angular forms and ngqp, though. Unfortunately due to how Angular works there is no really good, smooth way to make that work out of the box. You essentially need to manually hook up the QueryParamGroup and FormGroup and synchronize between the two.

verfault commented 3 years ago

Okay, got it. So if I'd like to make a filtering with query params syncing there is no easy way to make that work out of the box, isn't there?


I mean in the context where filtering has some groups of options (which are represented as checboxes) and each group has to be synced with URL depending on controls' values like:

?group1=option1,option2,option3&group2=option2 and so on.

Airblader commented 3 years ago

So if I'd like to make a filtering with query params syncing

That is essentially exactly what ngqp was designed for – if you want to do this as the user interacts with the form, not only once they submit the form (as is the case with Amazon). For the latter, most of what ngqp does isn't necessary. However, I do understand that ngqp might still provide enough "comfort" that you'd want to be able to use it nonetheless.

I go into a bit more detail on that in my response in #165, which I seem to have forgotten about during my illness. You can see there my thought that a likely way I'd implement this in ngqp is by allowing to turn off the automatic synchronization and provide a trigger instead which you'd have to call from your onSubmit handler.

I also mention there a way this could potentially be achieved already now. I did quickly prototype what that would look like, you can find it here: https://stackblitz.com/edit/angular-ivy-hgjj6l? – I can't promise this works in all cases and scenarios, but you could give it a shot if you wanted to.

verfault commented 3 years ago

Your example is exactly that I needed. I've implemented some workarounds around FormGroup to make the filtration either, so from now I have two different ways to solve the problem.

Thank you and get well!