简单的制作后台制作写一个json(string类型)格式
public partial class goodstype_type : System.Web.UI.Page { public string datalist = "",tree=""; goodstypeManage bll = new goodstypeManage(); goodstype model = new goodstype(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { datalist = getlist(); // tree = gettree("0"); } } protected string getlist() { string s = ""; DataTable data = bll.GetAllList(); foreach (DataRow row in data.Rows) { s += "{\"id\":" + row["id"].ToString() + ",\"parentId\":" + row["parentid"].ToString() + ",\"name\": \"" + row["name"].ToString() + "\"},"; } if (s != "") { s = s.Substring(0, s.Length -1); } return s; }
在前台取来的的数据处理easyUI tree结构使用的样式
<script type="text/javascript"> function convert(rows) { function exists(rows, parentId) { for (var i = 0; i < rows.length; i++) { if (rows[i].id == parentId) return true; } return false; } var nodes = []; // get the top level nodes for (var i = 0; i < rows.length; i++) { var row = rows[i]; if (!exists(rows, row.parentId)) { nodes.push({ id: row.id, text: row.name }); } } var toDo = []; for (var i = 0; i < nodes.length; i++) { toDo.push(nodes[i]); } while (toDo.length) { var node = toDo.shift(); // the parent node // get the children nodes for (var i = 0; i < rows.length; i++) { var row = rows[i]; if (row.parentId == node.id) { var child = { id: row.id, text: row.name }; if (node.children) { node.children.push(child); } else { node.children = [child]; } toDo.push(child); } } } return nodes; } $(function () { $(‘#tt‘).tree({ data: [<%=datalist %>], loadFilter: function (rows) { return convert(rows); } }); gettypetree(); //$(‘#tree‘).combotree({ // onSelect: function (node) // { // } // }); }); </script> <div style="float: left; width: 40%"> <ul id="tt"> </ul> </div>
第二种是用递归方式 取出来的 转换json格式 tree 直接使用
链接网址:https://www.cnblogs.com/yonex/p/3379362.html
Java 这个网页也是用的递归方式 链接:https://www.cnblogs.com/20gg-com/p/6206502.html
时间: 2024-10-28 21:50:28