简体   繁体   English

使用jQuery Cookie保存复杂的页面状态

[英]Save complex page state with jQuery cookie

I have a page with a custom treeview. 我有一个带有自定义树视图的页面。 When a node is expanded, its child nodes are loaded via jQuery Ajax calls. 扩展节点时,将通过jQuery Ajax调用加载其子节点。

Now, I would like to save the current state of the treeview so that when the user leaves and returns to the page, the previously expanded treeview stay expanded. 现在,我想保存树状视图的当前状态,以便当用户离开并返回页面时,先前展开的树状视图保持展开状态。

In similar situations, I have saved data like search queries, results or form data in a cookie, using this jQuery plugin: https://github.com/carhartl/jquery-cookie 在类似的情况下,我使用此jQuery插件将搜索查询,结果或表单数据等数据保存在cookie中: https : //github.com/carhartl/jquery-cookie

So far, I can see two options: 到目前为止,我可以看到两个选择:

  1. Save the expanded nodes ID's and reload the treeview from scratch, but: 保存扩展的节点ID并从头开始重新加载树视图,但是:
    • potentially a lot of Ajax calls, 可能有很多Ajax调用,
    • difficult to make the calls in the right order (ie don't load the children before the parent is there), 很难以正确的顺序拨打电话(即,不要在父母在那里之前加载孩子),
    • waiting time for the calls to finish 等待通话结束的时间
  2. Save the entire element containing the treeview into a cookie, but: 将包含树视图的整个元素保存到cookie中,但是:
    • may be a lot of data 可能有很多数据
    • may have difficulties with attaching the right jQuery events to the node elements when inserting the treeview HTML into the DOM on load. 将树形视图HTML加载到DOM中时,在将正确的jQuery事件附加到节点元素时可能会遇到困难。

Thank you in advance for any ideas! 预先感谢您的任何想法!

Chris 克里斯

In the past I used the cookie approach. 过去,我使用cookie方法。 But the data was too big for the cookie header, so I send the state to the server with ajax and stored it there. 但是对于cookie头来说,数据太大了,所以我用ajax将状态发送到服务器并将其存储在那里。 Doing this you can restore the state while rendering the tree. 这样做可以在渲染树时恢复状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM