Jquery easyui Tree的简单使用

时间:2022-07-25 20:34:18

Jquery easyui Tree的简单使用

Jquery easyui 是jQuery EasyUI是一组基于jQuery的UI插件集合,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面。开发者不需要编写复杂的javascript,也不需要对css样式有深入的了解,开发者需要了解的只有一些简单的html标签。

Jquery easyui 官网:http://jeasyui.com/ ,中文网站:http://www.jeasyui.net/,jquery easyui 下载地址:http://jeasyui.com/download/index.php

在项目中有时需要页面设计,不巧美工前端人员比较忙或者其他原因,造成敲代码的程序猿不得不进行ui设计,此时可以尝试easyui。

进入正题,本文分两部分介绍easyui中tree的使用:

  首先我们需要引用两个文件一个是 主题样式css文件,一个是easyui核心js文件(easyui依赖jquery,如果没有引用,需要添加引用)

Jquery easyui Tree的简单使用

  在想要生成tree的ul加上class "easyui-tree"

1.静态数据Tree,结构确定,数据是确定的,数据直接在html写死的

2.动态数据Tree,结构不确定,动态数据,数据需要从服务器端获取

  1. 静态数据Tree

    静态数据tree代码示例:

    <ul class="easyui-tree" id="nav_ul">
    <li><a href="default.aspx">信息管理</a> </li>
    <li><a href='columnManage.aspx'>栏目管理</a></li>
    <li><a href="ContentManage.aspx">内容管理</a></li>
    <li><a href="RecycleContent.aspx">内容回收站</a></li>
    <li><span>资源管理</span>
    <ul>
    <li><a href="ResourceManage-0.aspx">CSS管理</a></li>
    <li><a href="ResourceManage-1.aspx">JS管理</a></li>
    </ul>
    <li><span>模板管理</span>
    <ul>
    <li><a href="ResourceManage-2.aspx">内容页模板管理</a></li>
    <li><a href="ResourceManage-3.aspx">栏目页模板管理</a></li>
    </ul>
    </li>
    </li>
    </ul>

    在浏览器中的效果:Jquery easyui Tree的简单使用,可以根据自己想要实现的样式,进行样式的调整,建议加页面内联样式或行内样式,不要直接修改easyui的css文件

  2. 动态数据Tree

    动态数据tree前台html代码示例:

    <ul id="tt" class="easyui-tree" data-options="url:'/Handlers/getTypesNodeHandler.ashx'"></ul>

    url代表的是从服务器端获取tree的数据的处理程序路径

  经过使用 Fiddle调试可以发现每次请求时,请求参数为“id”,值为选择节点的id

服务器端处理程序getTypesNodeHandler.ashx示例代码:  Jquery easyui Tree的简单使用  

移除tree当前选择项,当选中tree的某个 节点时,对应节点会多一个class为“tree-node-selected ”的样式,将这个样式去掉就可以移除选择的tree的选项

$(".tree-node-selected").removeClass("tree-node-selected");
 using System;

 namespace Models.FormatModel
{
public class TreeModel
{
//节点id
public int id { get; set; } //节点显示的文本
public string text { get; set; } //open 、closed
public string state { get { return "closed"; } }
}
}

TreeModel

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace WebApplication1.Handlers
{
/// <summary>
/// Summary description for getTypesNodeHandler
/// </summary>
public class getTypesNodeHandler : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int parentId = ;
int.TryParse(context.Request["id"], out parentId);
List<Models.Category> types = null;
try
{
//判断父节点的值
if (parentId > )
{
//加载子级菜单
types = CommonNews.Helper.OperateContext.Current.LoadSecondaryCategory(parentId);
}
else
{
//加载*菜单
types = CommonNews.Helper.OperateContext.Current.LoadTopCategory();
}
//判断是否有值,有值的话先转换为tree模型再转换为json输出,没有值直接输出空字符串
if (types != null)
{
//转换为tree模型
List<Models.FormatModel.TreeModel> tree = types.Select(t => new Models.FormatModel.TreeModel() { id = t.CategoryId, text = t.CategoryName }).ToList();
//转换为json格式数据输出
context.Response.Write(Common.ConverterHelper.ObjectToJson(tree));
}
else
{
context.Response.Write("");
}
}
catch (Exception ex)
{
new Common.LogHelper(typeof(getTypesNodeHandler)).Error(ex);
context.Response.Write("error");
}
} public bool IsReusable
{
get
{
return true;
}
}
}
}

getTypesNodeHandler