FancyTree 状态保持
FancyTree非常优秀的树控件
examples:http://wwwendt.de/tech/fancytree/
如何将树节点的选中状态保存下来,无论刷新页面还是,ajax重复请求,都保持在上一次状态呢?
FancyTree提供状态持久化保存扩展,方案如下:
https://github.com/mar10/fancytree/wiki/ExtPersist
$("#tree").fancytree({ extensions: ["persist"], checkbox: true, persist: { // Available options with their default: cookieDelimiter: "~", // character used to join key strings cookiePrefix: undefined, // 'fancytree-<treeId>-' by default cookie: { // settings passed to jquery.cookie plugin raw: false, expires: "", path: "", domain: "", secure: false }, expandLazy: false, // true: recursively expand and load lazy nodes overrideSource: true, // true: cookie takes precedence over `source` data attributes. store: "auto", // 'cookie': use cookie, 'local': use localStore, 'session': use sessionStore types: "active expanded focus selected" // which status types to store }, [...] });
Options
-
cookieDelimiter, type: {string}, default: '~'
Character used to join key strings. -
cookiePrefix, type: {string}, default: 'fancytree-<treeId>-'
Used to generate storage keys. -
cookie, type: {object}, default: use a session cookie
Options passed to$.cookie
plugin (only if cookies are used; see also 'store' option). -
expandLazy, type: {boolean}, default: false
true: recursively expand and load lazy nodes. -
fireActivate, type: {boolean}, default: true
false: suppressactivate
event after active node was restored. -
overrideSource, type: {boolean}, default: true
true: persisted information will be used, even if source data is set totrue
orfalse
.
false: persisted information will only be used if source data isundefined
. -
store, type: {string}, default: 'auto'
Storage type 'local': localStorage, 'session': sessionStorage, 'cookie': use js-cookie (or jquery-cookie ) plugin.
'auto': use sessionStorage if available, fallback to cookie.
Use 'local' (or 'cookie' with expiration settings) to store status over sessions. -
types, type: {string}, default: 'active expanded focus selected'
Which status types to store, separated by space.
Events
- restore
Fired after tree status is restored.
Methods
{void} tree.clearCookies(types)
Reset persistence data.
{string} [types='active expanded focus selected']
这里注意:cookiePrefix,如果不设置该值的话,每次ajax请求的tree都是不一样(非刷新页面),无法达到保持状态的效果,设置该值后,无论刷新页面还是ajax请求,都可以保持状态了。
参考文献:http://stackoverflow.com/questions/33147617/fancytree-only-loads-persist-state-after-page-refresh
cookie: { expires:365 }, 可以设置cookie的有效期,当然还有其他若干存储方式可选择
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架