How to save a filter in Dojo

I have an EnhancedGrid in which users regularly use sophisticated filters. Is there a way to allow users to save or add bookmarks to the filter so that they can easily re-apply it in the future? I know that I can programmatically set a filter, but I cannot predict which filters my users will want to use.

Thanks!

edit: made some progress on its own ... using grid.getFilter () to return the JSON representation of the filter, and then json.stringify (jsonRepresentation) to convert it to a string. Now I am considering my options for storing, loading and converting this line. Would you load the string into a json object, and then apply it, since my filter will reveal XSS vulnerabilities to me? If I want to have filters specified as arguments in the url, can I compress the string to reduce the number of characters?

+4
source share
2 answers

From the very beginning, here are three <two> three "β†’ three approaches:

Save filter in URL (Good)

Just enter window.location and dojo/io-query::queryToObject() string with dojo/io-query::queryToObject() :

 require(['dojo/io-query'], function (ioQuery) { var uri = window.location.href; var query = uri.substring(uri.indexOf("?") + 1, uri.length); query = ioQuery.queryToObject(query); }); 

( Documentation for dojo / io-query )


Store filter in cookie (better)

The dojo/cookie module makes this very, very simple:

 require(['dojo/cookie', 'dojo/json'], function (cookie, json) { var filter = { ... }; cookie("myFilter", json.stringify(filter)); //store the cookie // json.parse(cookie("myFilter")); // ^-- returns the cookie as a JS object }); 

( Documentation for dojo / cookie )

Obviously, the user must have cookies in order for this to work, but it is much cleaner than storing a bunch of variables in the URL to bookmark them.


Use HTML5 local storage as suggested by Dimitri M : (even better)

Check if the user agent supports local storage, and if so, use this global variable to store the filter:

 require(['dojo/json'], function(json) { function supportsLocalStorage() { return ('localStorage' in window) && window['localStorage'] !== null; } var filter = { ... }; if(supportsLocalStorage()) { localStorage.setItem("myFilter", json.stringify(filter)); } // json.parse(localStorage.getItem("myFilter")) // ^-- returns the filter as a JS object }); 

The advantage of using web storage is that you can store much more data than cookies .

Perhaps you can use cookies as a reserve for browsers that do not support local storage (i.e. when supportsLocalStorage() returns false ) by adding extra overhead to your design, so this is ultimately your call, depending on which browsers you want to support.

Web Storage Browser Compatibility

+2
source

What about cookies?

The grid widget has a cookie plugin ( link ), but id does not save the filter. Perhaps a plug-in or grid extension might be an option.

Passing filter properties as strings also sounds like an option. Knowing the conditions and values ​​that can be used, or their type, you can easily check the filter before applying it to the grid.

+2
source

Source: https://habr.com/ru/post/1501389/


All Articles