1

解决了

我有一个问题。

是否可以在“展开”和“折叠”上使用带有 jQ​​uery Cookie 的嵌套 jQuery 插件?记住“展开”和“折叠”的位置。

链接到脚本:可嵌套的 jQuery 插件

感谢您的回答!

这是带有 cookie 保存的脚本:

expandItem: function(li)
    {
        li.removeClass(this.options.collapsedClass);
        li.children('[data-action="expand"]').hide();
        li.children('[data-action="collapse"]').show();
        li.children(this.options.listNodeName).show();
        if(getCookie("tree") != null){
            var CookieValue = getCookie("tree") + li.attr('data-id') + ",";
        }
        else{
            var CookieValue = li.attr('data-id') + ",";
        }
        $.cookie("tree", CookieValue, {expires:3600, path:'/administrator/'});
    },

    collapseItem: function(li, a)
    {
        var lists = li.children(this.options.listNodeName);
        if (lists.length) {                
            li.addClass(this.options.collapsedClass);
            li.children('[data-action="collapse"]').hide();
            li.children('[data-action="expand"]').show();
            li.children(this.options.listNodeName).hide();
            if(getCookie("tree") != null){
                var CookieValueArray = getCookie("tree").split(",");
                for(i=0;i<CookieValueArray.length;i++){
                    if(CookieValueArray[i] == li.attr('data-id') && a == true){
                        li.children('[data-action="expand"]').hide();
                        li.children('[data-action="collapse"]').show();
                        li.children(this.options.listNodeName).show();
                    }
                }
                if(a != true){
                    var CookieValue = "";
                    for(i=0;i<CookieValueArray.length-1;i++){
                        if(CookieValueArray[i] != li.attr('data-id')){
                            CookieValue += CookieValueArray[i] + ",";
                        }
                    }
                    if(CookieValue == ""){
                        $.cookie("tree", "", {expires:-1, path:'/administrator/'});
                    }
                    else{
                        $.cookie("tree", CookieValue, {expires:3600, path:'/administrator/'});
                    }
                }
            }
        }
    },
4

1 回答 1

1

是的。

我认为您必须通过添加折叠状态来更改序列化功能。

serialize: function()
    {
        var data,
            depth = 0,
            list  = this;
            step  = function(level, depth)
            {
                var array = [ ],
                    items = level.children(list.options.itemNodeName);
                items.each(function()
                {
                    var li   = $(this),
          // Check if dd-collapse is part of the class of the <li> element
                        var collapsed = "open";
                        if(li.hasClass( "dd-collapsed" ){
                          collapsed = "collapsed";
                        }
                        item = $.extend({}, li.data(), collapsed),
                        sub  = li.children(list.options.listNodeName);
                    if (sub.length) {
                        item.children = step(sub, depth + 1);
                    }
                    array.push(item);
                });
                return array;
            };
        data = step(list.el.find(list.options.listNodeName).first(), depth);
        return data;
    }

然后,在展开/折叠事件上写入 cookie(cookie 内容将是序列化字符串):

expandItem: function(li)
    {
        li.removeClass(this.options.collapsedClass);
        li.children('[data-action="expand"]').hide();
        li.children('[data-action="collapse"]').show();
        li.children(this.options.listNodeName).show();
        $.cookie("your_cookie", this.serialize());
    },

    collapseItem: function(li)
    {
        var lists = li.children(this.options.listNodeName);
        if (lists.length) {
            li.addClass(this.options.collapsedClass);
            li.children('[data-action="collapse"]').hide();
            li.children('[data-action="expand"]').show();
            li.children(this.options.listNodeName).hide();
        }
        $.cookie("your_cookie", this.serialize());
    },

    expandAll: function()
    {
        var list = this;
        list.el.find(list.options.itemNodeName).each(function() {
            list.expandItem($(this));
        });
        $.cookie("your_cookie", this.serialize());
    },

    collapseAll: function()
    {
        var list = this;
        list.el.find(list.options.itemNodeName).each(function() {
            list.collapseItem($(this));
        });
        $.cookie("your_cookie", this.serialize());
    }

当然,您必须在可嵌套的之前加载 jquery.cookie.js

我没有尝试过,但它在我的脑海中起作用......;)

于 2013-10-20T11:23:29.260 回答