1:添加引用

   <%--流程类别多选--引用js和css文件--开始--%>
<link rel="stylesheet" href="../css/zTreeStyle/demo.css?version=<%=VersionNo %>>" type="text/css">
<link rel="stylesheet" href="../css/zTreeStyle/zTreeStyle.css" type="text/css"> <%--流程类别多选--引用js和css文件--结束--%>

1 添加引用

2:js脚本

    <%--流程类别多选--引用js脚本--开始--%>
<script type="text/javascript">
<!--
var setting = {
check: {
enable: true, chkboxType: { "Y": "s", "N": "ps" }
},
view: {
dblClickExpand: false
},
data: {
simpleData: {
enable: true
}
},
callback: {
beforeClick: beforeClick,
onCheck: onCheck
}
}; var zNodes;
var selectIds = "";
function beforeClick(treeId, treeNode) {
var zTree = $.fn.zTree.getZTreeObj("treeProcessCategory");
zTree.checkNode(treeNode, !treeNode.checked, null, true);
return false;
} function onCheck(e, treeId, treeNode) {
var zTree = $.fn.zTree.getZTreeObj("treeProcessCategory"),
nodes = zTree.getCheckedNodes(true),
v = "";
selectIds = "";
for (var i = , l = nodes.length; i < l; i++) {
v += nodes[i].name + ",";
selectIds += nodes[i].id + ",";
}
if (v.length > ) v = v.substring(, v.length - );
if (selectIds.length > ) {
selectIds = selectIds.substring(, selectIds.length - );
}
var cityObj = $("#dropParProcessCategory");
cityObj.attr("value", v);
//给隐藏域赋值
setHidDropParProcessCategoryIds(v, selectIds);
selProcessCatergoryChange();
//清空二级树
clearSubProcess();
}
function showMenu() {
var cityObj = $("#dropParProcessCategory");
var cityOffset = $("#dropParProcessCategory").offset();
$("#menuProcessCategory").css({ left: cityOffset.left + "px", top: cityOffset.top + cityObj.outerHeight() + "px" }).slideDown("fast"); $("body").bind("mousedown", onBodyDown);
checkedNodesByProcessCategoryIds();
}
function hideMenu() {
$("#menuProcessCategory").fadeOut("fast");
$("body").unbind("mousedown", onBodyDown);
}
function onBodyDown(event) {
if (!(event.target.id == "menuBtn" || event.target.id == "dropParProcessCategory" || event.target.id == "menuProcessCategory" || $(event.target).parents("#menuProcessCategory").length > )) {
hideMenu();
}
} $(document).ready(function () {
zNodes = eval("(" + $("#<%=hidDropParProcessCategoryDataSource.ClientID %>").val() + ")");
$.fn.zTree.init($("#treeProcessCategory"), setting, zNodes);
$.fn.zTree.init($("#treeDemo"), setting2, zNodes2);
});
function setHidDropParProcessCategoryIds(v, selectIds) {
$("#<%=hidDropParProcessCategoryNames.ClientID %>").val(v, selectIds);
$("#<%=hidDropParProcessCategoryIds.ClientID %>").val(selectIds);
}
</script> <script type="text/javascript"> var setting2 = {
check: {
enable: true,
chkboxType: { "Y": "", "N": "" }
},
view: {
dblClickExpand: false
},
data: {
simpleData: {
enable: true
}
},
callback: {
beforeClick: beforeClick2,
onCheck: onCheck2
}
}; var zNodes2 = [];
var selectIds2 = "";
function beforeClick2(treeId, treeNode) {
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
zTree.checkNode(treeNode, !treeNode.checked, null, true);
return false;
} function onCheck2(e, treeId, treeNode) {
var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
nodes = zTree.getCheckedNodes(true),
v = "";
selectIds2 = "";
for (var i = , l = nodes.length; i < l; i++) {
v += nodes[i].name + ",";
selectIds2 += nodes[i].id + ",";
}
if (v.length > ) { v = v.substring(, v.length - ); }
if (selectIds2.length > ) {
selectIds2 = selectIds2.substring(, selectIds2.length - );
}
var cityObj = $("#dropSubProcess");
cityObj.attr("value", v);
//给隐藏域赋值
SetValueToHidd(v, selectIds2)
}
function showMenu2() {
var cityObj = $("#dropSubProcess");
var cityOffset = $("#dropSubProcess").offset();
$("#menuContent").css({ left: cityOffset.left + "px", top: cityOffset.top + cityObj.outerHeight() + "px" }).slideDown("fast"); $("body").bind("mousedown", onBodyDown2);
checkedNodesBySubProcessIds();
}
function hideMenu2() {
$("#menuContent").fadeOut("fast");
$("body").unbind("mousedown", onBodyDown2);
}
function onBodyDown2(event) {
if (!(event.target.id == "menuBtn" || event.target.id == "dropSubProcess" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length > )) {
hideMenu2();
}
}
//清空二级树
function clearSubProcess() {
$("#<%=hidSubProcessNames.ClientID %>").val("");
$("#<%=hidSubProcess.ClientID %>").val("");
$("#<%=dropSubProcess.ClientID %>").val("");
}
</script>
<%--流程类别多选--引用js脚本--结束--%>

