truckingsim / Ajax-Bootstrap-Select

This uses the bootstrap-select plugin and extends it so that you can use a remote source to search.
MIT License
280 stars 97 forks source link

ajax.data value is empty when selecting a value from another ajax select #175

Closed ppazos closed 4 years ago

ppazos commented 5 years ago

I have two ajax select, when I do the search on the first one, $('#organization_id'), I can get the selected value in the console correctly by $('#organization_id').val().

Then on the second, I need to include the value selected from the first select in the ajax params, so I have:

   ajax: {
      ...
            data: {
              organization_id: $('#organization_id').val(),
              q: "{{{q}}}"
            }
      ...

In the request for the second ajax search, the organization_id value is empty. Not sure why the select picker doesn't read the value, or if it's read, why it is not included in the request.

markhalliwell commented 4 years ago

That's because you're assigning data upon initialization of ajax-bootstrap-select, which at the time is of course empty.

You can use a function instead to defer building/returning the data right before the request is actually invoked: https://github.com/truckingsim/Ajax-Bootstrap-Select/blob/e33d67f0fc08b2a259f9ec1772bab1db286e122b/src/js/classes/AjaxBootstrapSelectRequest.js#L44-L49

   ajax: {
      //...
            data: function () {
                return {
                    organization_id: $('#organization_id').val(),
                    q: "{{{q}}}"
                };
            }
      //...
    },