菜单

模糊搜索简单实例分享

2020年3月18日 - 新闻中心

一、节点模糊搜索功用:搜索成功后,自动高亮显示并一定、展开搜寻到的节点。

澳门太阳集团,jquery zTree异步加载、模糊搜索轻松实例分享,jqueryztree

正文实例为我们讲授了jquery zTree树插件的大旨采取办法,具体内容如下

一、节点模糊搜索效果:搜索成功后,自动高亮显示并一直、张开索求到的节点。

二、节点异步加载:1、点击展开时加载数据;2、选中节点时加载数据。
前台代码如下:

<script type="text/javascript">
 //ztree设置
 var setting = {
 view: {
 fontCss: getFontCss
 },
 check: {
 enable: true
 },
 data: {
 simpleData: {
 enable: true,
 idKey: "id",
 pIdKey: "pId",
 rootPId: 0
 }
 },
 async: {
 enable: true,
 url: "#{getStudentsJsonUrl}",
 autoParam: ["id", "level"]
 },
 callback: {
 beforeCheck: zTreeBeforeCheck,
 onNodeCreated: zTreeOnNodeCreated,
 beforeExpand: zTreeBeforeExpand
 }
 };

 var reloadFlag = false; //是否重新异步请求
 var checkFlag = true; //是否选中

 //节点展开前
 function zTreeBeforeExpand(treeId, treeNode) {
 reloadFlag = false;
 return true;
 };

 //节点创建后
 function zTreeOnNodeCreated(event, treeId, treeNode) {
 var zTree = $.fn.zTree.getZTreeObj(treeId);
 if (reloadFlag) {
 if (checkFlag) {
 zTree.checkNode(treeNode, true, true);
 }
 if (!treeNode.children) {
 zTree.reAsyncChildNodes(treeNode, "refresh");
 }
 }
 };

 //选中节点前
 function zTreeBeforeCheck(treeId, treeNode) {
 var zTree = $.fn.zTree.getZTreeObj(treeId);
 if (!treeNode.children) {
 reloadFlag = true;
 checkFlag = true;
 zTree.reAsyncChildNodes(treeNode, "refresh");
 }
 return true;
 }

 //页面加载完成
 _run(function () {
 require(['zTree/js/jquery.ztree.all-3.5'], function () {
 $.ajax({
 type: "POST",
 url: "#{getStudentsJsonUrl}",
 success: function (data) {
  if (data && data.length != 0) { //如果结果不为空
  $.fn.zTree.init($("#tree"), setting, data);
  }
  else { //搜索不到结果

  }
 }
 });
 });

 //提交
 $("#inputSubmit").click(function () {
 var zTree = $.fn.zTree.getZTreeObj("tree");
 var nodes = zTree.getCheckedNodes(true);
 var ids = "";
 var names = "";
 for (var i = 0; i < nodes.length; i++) { //遍历选择的节点集合
 if (!nodes[i].isParent) {
  ids += nodes[i].id.replace("level" + nodes[i].level, "") + ",";
  names += nodes[i].name + ",";
 }
 }
 Simpo.ui.box.hideBox();
 parent.$(".boxFrm").contents().find("#inputRange").val(names.substr(0, names.length - 1));
 parent.$(".boxFrm").contents().find("#hidRange").val(ids.substr(0, ids.length - 1));
 })
 });

 //查找节点
 var lastNodeList = [];
 var lastKey;
 function searchNode() {
 var zTree = $.fn.zTree.getZTreeObj("tree");

 var key = $.trim($("#inputSearchNode").val());
 if (key != "" && key != lastKey) {
 nodeList = zTree.getNodesByParamFuzzy("name", key);
 for (var i = 0, l = lastNodeList.length; i < l; i++) { //上次查询的节点集合取消高亮
 lastNodeList[i].highlight = false;
 zTree.updateNode(lastNodeList[i]);
 }
 zTree.expandAll(false); //全部收缩
 if (nodeList.length > 0) {
 for (var i = 0, l = nodeList.length; i < l; i++) { //遍历找到的节点集合
  if (nodeList[i].getParentNode()) {
  zTree.expandNode(nodeList[i].getParentNode(), true, false, false); //展开其父节点
  }
  nodeList[i].highlight = true;
  zTree.updateNode(nodeList[i]);
 }
 }
 zTree.refresh(); // 很重要,否则节点状态更新混乱。
 lastNodeList = nodeList;
 lastKey = key;
 }
 }

 //加载数据
 function loadData() {
 var zTree = $.fn.zTree.getZTreeObj("tree");
 var rootNodes = zTree.getNodes();
 reloadFlag = true;
 checkFlag = false;
 for (var i = 0; i < rootNodes.length; i++) {
 if (!rootNodes[i].children) {
 zTree.reAsyncChildNodes(rootNodes[i], "refresh"); //异步加载
 }
 }
 }

 //全部收缩
 function closeAll() {
 var zTree = $.fn.zTree.getZTreeObj("tree");
 if ($("#inputCloseAll").val() == "全部收缩") {
 zTree.expandAll(false);
 $("#inputCloseAll").val("全部展开")
 }
 else {
 zTree.expandAll(true);
 $("#inputCloseAll").val("全部收缩")
 }
 }

 //高亮样式
 function getFontCss(treeId, treeNode) {
 return (treeNode.highlight) ? { color: "#A60000", "font-weight": "bold"} : { color: "#333", "font-weight": "normal" };
 }
</script>

<div style="width: 200px; height: 260px; overflow: auto; border: solid 1px #666;">
 <ul id="tree" class="ztree">
 </ul>
 </div>

后台代码(后台重返Json数据):

 public void SelStudent()
 {
 set("getStudentsJsonUrl", to(GetStudentsJson));
 }

 public void GetStudentsJson()
 {
 List<Dictionary<string, string>> dicList = new List<Dictionary<string, string>>();

 string level = ctx.Post("level");
 string id = ctx.Post("id");
 if (strUtil.IsNullOrEmpty(id))
 {
 #region 加载班级
 //获取当前登录用户
 Sys_User user = AdminSecurityUtils.GetLoginUser(ctx);
 //获取当前用户关联的老师
 Edu_Teacher teacher = edu_TeacService.FindByUserId(user.Id);
 //获取班级集合
 List<Edu_ClaNameFlow> list = edu_ClaNameFlowService.GetListByTeacherId(teacher.Id);
 foreach (Edu_ClaNameFlow item in list)
 {
  Dictionary<string, string> dic = new Dictionary<string, string>();
  dic.Add("id", "level0" + item.Calss.Id.ToString());
  dic.Add("pId", "0");
  dic.Add("name", item.Gra.Name + item.Calss.Name);
  dic.Add("isParent", "true");
  dicList.Add(dic);
 }
 #endregion
 }
 else
 {
 if (level == "0")
 {
  //加载学生
  List<Edu_Student> list = edu_StudService.GetListByClassId(id.Replace("level0", ""));
  foreach (Edu_Student item in list)
  {
  Dictionary<string, string> dic = new Dictionary<string, string>();
  dic.Add("id", "level1" + item.Id.ToString());
  dic.Add("pId", id);
  dic.Add("name", item.Name);
  dic.Add("isParent", "false");
  dicList.Add(dic);
  }
 }
 }

 echoJson(dicList);
 }

三、基于cookie实现zTree树刷新后,展开状态不改变

模糊搜索简单实例分享。1、除了援引jQuery和zTree的JS外,援引cookie的JS:
复制代码 代码如下:<script
type=”text/javascript”
src=”~/Scripts/jquery-treeview/lib/jquery.cookie.js”></script>

2、JS代码:

$(function () {
 //ztree设置
 var setting = {
  data: {
   simpleData: {
    enable: true,
    idKey: "id",
    pIdKey: "pId",
    rootPId: null
   }
  },
  callback: {
   onExpand: onExpand,
   onCollapse: onCollapse
  }
 };

 $.ajax({
  type: "POST",
  url: "/Tech/TemplateTypeManage/GetData",
  success: function (data) {
   if (data && data.length != 0) {
    $.fn.zTree.init($("#tree"), setting, data);
    var treeObj = $.fn.zTree.getZTreeObj("tree");
    var cookie = $.cookie("z_tree" + window.location);
    if (cookie) {
     z_tree = JSON2.parse(cookie);
     for (var i = 0; i < z_tree.length; i++) {
      var node = treeObj.getNodeByParam('id', z_tree[i])
      treeObj.expandNode(node, true, false)
     }
    }
   }
  }
 });
});//end $

function onExpand(event, treeId, treeNode) {
 var cookie = $.cookie("z_tree" + window.location);
 var z_tree = new Array();
 if (cookie) {
  z_tree = JSON2.parse(cookie);
 }
 if ($.inArray(treeNode.id, z_tree) < 0) {
  z_tree.push(treeNode.id);
 }
 $.cookie("z_tree" + window.location, JSON2.stringify(z_tree))
}

function onCollapse(event, treeId, treeNode) {
 var cookie = $.cookie("z_tree" + window.location);
 var z_tree = new Array();
 if (cookie) {
  z_tree = JSON2.parse(cookie);
 }
 var index = $.inArray(treeNode.id, z_tree);
 z_tree.splice(index, 1);
 for (var i = 0; i < treeNode.children.length; i++) {
  index = $.inArray(treeNode.children[i].id, z_tree);
  if (index > -1) z_tree.splice(index, 1);
 }
 $.cookie("z_tree" + window.location, JSON2.stringify(z_tree))
}

如上就是关于树插件zTree异步加载、模糊寻觅容易实例批注,希望对我们的上学抱有利于。

二、节点异步加载:1、点击展开时加载数据;2、选中节点时加载数据。

你只怕感兴趣的篇章:

zTree异步加载、模糊搜索轻易实例分享,jqueryztree
本文实例为大家疏解了jquery zTree树插件的主题选择办法,具体内容如下
一、节点模糊…

//ztree&#35774;&#32622;var setting = {view: {fontCss: getFontCss},check: {enable: true},data: {simpleData: {enable: true,idKey: "id",pIdKey: "pId",rootPId: 0}},async: {enable: true,url: "#{getStudentsJsonUrl}",autoParam: ["id", "level"]},callback: {beforeCheck: zTreeBeforeCheck,onNodeCreated: zTreeOnNodeCreated,beforeExpand: zTreeBeforeExpand}};var reloadFlag = false; //&#26159;&#21542;&#37325;&#26032;&#24322;&#27493;&#35831;&#27714;var checkFlag = true; //&#26159;&#21542;&#36873;&#20013;//&#33410;&#28857;&#23637;&#24320;&#21069;function zTreeBeforeExpand {reloadFlag = false;return true;};//&#33410;&#28857;&#21019;&#24314;&#21518;function zTreeOnNodeCreated(event, treeId, treeNode) {var zTree = $.fn.zTree.getZTreeObj;if  {if  {zTree.checkNode;}if  {zTree.reAsyncChildNodes;}}};//&#36873;&#20013;&#33410;&#28857;&#21069;function zTreeBeforeCheck {var zTree = $.fn.zTree.getZTreeObj;if  {reloadFlag = true;checkFlag = true;zTree.reAsyncChildNodes;}return true;}//&#39029;&#38754;&#21152;&#36733;&#23436;&#25104;_run {require(['zTree/js/jquery.ztree.all-3.5'], function () {$.ajax({type: "POST",url: "#{getStudentsJsonUrl}",success: function  {if (data &amp;&amp; data.length != 0) { //&#22914;&#26524;&#32467;&#26524;&#19981;&#20026;&#31354;$.fn.zTree.init, setting, data);}else { //&#25628;&#32034;&#19981;&#21040;&#32467;&#26524;}}});});//&#25552;&#20132;$.click {var zTree = $.fn.zTree.getZTreeObj;var nodes = zTree.getCheckedNodes;var ids = "";var names = "";for (var i = 0; i &lt; nodes.length; i++) { //&#36941;&#21382;&#36873;&#25321;&#30340;&#33410;&#28857;&#38598;&#21512;if  {ids += nodes[i].id.replace("level" + nodes[i].level, "") + ",";names += nodes[i].name + ",";}}Simpo.ui.box.hideBox.contents.val(names.substr;parent.$.find.val(ids.substr;})});//&#26597;&#25214;&#33410;&#28857;var lastNodeList = [];var lastKey;function searchNode() {var zTree = $.fn.zTree.getZTreeObj;var key = $.trim.val;if (key != "" &amp;&amp; key != lastKey) {nodeList = zTree.getNodesByParamFuzzy;for (var i = 0, l = lastNodeList.length; i &lt; l; i++) { //&#19978;&#27425;&#26597;&#35810;&#30340;&#33410;&#28857;&#38598;&#21512;&#21462;&#28040;&#39640;&#20142;lastNodeList[i].highlight = false;zTree.updateNode;}zTree.expandAll; //&#20840;&#37096;&#25910;&#32553;if  {for (var i = 0, l = nodeList.length; i &lt; l; i++) { //&#36941;&#21382;&#25214;&#21040;&#30340;&#33410;&#28857;&#38598;&#21512;if (nodeList[i].getParentNode {zTree.expandNode(nodeList[i].getParentNode(), true, false, false); //&#23637;&#24320;&#20854;&#29238;&#33410;&#28857;}nodeList[i].highlight = true;zTree.updateNode;}}zTree.refresh(); // &#24456;&#37325;&#35201;&#65292;&#21542;&#21017;&#33410;&#28857;&#29366;&#24577;&#26356;&#26032;&#28151;&#20081;&#12290;lastNodeList = nodeList;lastKey = key;}}//&#21152;&#36733;&#25968;&#25454;function loadData() {var zTree = $.fn.zTree.getZTreeObj;var rootNodes = zTree.getNodes();reloadFlag = true;checkFlag = false;for (var i = 0; i &lt; rootNodes.length; i++) {if (!rootNodes[i].children) {zTree.reAsyncChildNodes(rootNodes[i], "refresh"); //&#24322;&#27493;&#21152;&#36733;}}}//&#20840;&#37096;&#25910;&#32553;function closeAll() {var zTree = $.fn.zTree.getZTreeObj;if .val {zTree.expandAll;$.val}else {zTree.expandAll;$.val}}//&#39640;&#20142;&#26679;&#24335;function getFontCss {return  &amp;#63; { color: "#A60000", "font-weight": "bold"} : { color: "#333", "font-weight": "normal" };}



 public void SelStudent() { set("getStudentsJsonUrl", to; } public void GetStudentsJson() { List> dicList = new List>(); string level = ctx.Post; string id = ctx.Post; if (strUtil.IsNullOrEmpty { #region 加载班级 //获取当前登录用户 Sys_User user = AdminSecurityUtils.GetLoginUser; //获取当前用户关联的老师 Edu_Teacher teacher = edu_TeacService.FindByUserId; //获取班级集合 List list = edu_ClaNameFlowService.GetListByTeacherId; foreach (Edu_ClaNameFlow item in list) { Dictionary dic = new Dictionary(); dic.Add("id", "level0" + item.Calss.Id.ToString; dic.Add; dic.Add("name", item.Gra.Name + item.Calss.Name); dic.Add; dicList.Add; } #endregion } else { if  { //加载学生 List list = edu_StudService.GetListByClassId(id.Replace; foreach (Edu_Student item in list) { Dictionary dic = new Dictionary(); dic.Add("id", "level1" + item.Id.ToString; dic.Add; dic.Add; dic.Add; dicList.Add; } } } echoJson; }

越来越多关于ztree控件的开始和结果,请参见专项论题《jQuery插件ztree使用汇总》 。

上述便是本文的全体内容,希望对大家的读书抱有助于,也盼望我们多都赐教脚本之家。

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图