本树型菜单主要实现功能有:基本的树型菜单,可勾选进行多选项操作。

本树型菜单适合最初级的学者学习,涉及内容不难,下面看代码。

首先看View的代码,第一个<div>用来定义树显示的位置和id

 <div id="PersonTree"></div>
</div>
<div style=" margin-left:230px;" id="result"></div>
<button class="t-button t-state-default" id="PersonSelectSubmit" onclick="displayCheckedPersons ()">选择人员</button>

接下来看JS代码:

         $(document).ready(function () {
$("#PersonSequenceSubmit").attr('disabled', true);
});
$("#PersonTree").jstree({
json_data: {
ajax: {
url: '<%= Url.Action("GetPersonTreeDataAll", "Home")%>',
data: '{did:3}',
type: 'POST',
dataType: 'json',
contentType: 'application/json charset=utf-8'
}
}, "themes": { "theme": "default", "dots": true, "icons": true },
"plugins": ["themes", "json_data", "ui", "checkbox"]
});
}); var SelectedPersonNum = ;
var SelectedPersons = new Array(); function displayCheckedPersons() {
var checkedPersons = new Array();
var j;
j = ;
var nodes = $("#PersonTree").jstree("get_checked",null,true); //使用get_checked方法
$.each(nodes, function (i, element) {
if ($(element).attr("ifPerson") == "Y") {
checkedPersons[j] = $(element).attr("id");
j = j + ;
}
}); SelectedPersonNum = checkedPersons.length;
if (checkedPersons.length < ) {
alert('请首先选择要管理的工作人员.');
return;
}
SelectedPersons = checkedPersons;
$.ajax({
type: "POST",
url: '<%= Url.Action("DisplayCheckedPersons", "Home") %>',
data: { checkedRecords: checkedPersons },
dataType: "html",
success: function (request) {
$("#result").html(request);
},
traditional: true
});
return;
}

上半部分,是用于显示树的,下面的function是用于勾选目标项目进行操作的。这里使用的是ajax形式,不太了解的朋友可以搜搜资料了解下,还是蛮好掌握的。

接下来看后台控制器代码:

  [HttpPost]
public JsonResult GetPersonTreeDataAll(string did)//没用到地点值,显示全部人员,只是保留
{ string classnum = (HttpContext.User.Identity.Name.Split(',')[]).Substring(, );
jstreeDataContext db = new jstreeDataContext();
var d = db.ClassInfo.FirstOrDefault(c => c.classnum == classnum);
if (d != null)
{
JsTreeModel rootNode = new JsTreeModel();
rootNode.attr = new JsTreeAttribute();
rootNode.data = d.classname;
rootNode.attr.id = d.classnum;
rootNode.state = "open"; //根节点设定为初始打开状态
new JsTreeRepository().PopulateTree(classnum, rootNode);
return Json(rootNode);
}
else
{
return null;
}
}

这是用于根节点的选取,由第五行获得登录用户的身份并获取该类用户可选操作的根节点,

         public void PopulateTree(string dhid, JsTreeModel node)
{
jstreeDataContext db = new jstreeDataContext();
if (node.children == null)
{
node.children = new List<JsTreeModel>();
} var dp = db.ClassInfo.Where(c => c.classnum == dhid).FirstOrDefault();
var dpid = dp == null ? : dp.id;
var hid = dp.id;
foreach (var d in db.ClassInfo.Where(c => c.classnum.Substring(, dhid.Length) == dhid & c.classnum.Length == dhid.Length + ).ToList())
{
// create a new node
JsTreeModel t = new JsTreeModel();
t.attr = new JsTreeAttribute();
t.attr.id = d.classnum;
t.data = d.classname;
// populate the new node recursively
PopulateTree(d.classnum, t);
node.children.Add(t); // add the node to the "master" node
}
//lastly, loop through each file in the directory, and add these as nodes
foreach (var p in db.UsersInfo.Where(e => e.classID == hid).OrderBy(e => e.username).ToList())
{
// create a new node
JsTreeModel t = new JsTreeModel();
t.attr = new JsTreeAttribute();
t.attr.id = p.usernum;
t.attr.ifPerson = "Y"; //表明是人员节点
t.data = p.username;
t.state = "close";
t.children = null;
// add it to the "master"
node.children.Add(t);
}
}

这里,有2个遍历,第一个用于遍历之前传过来的根节点下的所有节点,第二个用于遍历每个节点的叶子。

这里的Model需要注意的一点事:

     public class JsTreeModel
{
public string data;
public JsTreeAttribute attr;
public string state ;
public List<JsTreeModel> children;
} public class JsTreeAttribute
{
public string id;
public string ifPerson = "N"; //初始化都标识不是人员节点
} public class JsTreeLeafModel
{
public string data;
public JsTreeAttribute attr;
}

这是树型菜单需要的一个Model。

好了,剩下的就是数据库和数据元素的Model的建立了,这里就不做多演示了,相信不会难。希望能帮到大家,有不足的望指出。

JS树型菜单的更多相关文章

  1. 下拉的DIV+CSS+JS二级树型菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. oracle使用connect by进行级联查询 树型菜单

    Oracle使用connect by进行级联查询 树型菜单(转) connect by可以用于级联查询,常用于对具有树状结构的记录查询某一节点的所有子孙节点或所有祖辈节点. 来看一个示例,现假设我们拥 ...

  3. js, 树状菜单隐藏显示

    js写的不是很严谨~~~嘿嘿   <!DOCTYPE html> <html> <head> <meta charset="UTF-8"& ...

  4. php实现无限级树型菜单(函数递归算法)

    首先到数据库取数据,放到一个数组,然后把数据转化为一个树型状的数组,最后把这个树型状的数组转为html代码.也可以将第二步和第三步合为一步. 详细如下:1.数据库设计:脚本如下:CREATE TABL ...

  5. php通用的树型类创建无限级树型菜单

    生成树型结构所需要的2维数组,var $arr = array()数组格式如下: array( 1 => array('id'=>'1','parentID'=>0,'name'=& ...

  6. JavaScript:使用递归构建树型菜单

    使用递归函数将扁平数据转为树型结构,并渲染到页面 效果图: 代码: <!DOCTYPE html> <html lang="en"> <head> ...

  7. 强大的Js树型控件Dtree使用详解

    http://www.lmwlove.com/ac/ID868 在学习文章之前,要学会看官方网站http://destroydrop.com/javascripts/tree.从官方页面你能知道:dt ...

  8. Js树型控件Dtree使用

    dtree地址:http://destroydrop.com/javascripts/tree/ Key features Unlimited number of levels 无限级 Can be ...

  9. jQuery 树型菜单插件(Treeview)

    jQuery Treeview 提供了一个无序灵活的可折叠的树形菜单.试用于一些菜单的导航,支持基于 cookie 的持久性菜单

随机推荐

  1. SpringMVC1

    itRed You are never too old to set another goal or to dream a new dream. SpringMVC一路总结(一) SpringMVC听 ...

  2. Oracle 11g password过期被锁定报道 ORA-28000 the account is locked

    一.触发这个错误的原因及相关因素 是因为oracle11g中默认在default概要文件里设置了"PASSWORD_LIFE_TIME=180天"所导致.在Oracle 11g中是 ...

  3. 电商指尖---(9).net发展Solr中间Facet特征

    上一节中我们演示了在SolrAdmin中使用Facet功能来进行分组统计.这一节我们看看如何使用.NET开发Solr中的Facet功能.在讲Facet功能的同一时候, 我们看下.Net中如何使用Sol ...

  4. 安裝 Rails 開發環境

    安裝 Rails 開發環境 Give someone a program, you frustrate them for a day; teach them how to program, you f ...

  5. SQL Server 得到SPID,唯一的sessionID

    像.net中的session一样,假设能知道了数据库中的sessionID,那全部的操作都能知道了,由于有了这个唯一的身份识别的标识. 能够做的事情有非常多,如:当前哪个用户在做什么操作,在运行什么s ...

  6. jQuery整理您的笔记----jQuery开始

    Jquery它是一种高速.简明的JavaScript相框,jQuery设计目标:Write Less,Do More(写更少的代码,做很多其他的事情). 一.Jquery框架优势: 1.轻量级 jQu ...

  7. POJ 1182(食物链-另类做法【拆点】)[Template:并查集]

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 46039   Accepted: 13400 Description ...

  8. ExcelReport源码解析

    ExcelReport第二篇:ExcelReport源码解析   导航 目   录:基于NPOI的报表引擎——ExcelReport 上一篇:使用ExcelReport导出Excel 下一篇:扩展元素 ...

  9. Android自动化测试框架新书:交流

    大家觉得编写一本描述MonkeyRunner原理分析的书籍如何?估计大概10万字左右.内容大概分布如下: Monkey实现原理: 去描述运行在目标安卓机器的monkey是如何运行并处理MonkeyRu ...

  10. ActiveReports 9实战教程(2): 准备数据源(设计时、运行时)

    原文:ActiveReports 9实战教程(2): 准备数据源(设计时.运行时) 在上讲中<ActiveReports 9实战教程(1): 手把手搭建环境Visual Studio 2013 ...