Remember (persist) the filter, sort order and current page of jqGrid

前端 未结 5 1109
面向向阳花
面向向阳花 2020-12-04 15:57

My application users asked if it were possible for pages that contain a jqGrid to remember the filter, sort order and current page of the grid (because when they click a gri

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 16:31

    Hi You can make this easier (no other dependency needed) in Pure Js not a jQuery plugin

    var prefs = {
    
        data: {},
    
        load: function () {
            var the_cookie = document.cookie.split(';');
            if (the_cookie[0]) {
                this.data = JSON.parse(unescape(the_cookie[0]));
            }
            return this.data;
        },
    
        save: function (expires, path) {
            var d = expires || new Date(2020, 02, 02);
            var p = path || '/';
            document.cookie = escape( JSON.stringify(this.data) )
                              + ';path=' + p
                              + ';expires=' + d.toUTCString();
        }
    
    }
    

    How to use ?

    to_save = { "size": 40, "color": "green", "products":"jeans"};//and any other data/filters you wanna store here
    
    prefs.data = to_save;
    prefs.save();//now our json object is saved on the client document cookie
    
    
    // delete
    var date_in_the_past = new Date(2000,02,02);
    prefs.save(date_in_the_past);
    
    
    // read
    var what = prefs.load();
    // load populates prefs.data and also returns
    alert(what.color);
    // or ...
    alert(prefs.data.color);
    

    PS : All modern browsers support native JSON encoding/decoding (Internet Explorer 8+, Firefox 3.1+, Safari 4+, and Chrome 3+). Basically, JSON.parse(str) read more. PSS : the Object i used there is just optimization and removing the dependency of .toJSONstring ... source

    Take a look at these jQuery plugins

    USE this don't use the custom funcation above https://github.com/carhartl/jquery-cookie

    https://github.com/ScottHamper/Cookies

提交回复
热议问题