jQuery UI Widgets Forums Plugins Data Adapter How to send multiple values for the same query parameter

This topic contains 2 replies, has 2 voices, and was last updated by  David 8 years, 1 month ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author

  • David
    Participant

    I am trying to figure out how to get the jqxDataAdapter “data” object to submit multiple values for the same parameter to a GET call, the same way that a <select multiple…> HTML element would. For example, I need to make a call similar to http://server/api-call?foo=bar&itemId=1&itemId=2&itemId=3.

    Sample code:

        var dataAdapter = new $.jqx.dataAdapter(source, {
            formatData: function (data) {
                data.foo = "bar";
                data.itemId = 1;
                return data;
            },
            ...
        });

    This works fine if you only need one value, but setting data.itemId = [1, 2, 3] doesn’t work. Setting data.itemId = “1&itemId=2&itemId=3” doesn’t work either because the framework escapes the special characters.

    Is there any way to do what I’m attempting with the data object? And if not, what would be the preferred workaround? Thanks.


    ivailo
    Participant

    Hi David,

    You also can add your optional parameters in your source url string.

    var source =
    {
    datatype: “json”,
    datafields: [
    …..
    ],
    url: “http://server/api-call?foo=bar&itemId=1&itemId=2&itemId=3”
    };

    Best Regards,
    Ivailo Ivanov

    jQWidgets Team
    http://www.jqwidgets.com


    David
    Participant

    Thanks for the reply. I wrote my initial example the way I did to simplify the point. What I’m actually doing is more complicated and dynamic, for example:

    var dataAdapter = new $.jqx.dataAdapter(source, {
            formatData: function (data) {
                data.itemId = getItemIds();
                data.otherParam = getOtherParams();
                ...
                return data;
            },
            ...
        });

    And I often have multiple query parameters that take multiple values. The helper functions getItemIds(), getOtherParams(), etc. will query my UI components and return the appropriate values on-the-fly whenever the data adapter (and grid) get refreshed.

    Assuming this isn’t going to work in some cases, I’ve switched some of my code to set/change the url field of the source object repeatedly, and it seems to work that way. Still, it would be nice if the formatData solution could be made to support multiple values, e.g., by assigning an array to one of data’s members. I find that to be a bit more elegant than reconstructing the url every time. Just a suggestion.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.