设置两个js脚本

3:页面刷新后状态保持

     <%--流程类别多选--查询后再次刷新页面,选中已选中的值--结束--%>
<script type="text/javascript">
function checkedNodesByProcessCategoryIds() {
var zTree = $.fn.zTree.getZTreeObj("treeProcessCategory");
if (zTree == null) {
return;
}
var processCategoryIds = $("#<%=hidDropParProcessCategoryIds.ClientID %>").val()
var ids = processCategoryIds.split(',');
for (var i = ; i < ids.length; i++) {
var node = zTree.getNodeByParam("id", ids[i], null);
if (node != null) {
node.checked = true;
zTree.updateNode(node);
}
}
}; function checkedNodesBySubProcessIds() {
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
if (zTree==null) {
return;
}
var subProcessIds = $("#<%=hidSubProcess.ClientID %>").val()
var ids = subProcessIds.split(',');
for (var i = ; i < ids.length; i++) {
var node = zTree.getNodeByParam("id", ids[i], null);
if (node != null) {
node.checked = true;
zTree.updateNode(node);
}
}
}; </script> <%--流程类别多选--查询后再次刷新页面,选中已选中的值--结束--%>

查询后再次刷新页面,选中已选中的值

4:二级联动

     function selProcessCatergoryChange() {
$.ajax({
type: "GET",
url: "/Services/RetrieveMDSService.aspx",
dataType: "json",
cache: true,
data: { RetrieveType: "SetParProcessCategorySelectChangeZTree", strId: $("#<%=hidDropParProcessCategoryIds.ClientID %>").val() },
success: function (returnValue) {
var vhtml = new Array(); var jsonList = returnValue; for (var i = ; i < jsonList.length; i++) {
//{id:3, pId:0, name:"上海"},
if (jsonList[i].ProcessID != "undefined" && jsonList[i].ProcessID != "") {
var eJson = '{id: ' + jsonList[i].ProcessID + ', pId:0, name:\"' + jsonList[i].ProcessName + '\"}'; vhtml.push(eval("(" + eJson + ")"));
}
}
zNodes2 = vhtml;
$.fn.zTree.init($("#treeDemo"), setting2, zNodes2);
},
error: function (data, textStatus, errorThrown) {
}
});
} function SetValueToHidd(v, subProcessIDs) {
document.getElementById("<%= hidSubProcessNames.ClientID%>").value = v;
document.getElementById("<%= hidSubProcess.ClientID%>").value = subProcessIDs;
}

构建2级联动

    private void SetParProcessCategorySelectChangeZTree()
{
//20140610 Eva Add:设置多语言
string lang = this.GetCurrentLanguage();
StringBuilder builder = new StringBuilder();
builder.Append("[");
builder.Append("{");
if (lang == "en-us")
{
builder.AppendFormat("\"ProcessName\":\"{0}\",", "All");
}
else
{
builder.AppendFormat("\"ProcessName\":\"{0}\",", "全部");
} builder.AppendFormat("\"ProcessID\":\"{0}\"", "");
builder.Append("},");
if ((base.Request.QueryString["strId"] != null) && (base.Request.QueryString["strId"].Trim() != ""))
{
string strIds = base.Request.QueryString["strId"].Trim();
string whereStr = String.Format(@" select a.sort,b.OrderNo,a.sort,a.* from ProcessPublish a inner join ProcessCategory b on b.ProcessCategoryID = a.ProcessCategoryID and b.IsUse <> 0
where a.ProcessCategoryID in ({0}) and a.isAvailable =1
Order by b.OrderNo,a.sort", strIds);
List<IProcessPublish> list = WFModelManager.ProcessPublishDAO.RetrieveListByWhereNoOrder(whereStr);
if ((list != null) && (list.Count > ))
{
foreach (IProcessPublish publish in list)
{
builder.Append("{");
if (lang == "en-us")
{
builder.AppendFormat("\"ProcessName\":\"{0}\",", publish.ProcessEnglishName);
}
else
{
builder.AppendFormat("\"ProcessName\":\"{0}\",", publish.ProcessName);
} builder.AppendFormat("\"ProcessID\":\"{0}\"", publish.ProcessID);
builder.Append("},");
}
}
}
if (builder.ToString().EndsWith(","))
{
builder = builder.Remove(builder.Length - , );
}
builder.Append("]");
base.Response.Write(builder.ToString());
base.Response.End();
}

RetrieveMDSService.aspx

5:html代码

  <asp:HiddenField runat="server" ID="hidDropParProcessCategoryIds" />
<asp:HiddenField runat="server" ID="hidDropParProcessCategoryNames" />
<asp:HiddenField ID="hidSubProcessNames" runat="server" Value="" />
<asp:HiddenField ID="hidSubProcess" runat="server" Value="" />
<asp:HiddenField runat="server" ID="hidDropParProcessCategoryDataSource" /> <td>
<asp:TextBox ID="dropParProcessCategory" ClientIDMode="Static" runat="server" ReadOnly="true" onfocus="showMenu();"></asp:TextBox>

<asp:TextBox ID="dropSubProcess" ClientIDMode="Static" runat="server" ReadOnly="true" onfocus="showMenu2();"></asp:TextBox> </td> <%--流程类别多选--弹出层--开始--%>
<div id="menuProcessCategory" class="menuProcessCategory" style="display: none; position: absolute;">
<ul id="treeProcessCategory" class="ztree" style="margin-top: 0; width: 180px; height: 300px;"></ul>
</div> <div id="menuContent" class="menuContent" style="display: none; position: absolute;">
<ul id="treeDemo" class="ztree" style="margin-top: 0; width: 500px; height: 300px;"></ul>
</div> <%--流程类别多选--弹出层--结束--%>

html代码

6:后台代码

    private void InitDropParProcessCategory(IList<IProcessCategory> list)
{ // { id: 1, pId: 0, name: "北京" },
//{ id: 2, pId: 0, name: "天津" },
//{ id: 3, pId: 0, name: "上海" },
//{ id: 6, pId: 0, name: "重庆" },
StringBuilder sb = new StringBuilder();
if (list != null && list.Count > )
{
sb.Append("["); for (int i = ; i < list.Count; i++)
{ if (list[i] != list[list.Count - ])
{
sb.Append("{ id: " + list[i].ProcessCategoryID + ", pId: " + list[i].ParentCategoryID + ", name: \"" + list[i].ProcessCategoryName.Replace(" └ ", "") + "\" },"); }
else
{
sb.Append("{ id: " + list[i].ProcessCategoryID + ", pId: " + list[i].ParentCategoryID + ", name: \"" + list[i].ProcessCategoryName.Replace(" └ ", "") + "\" , open:true}");
}
}
sb.Append("] ");
}
hidDropParProcessCategoryDataSource.Value = sb.ToString();
}

创建一级目录数据源

步步为营103-ZTree 二级联动的更多相关文章

  1. 微信小程序picker组件 - 省市二级联动

    picker 从底部弹起的滚动选择器,现支持五种选择器,通过mode来区分,分别是普通选择器,多列选择器,时间选择器,日期选择器,省市区选择器,默认是普通选择器. picker官方文档链接 由于项目需 ...

  2. JavaScript 实现省市二级联动

    JavaScript 实现省市二级联动 版权声明:未经授权,严禁转载! 案例代码 <style> .hide { display: none; } </style> <s ...

  3. js实现城市二级联动列表

    这个是一个同事写的,我看着有用,就cv下来了. 程序功能主要逻辑是: 1.当一级标签市显示默认状态 '-请选择-'时,二级标签要隐藏 2.一级标签选中城市时,二级标签显示在页面,并列出响应市区 3.当 ...

  4. iOS开发之"省市"二级联动的数据组织(PHP版)以及PickerView的实现与封装

    之所以要发表这篇博客,还源于最近的开发工作所实现的一个小的Demo, 当然这个Demo不会涉及工作中App的一些内容,下方要实现的Demo是通用的.因为项目需求的迭代,要求在银行卡绑定中添加支行所在的 ...

  5. asp.net 使用DroDownList来实现二级联动

    今天做新闻发布系统的时候,用到了二级联动,我把使用方法记录下来,以便日后查阅以及帮助新手朋友们.下面是效果图: 下面来讲解一下实现的方法: 1.在.aspx页面中,拖入两个DroDownList控件. ...

  6. Select标签下拉列表二级联动级联

    首先从服务器端,绑定下拉列表,二级下拉的text命名按照一定规则加上一级下拉的ID. var options=new Array(); $(document).ready(function(){ // ...

  7. 通过Ajax异步提交的方法实现从数据库获取省份和城市信息实现二级联动(xml方法)

    之前有写过是从JavaScript数组里获取省市信息来实现二级联动,但是似乎有很多需求是要从数据库里获取信息,所以就需要根据异步提交,局部刷新的思想来实现来提高用户交互问题 第一种方法是xml方法 1 ...

  8. 利用JavaScript来实现省份—市县的二级联动

    所谓省-市二级联动是指当选择省份下拉选择框时,市县的下拉框会根据选择的省市而有相应的市县加载出来,如下图所示选择"上海市",城市的下拉选择框只会出现上海的市县: 这种二级联动非常常 ...

  9. 省市二级联动(原生JS)

    代码如下: <html> <head> <meta charset="UTF-8"> <title>省市二级联动</title ...

随机推荐

  1. Java NIO之Selector(选择器)

    历史回顾: Java NIO 概览 Java NIO 之 Buffer(缓冲区) Java NIO 之 Channel(通道) 其他高赞文章: 面试中关于Redis的问题看这篇就够了 一文轻松搞懂re ...

  2. 修改/dev/shm大小 tmps

    /dev/shm  在oracle开启AMM时发现/dev/shm下的空间不够用 WARNING: You are trying to use the MEMORY_TARGET feature. T ...

  3. [sklearn] 官方例程-Imputing missing values before building an estimator 随机填充缺失值

    官方链接:http://scikit-learn.org/dev/auto_examples/plot_missing_values.html#sphx-glr-auto-examples-plot- ...

  4. safarai - loading.close() 无效问题

    代码环境: vue + elenment 问题描述: 上传文件时,显示loading动画:上传成功后,隐藏loading动画.window 下常用的浏览正常,safari 下的chrome浏览器(目前 ...

  5. python操作三大主流数据库(9)python操作mongodb数据库③mongodb odm模型mongoengine的使用

    python操作mongodb数据库③mongodb odm模型mongoengine的使用 文档:http://mongoengine-odm.readthedocs.io/guide/ 安装pip ...

  6. 如何安装和配置RabbitMQ

    今天开始一个小小的练习,学习一下安装和配置RabbitMQ,为什么要学它,因为WCF可以完全兼容和使用RabbitMQ了.我们新的大数据系统需要使用消息队列,所以就开始研究一下,把它重新封装一下,更便 ...

  7. Light OJ 1148

    题意: 给你N 个人, 每个人说出有多少人和他一队, 不包括他自己, 输出总人数最少值 思路: 排个序, 按照给的数目把人分为一组,就可以得出最少人数 #include<bits/stdc++. ...

  8. ActiveMQ在Windows下的安装与启动(懒人专属)

    其实这些ActiveMQ官网都有,但是如果你懒得看官网,那就直接看这吧! 1. 官网下载最新的ActiveMQ安装包 apache-activemq-x.x.x-bin.zip并解压   2.进入安装 ...

  9. which命令和bin目录

    命令: which 作用: 查看执行命令所在位置 使用: which ls which useradd 等等... bin和sbin: 绝大多数可执行文件都保存在 /bin./sbin./usr/bi ...

  10. 手机端上点击input框软键盘出现时把input框不被覆盖,显示在屏幕中间(转)

    转载地址:https://www.cnblogs.com/xzzzys/p/7526761.html 1  用定位为题来解决var oHeight = $(document).height(); // ...