ztree-object.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * ztree插件的封装
  3. */
  4. (function() {
  5. var $ZTree = function(id, url) {
  6. this.id = id;
  7. this.url = url;
  8. this.onClick = null;
  9. this.settings = null;
  10. this.ondblclick=null;
  11. };
  12. $ZTree.prototype = {
  13. /**
  14. * 初始化ztree的设置
  15. */
  16. initSetting : function() {
  17. var settings = {
  18. view : {
  19. dblClickExpand : true,
  20. selectedMulti : false
  21. },
  22. data : {simpleData : {enable : true}},
  23. callback : {
  24. onClick : this.onClick,
  25. onDblClick:this.ondblclick
  26. }
  27. };
  28. return settings;
  29. },
  30. /**
  31. * 手动设置ztree的设置
  32. */
  33. setSettings : function(val) {
  34. this.settings = val;
  35. },
  36. /**
  37. * 初始化ztree
  38. */
  39. init : function() {
  40. var zNodeSeting = null;
  41. if(this.settings != null){
  42. zNodeSeting = this.settings;
  43. }else{
  44. zNodeSeting = this.initSetting();
  45. }
  46. var zNodes = this.loadNodes();
  47. $.fn.zTree.init($("#" + this.id), zNodeSeting, zNodes);
  48. },
  49. /**
  50. * 绑定onclick事件
  51. */
  52. bindOnClick : function(func) {
  53. this.onClick = func;
  54. },
  55. /**
  56. * 绑定双击事件
  57. */
  58. bindOnDblClick : function(func) {
  59. this.ondblclick=func;
  60. },
  61. /**
  62. * 加载节点
  63. */
  64. loadNodes : function() {
  65. var zNodes = null;
  66. var ajax = new $ax(Feng.ctxPath + this.url, function(data) {
  67. zNodes = data;
  68. }, function(data) {
  69. Feng.error("加载ztree信息失败!");
  70. });
  71. ajax.start();
  72. return zNodes;
  73. },
  74. /**
  75. * 获取选中的值
  76. */
  77. getSelectedVal : function(){
  78. var zTree = $.fn.zTree.getZTreeObj(this.id);
  79. var nodes = zTree.getSelectedNodes();
  80. return nodes[0].name;
  81. }
  82. };
  83. window.$ZTree = $ZTree;
  84. }());