【问题标题】:jquery how to keep settings after page reload using filtrify plugin?jquery如何在使用过滤插件重新加载页面后保留设置?
【发布时间】:2013-06-26 12:15:41
【问题描述】:

首先我想说我已经完成了我的功课。我知道 javascript 在页面重新加载后不应该保留它的设置。

我还发现了 3 种方法可以做到这一点:
1. 饼干
2. html本地存储
3.阿贾克斯

在尝试使用 html 本地存储来实现这一点之后,我已经放弃了几天。我找到了本教程,它允许缓存整个 html 界面 - see "Caching a whole interface"。我使用这种方法取得了一些进展,但最终我失败了。

这是我的filtrify 代码:

<script type="text/javascript">
$(function() {
    var container = $("#itemListLeading"),
        pagination = $("#pagination");

    function setLazyLoad () {
        container.find("img").lazyload({
            event : "turnPage",
            effect : "fadeIn"
        });
    };

    function setPagination () {
        pagination.jPages({
            containerID : "itemListLeading",
            perPage : 9,
            direction : "auto",
            animation : "fadeInUp",
            previous  : "a.jprev",
            next      : "a.jnext",
            callback : function( pages, items ){
                items.showing.find("img").trigger("turnPage");
                items.oncoming.find("img").trigger("turnPage");
            }
        });
    };

    function destroyPagination () {
        pagination.jPages("destroy");
    };



    setLazyLoad();
    setPagination();

    var ft = $.filtrify("itemListLeading", "placeHolder", {
        close: true, // Close windows after tag select
        block : "data-original",
        callback: function ( query, match, mismatch ) {

            if ( mismatch.length ) $("div#reset").show(); // Show Reset
            else $("div#reset").hide();

            $('.ft-label').parent() // Hide unrelated tags
                .find('li[data-count=0]').hide().end()
                .find(':not(li[data-count=0])').show().end();

            $(".ft-selected li").css("display","inline-block"); // small tag display fix

            destroyPagination();
            setPagination();

        }
    });

    $("div#reset span").click(function() { // Make reset button clickable
        ft.reset();
    }); 
}); 
</script>

这是我对本地存储实现的看法:

 $(function() {
  var edit = document.getElementById('filter-menu');
  $(edit).blur(function() {
        localStorage.setItem('todoData', this.innerHTML);
        });

  // when the page loads
  if ( localStorage.getItem('todoData') ) {
        edit.innerHTML = localStorage.getItem('todoData');
        }

  $("#reset span").click(function() { // Reset cache button
      localStorage.clear();
      location.reload();
  });   
});

我遇到的问题:
1.缓存并不总是有效(我使用 .click 函数得到了更好的结果)

2.当缓存确实起作用时,它会缓存过滤器设置的状态(按下按钮),但会显示所有过滤器对象(而不仅仅是那些与过滤器设置相关的对象)

3.我无法使用重置按钮(代码的最后一段)重置缓存(重新加载有效),尽管当我在 firebug 控制台缓存中复制 localStorage.clear(); location.reload(); 时被重置。

【问题讨论】:

  • +1 显示一些努力和格式

标签: jquery caching jquery-plugins browser-cache


【解决方案1】:

在您的过滤回调函数中,您需要将query 保存为字符串,例如本地存储

localStorage.setItem('ftquery', JSON.stringify(query))

然后在实例化 filtrify 后,您需要“触发”查询

ft.trigger(JSON.parse(localStorage.getItem('ftquery')));

因此,要使用您的代码示例,请像这样编辑 filtrify 部分:

var ft = $.filtrify("itemListLeading", "placeHolder", {
    close: true, // Close windows after tag select
    block : "data-original",
    callback: function ( query, match, mismatch ) {

        // save query to localStorage
        localStorage.setItem('ftquery', JSON.stringify(query));

        ...

    }
});

// retrieve query from localStorage
if (localStorage.getItem('ftquery')) {
    ft.trigger(JSON.parse(localStorage.getItem('ftquery')));
}

【讨论】:

    【解决方案2】:

    所以,如果您对 javascript 解决方案一无所知,并且您实际上正在使用 jQuery,我会考虑查看 jQuery Cookie 插件 (https://github.com/carhartl/jquery-cookie)。它非常适合这种情况,可能会激发您进一步研究 cookie,比如 php。

    如果您决定采用这条路线,那么实施将是直截了当的。你会以与 localStorage 类似的方式处理它:

    $(function() {
         var edit = document.getElementById('filter-menu').innerHTML;   
         $.cookie('todoData', edit, { expires: 7 });
    });
    

    我已经使用它来实现类似的功能集,并且我相信它在大多数现代浏览器中都能很好地处理。我还建议更具体地抓取每个标签,也许循环遍历每个列表项 (LI),并用逗号分隔。

    【讨论】:

    • 谢谢,但不幸的是我仍然无法在我的网站$(document).ready(function() { var edit = document.getElementById('filter-menu').innerHTML; $('#filter-menu').blur(function() { $.cookie('todoData', edit, { expires: 7 }); }); }); 上完成这项工作,我添加了$(document).ready(function() {,因为菜单是由 jquery filtrify 插件生成的。我添加了blur(function() {,以便当过滤器的ul 丢失它的forucs 时开始缓存。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    相关资源
    最近更新 更多