如何在Dojo中保存过滤器 [英] how to save a filter in Dojo

查看:170
本文介绍了如何在Dojo中保存过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EnhancedGrid,用户经常使用复杂的过滤器。有没有办法允许用户保存或加入过滤器,以便日后轻松重新应用?我知道我可以通过编程方式设置一个过滤器,但我无法预测我的用户将需要什么过滤器。



谢谢!



编辑:自己做了一些进展...使用grid.getFilter()返回过滤器的JSON表示形式,然后使用json.stringify(jsonRepresentation)将其转换为字符串。现在我正在考虑我如何存储,加载和转换该字符串的选项。将一个字符串加载到一个json对象,然后应用它作为我的过滤器打开我的XSS漏洞?如果我想要将过滤器指定为url中的参数,可以压缩字符串以减少字符数吗?

解决方案

关闭蝙蝠,这里是我看到的两种三种方法:



将过滤器存储在URL (好的)



只需获取 window.location ,并使用 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);
});

dojo / io-query的文档






存储cookie中的过滤器(Better)



dojo / cookie 模块使这非常非常容易:

  require(['dojo / cookie','dojo / json'],function(cookie,json){
var filter = {...};
cookie(myFilter,json.stringify(filter)); //存储cookie
// json.parse(cookie(myFilter ));
// ^ - 将cookie作为JS对象返回
});

dojo / cookie的文档



显然,用户必须启用Cookie才能工作,但比存储更干净一些变量在他们的书签中。






使用HTML5本地存储为由 Dimitri M 建议:(更好)



检查用户的代理是否支持本地存储,如果是,请使用该全局变量来保持过滤器:

  require(['dojo / json'],function(json){
function supportsLocalStorage(){
return('localStorage'in window)&& window ['localStorage']! ;
}

var filter = {...};

if(supportsLocalStorage()){
localStorage.setItem(myFilter json.stringify (filter));
}

// json.parse(localStorage.getItem(myFilter))
// ^ - 返回过滤器作为JS对象
});

使用网络存储的一个优点是可以存储更多的数据 cookies可以



你可以想象使用cookies作为对于不支持本地存储的浏览器的回退(即当 supportsLocalStorage()返回 false 时)为您的设计添加触摸更多的开销,最终是您的电话,具体取决于您要支持的浏览器。



Web存储的浏览器兼容性


I have an EnhancedGrid that users regularly use complex filters on. Is there a way to allow users to save or bookmark a filter so they can easily re-apply it in future? I know I can programmatically set a filter, but I can't predict what filters my users will want.

Thank you!

edit: made some progress myself... using grid.getFilter() to return a JSON representation of the filter, then json.stringify(jsonRepresentation) to convert it into a string. Now I'm considering my options for how to store, load and convert that string. Would loading a string into a json object then applying it as my filter open me up to XSS vulnerabilities? If I want to have filters specified as arguments in the url, can I compress the string to reduce the character count?

解决方案

Right off the bat, here are the two three approaches I see:

Store the filter in a URL (Okay)

Simply get the window.location and parse out the query string using 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 the filter in a cookie (Better)

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

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 enabled for this to work, but it's much cleaner than storing a bunch of variables in a URL for them to bookmark.


Use HTML5 Local Storage as suggested by Dimitri M: (Even better)

Check to see if the user's agent supports Local Storage, and if so, use that global variable to hold on to 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
});

An advantage in using web storage is that you can store much more data than cookies can.

You could conceivably use cookies as a fallback for browsers that don't support Local Storage, (i.e. when supportsLocalStorage() returns false) at the cost of adding a touch more overhead to your design, so ultimately it's your call, depending on what browsers you want to support.

Browser Compatibility for Web Storage

这篇关于如何在Dojo中保存过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