2009-08-03 87 views
1

我有一个“李”标签的列表,我可以通过基于用户输入的RJS来隐藏和显示没有问题,但数据被保存,然后用户第二天回来,我怎样才能让页面出现元素仍然隐藏?换句话说,根据模型中的数据,我如何告诉一个元素它应该被隐藏起来?如何在页面加载到rails之前隐藏元素?

回答

1

所以你用户点击一些东西或提交的东西和李项目显示/隐藏?

然后他们离开网站,也许明天或晚些时候出现?

我会设置一个cookie,其中包含要隐藏的li项目的字符串。

我会有一个jquery函数,我可以将该字符串传递给。在我的网站的身体我也有一点像(伪代码)

if cookie exists { 

    // echo function in the body tag 

    echo 'onload="hideItems(' . $_COOKIE['hiddenItems'] . ')"; 


    // so if there's a cookie they'll wind up having code that looks like this 
    // <body onload="hideItems(12-23-34-52);"> 

} 

你会想这是要分析这些数字,然后隐藏需要隐藏的那些JavaScript函数。

function hideItems(string) { 

    var partsArray = string.split('-'); 

    // this'll put all your list items in an array 
    // e.g partsArray[0] == 12 and partsArray[1] == 23 

    // now with all the items in an array, you should be able to go through and hide any list items that match the values 

    // e.g 
    for (i=0; i< {partsArray}.length; i++) { 
     // jquery hide the list items 
     $('#listItem'+partsArray[$i]).hide(); 
    } 

} 

我会想象那么你的列表中的项目将需要在这样的格式:

<ul> 
    <li id="listItem12"></li> 
    <li id="listItem23"></li> 
</ul> 

所以我觉得这就是你如何用JavaScript做到这一点。

当然,您可以在服务器端完成它,但在JavaScript甚至加载之前,并将该cookie解析为红宝石。如果列表中的项目编号包含在cookie中,则将隐藏的类别回显到列表项目中。简单。

我不知道红宝石,所以我编辑出来的cookie代码是基于php的行话。

祝你好运。希望这可以帮助。

+0

没有做到这一点与饼干,只是实例变量。但这基本上是正确的答案。 – chrishomer 2009-09-27 17:01:43

相关问题