Search code examples
dojodgridjsonreststore

dgrid / dstore / Rest - How to add additional query parameters to request


I used an older version of dgrid (onDemandGrid) toghether with a dojo JsonRestStore.

My web page has some input fields (e.g. date, name etc). After changing one of these fields I change the query of the grid:

grid.setQuery({y: year, m: month, l: name1, d: flag, e: name2});

and the Json request was somthing like ?y=2015&m=5&l=test1&....

I learned, that with the actual version of dgrid this isn't longer possible.

Is something simelar possible with dgrid/dstore.? Filtering seems not to be an option, as far as I understood this is only on client side and I'm not able to load all my data. What I need are some dynamic parameters to filter the data on the server.


Solution

  • dstore's equivalent to dojo/store/JsonRest is dstore/Rest (however, if you don't have a compliant REST API on the server you may want to use dstore/Request).

    dstore's filter method allows you to make arbitrary queries. It returns a collection with any applied filters stored so that they can be included whenever fetch or fetchRange is called.

    var store = new Request({ target: '/path/to/service' });
    var filteredCollection = store.filter({ y: 2015, m: 5 });
    filteredCollection.fetch();
    

    would result in the following HTTP request:

    /path/to/service?y=2015&m=5
    

    To use this functionality with dgrid 0.4, you would assign the filtered collection to the grid instance (and dgrid will handle calling fetch/fetchRange as necessary):

    grid.set('collection', store.filter({y: year, m: month}));
    

    dgrid 0.4 and dstore introduce a significant shift in the way the grid interacts with the store. In dgrid 0.3 the grid had a much more active role in managing the query state of the store - with dgrid 0.4, this is no longer the case (hence the removal of the setQuery method). In dgrid 0.4, it is up to code external to the grid to implement logic related to filtering store data. Whereas in dgrid 0.3 you typically set the grid's store property a single time and call setQuery whenever you need to filter the data, with dgrid 0.4 you set the collection property any time you need to update the filtering. dstore's Request store allows you to configure the parameter names for range and sort filtering.