英文网站注册,网站开发常用的数据库,亚马逊网站域名,渭南网站建设网站建设jQuery EasyUI 树形网格#xff08;TreeGrid#xff09; - 动态加载#xff08;按需加载子节点#xff09;
jQuery EasyUI TreeGrid 支持两种常见的“动态加载”方式#xff1a; 服务器端按需加载#xff08;On-Demand Loading / Remote Lazy Loading#xff09; 最常见…jQuery EasyUI 树形网格TreeGrid - 动态加载按需加载子节点jQuery EasyUI TreeGrid 支持两种常见的“动态加载”方式服务器端按需加载On-Demand Loading / Remote Lazy Loading最常见场景首次加载根节点展开节点时向服务器请求该节点的子节点数据推荐用于大数据量。客户端懒加载Client Lazy Loading一次性从服务器获取全部层级数据但首次只显示根节点展开时在客户端逐步追加子节点适用于已知全部数据但想避免一次性渲染过多节点。下面分别提供完整示例。示例1服务器端按需动态加载推荐TreeGrid 会自动在展开节点时向服务器发送参数id父节点ID服务器根据id返回子节点数组。HTML 部分!DOCTYPEhtmlhtmlheadmetacharsetUTF-8titleTreeGrid 服务器端动态加载/titlelinkrelstylesheettypetext/csshrefhttps://www.jeasyui.com/easyui/themes/default/easyui.csslinkrelstylesheettypetext/csshrefhttps://www.jeasyui.com/easyui/themes/icon.cssscripttypetext/javascriptsrchttps://code.jquery.com/jquery-1.12.4.min.js/scriptscripttypetext/javascriptsrchttps://www.jeasyui.com/easyui/jquery.easyui.min.js/script/headbodyh2TreeGrid - 服务器端按需加载子节点/h2tableidtgclasseasyui-treegridstylewidth:700px;height:500pxdata-optionsurl:treegrid_dynamic.php, !-- 服务器脚本 -- method:get, rownumbers: true, idField:id, treeField:name, lines: truetheadtrthfieldnamewidth300名称/ththfieldsizewidth100alignright大小/ththfielddatewidth150修改日期/th/tr/thead/table/body/html服务器端示例treegrid_dynamic.php?php// 获取父节点ID首次加载根节点时 id 为 null 或不存在$parentIdisset($_GET[id])?$_GET[id]:null;// 模拟数据实际从数据库查询$dataarray();if($parentIdnull){// 根节点$data[]array(id1,nameApplications,size,date2025-01-01,stateclosed);$data[]array(id2,nameDocuments,size,date2025-02-01,stateclosed);}elseif($parentId1){// Applications 的子节点$data[]array(id11,nameEasyUI,size2MB,date2025-12-01);$data[]array(id12,namejQuery,size1MB,date2025-11-01);}elseif($parentId2){// Documents 的子节点$data[]array(id21,nameReport.pdf,size500KB,date2025-12-10);}echojson_encode($data);?关键点有子节点的父节点需设置state: closed这样才会显示展开图标。首次请求无id参数返回根节点。展开时自动带id参数请求子节点。示例2客户端懒加载一次性加载全部数据但逐步渲染适用于已获取完整层级数据但想避免一次性渲染大量节点。HTML 部分tableidtgclasseasyui-treegridtitle客户端懒加载 TreeGridstylewidth:700px;height:500pxdata-optionsurl:treegrid_full_data.json, method:get, rownumbers: true, idField:id, treeField:name, lines: true, loadFilter: myLoadFiltertheadtrthfieldnamewidth300名称/ththfieldsizewidth100alignright大小/ththfielddatewidth150修改日期/th/tr/thead/tablescripttypetext/javascriptfunctionmyLoadFilter(data,parentId){functionsetData(){vartodo[];for(vari0;idata.length;i){todo.push(data[i]);}while(todo.length){varnodetodo.shift();if(node.children){node.stateclosed;// 有子节点时关闭node.children1node.children;// 临时保存子节点node.childrenundefined;// 清空避免立即加载todotodo.concat(node.children1);}}}setData(data);varopts$(this).treegrid(options);opts.onBeforeExpandfunction(row){if(row.children1){$(this).treegrid(append,{parent:row[opts.idField],data:row.children1});row.children1undefined;$(this).treegrid(expand,row[opts.idField]);}returnrow.children1undefined;// 已加载则不再触发};returndata;}/scripttreegrid_full_data.json完整层级数据示例与基础示例相同官方参考服务器端动态加载https://www.jeasyui.com/tutorial/tree/treegrid3.php客户端懒加载https://www.jeasyui.com/tutorial/tree/treegrid5.phpDemo 页面https://www.jeasyui.com/demo/main/index.php?pluginTreeGrid 查看 Dynamic Loading 和 Lazy Loading 示例如果需要结合分页、编辑或其他功能或提供你的后端语言PHP/Java/Node 等我可以给出更具体的服务器端代码