Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change of query_parameters is not recognized when done in onchange-function #179

Open
knallcharge opened this issue Aug 20, 2021 · 1 comment

Comments

@knallcharge
Copy link
Contributor

I see a strange behaviour when trying to set the query_parameters of a Select2-form field. I have set query_parameters to ['var' => ''] in my form and it shows in the source-code of my page alright.
I now need to fill "var" with a value from another form-element dynamically, but the change is ignored and the value not sent to my remote_route. This strangely only happens when the change of the query_parameters are done in the onchange-event of the other field.

What works:

$('#my_element').data('query-parameters', {'var': 'value'});

Now I receive "value" in "var" in my remote_route and can process it.

What does NOT work:

$('#another_element').change(function () {
    $('#my_element').data('query-parameters', {'var': 'value'});
}

"var" is just empty in my remote_route in this case.

I have checked the contents of "var" using console.log($('#my_element').data('query-parameters')); before and after setting it and the change is done in both cases (!), only when setting it in the onchange-event, it is not transmitted to the remote_route.

@knallcharge
Copy link
Contributor Author

knallcharge commented Aug 25, 2021

I did some more research on this and found something: In select2entity.js the following line (146) seems to initialize the fields:
$('.select2entity[data-autostart="true"]').select2entity();
but that happens only once on document.ready, so my guess (I'm not good at JavaScript) is that once the page is loaded, the select2-fields are initialized, the query-parameters are taken from data-query-parameters, but that's it, the select2-fields won't get notified of any change in data-query-parameters, so the new value is never transmitted to the controller.

Just to test this, I changed my code and did this:

$('#another_element').change(function () {
    $('#my_element').data('query-parameters', {'var': 'value'});
    $('.select2entity[data-autostart="true"]').select2entity();
}

This wil basically re-initialize the select2-fields after "another_element" changes and have the select2-fields re-read the (now changed) value from "my_element".

I have no idea (once again: JavaScript ...) if this will break or overwrite anything in the select2-fields, but this actually resolves the problem and sends the (changed) values to the controller.

Don't know if this is a valid fix, so I'd appreciate if someone could verify this.
(I guess it would probably better, if the bundle read the current data-query-parameters before sending the request to the controller, making the re-initialization obsolete, but I have no idea how to do this.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant