Ztree ajax关键字检索动态树

前端之家收集整理的这篇文章主要介绍了Ztree ajax关键字检索动态树前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
http://blog.csdn.net/liuyuhua0066/article/details/8949885?reload

实现效果

初始页面

匹配记录

无记录


Js代码

[javascript] view plain copy
  1. <scripttype="text/javascript">
  2. varquickMsg="输入关键字按回车键检索";
  3. $(function(){
  4. $("#quickQuery").focus(
  5. function(){
  6. if($.trim($("#quickQuery").val())==quickMsg)
  7. {
  8. $("#quickQuery").val("");
  9. }
  10. });
  11. });
  12. functioncheckBack(e,treeId,treeNode){
  13. varzTree=$.fn.zTree.getZTreeObj("treeForRole");
  14. nodes=zTree.getCheckedNodes(true);
  15. id="";
  16. name="";
  17. nodes.sort(functioncompare(a,b){returna.id-b.id;});
  18. for(vari=0,l=nodes.length;i<l;i++)
  19. {
  20. id+=nodes[i].id+",";
  21. name+=nodes[i].name+",";
  22. }
  23. if(name.length>0)
  24. {
  25. name=name.substring(0,name.length-1);
  26. }
  27. if(id.length>0)
  28. {
  29. id=id.substring(0,id.length-1);
  30. }
  31. varselObj=$("#userForRoleSel");
  32. selObj.attr("value",name);
  33. $("#selectRole").val(id);
  34. $("#userForRoleSel").focus();
  35. }
  36. //ajax回调如果无记录则给予提示
  37. functionasyncBack(event,treeNode,msg){
  38. varnodes=$.fn.zTree.getZTreeObj("treeForRole").getNodes();
  39. if(nodes.length==0)
  40. {
  41. $("#treeForRole").html("<spanstyle='color:#ff0000;margin-left:10px;margin-top:105px;'>未检索到匹配的记录</span>");
  42. }
  43. }
  44. functionshowMenu(){
  45. $(function(){
  46. if($("#quickQuery").val()=="")
  47. {
  48. $("#quickQuery").val(quickMsg);
  49. }
  50. document.onkeydown=function(e)
  51. {
  52. //回车触发ajax查询
  53. if((e?e.which:event.keyCode)==13)
  54. {
  55. varsetting={
  56. check:{
  57. enable:true,
  58. chkStyle:"checkBox",
  59. chkBoxType:{"Y":"s","N":"s"}
  60. },
  61. callback:{
  62. onCheck:checkBack,
  63. onAsyncSuccess:asyncBack
  64. },
  65. async:{
  66. enable:true,
  67. url:"User_ajaxRoles.action",
  68. otherParam:{"nameKey":$.trim($("#quickQuery").val())},
  69. dataType:"json",
  70. dataFilter:null
  71. },
  72. data:{
  73. simpleData:{
  74. enable:true
  75. }
  76. }
  77. };
  78. $.fn.zTree.init($("#treeForRole"),setting);
  79. }
  80. };
  81. });
  82. varselObj=$("#userForRoleSel");
  83. varbusinessOffset=$("#userForRoleSel").offset();
  84. $("#menuContentForRole").css({left:businessOffset.left+"px",top:businessOffset.top+selObj.outerHeight()+"px"}).slideDown("fast");
  85. $("body").bind("mousedown",onBodyDown);
  86. }
  87. functionhideMenu(){
  88. $("#menuContentForRole").fadeOut("fast");
  89. }
  90. functiononBodyDown(event){
  91. if(!(event.target.id=="menuBtnForRole"||event.target.id=="menuContentForRole"||$(event.target).parents("#menuContentForRole").length>0)){
  92. hideMenu();
  93. }
  94. }
  95. </script>

HTML代码

  1. <s:textfieldid="userForRoleSel"name="userForRoleSel"readonly="true"size="18"/>
  2. <aid="menuBtnForRole"href="javascript:showMenu();">选择</a>
  3. <divid="menuContentForRole"class="menuContent"
  4. style="border:1pxsolid#ccc;width:198px;height:160px;overflow:scroll;scroll-y;display:none;position:absolute;background-color:#fcfcfc;">
  5. <inputclass="quickText"id="quickQuery"type="text"/>
  6. <ulid="treeForRole"class="ztree"style="margin-top:0;width:140px;">
  7. </ul>
  8. </div>
  9. <s:hiddenid="selectRole"name="role.roleId"/>

后台使用Struts2,相关代码如下

Action代码

  1. publicStringajaxRoles()
  2. throwsException
  3. {
  4. try
  5. {
  6. result=roleService.getAjaxRoles(nameKey);
  7. }
  8. catch(Exceptione)
  9. {
  10. log.error(e.getMessage());
  11. throwe;
  12. }
  13. return"ajaxRoles";
  14. }

Struts2-User.xml 代码

  1. <packagename="rbac_user"namespace="/"extends="json">
  2. <actionname="User_*"class="xx.UserAction"method="{1}">
  3. <resultname="ajaxRoles"type="json">
  4. <paramname="root">result</param>
  5. </result>
  6. </action>
  7. </package>

Service代码

  1. publicStringgetAjaxRoles(StringnameKey)
  2. {
  3. //Dao层是一个简单的根据name查询不再赘述
  4. List<Role>results=roleDao.getAjaxRoles(nameKey);
  5. JSONArrayjson=newJSONArray();
  6. for(Rolero:results)
  7. {
  8. JSONObjectjo=newJSONObject();
  9. jo.put("id",ro.getRoleId());
  10. jo.put("name",ro.getName());
  11. json.add(jo);
  12. }
  13. returnjson.toJSONString();
  14. }
原文链接:https://www.f2er.com/ajax/166134.html

猜你在找的Ajax相关文章