selectTree.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. var parentTopHeight;
  2. var broswerFlag;
  3. var sigleSelectionSetting = {
  4. view: {
  5. dblClickExpand: false,
  6. selectedMulti: false
  7. },
  8. data: {
  9. simpleData: {
  10. enable: true
  11. }
  12. },
  13. callback: {
  14. onClick: zTreeSelectItemClick
  15. }
  16. };
  17. var multiNoGroupSelectionSetting = {
  18. view: {
  19. selectedMulti: true,
  20. showIcon: false,
  21. showLine: false
  22. },
  23. check: {
  24. enable: true,
  25. chkboxType: {
  26. Y: "",
  27. N: ""
  28. }
  29. },
  30. data: {
  31. simpleData: {
  32. enable: true
  33. }
  34. },
  35. callback: {
  36. onClick: zTreeSelectItemClick
  37. }
  38. };
  39. var multiTreeSelectionSetting = {
  40. view: {
  41. selectedMulti: false
  42. },
  43. check: {
  44. enable: true,
  45. chkboxType: {
  46. Y: "",
  47. N: ""
  48. }
  49. },
  50. data: {
  51. simpleData: {
  52. enable: true
  53. }
  54. },
  55. callback: {
  56. onClick: zTreeSelectItemClick
  57. }
  58. };
  59. var multiTreeSelectionSetting2 = {
  60. view: {
  61. selectedMulti: false
  62. },
  63. check: {
  64. enable: true,
  65. chkboxType: {
  66. Y: "ps",
  67. N: "ps"
  68. }
  69. },
  70. data: {
  71. simpleData: {
  72. enable: true
  73. }
  74. },
  75. callback: {
  76. onClick: zTreeSelectItemClick
  77. }
  78. };
  79. jQuery.fn.extend({
  80. selectTreeRender: function (a) {
  81. return this.each(function () {
  82. $(this).html("");
  83. new jQuery.SelectTreeBox(this, a)
  84. })
  85. },
  86. selectTreeAddItem: function (a) {
  87. this.each(function () {
  88. var b = $(this).data("data");
  89. var c = "treeNodes";
  90. if ($(this).attr("dataRoot")) {
  91. c = $(this).attr("dataRoot")
  92. }
  93. b[c].push(a);
  94. $(this).data("data", b);
  95. $(this).html("");
  96. new jQuery.SelectTreeBox(this)
  97. })
  98. },
  99. selectTreeRemoveItem: function (a) {
  100. this.each(function () {
  101. var b = $(this).data("data");
  102. var c = -1;
  103. var d = "treeNodes";
  104. if ($(this).attr("dataRoot")) {
  105. d = $(this).attr("dataRoot")
  106. }
  107. $.each(b[d], function (e, f) {
  108. if (f.id.toString() == a) {
  109. c = e
  110. }
  111. });
  112. if (c != -1) {
  113. b[d].splice(c, 1)
  114. }
  115. $(this).data("data", b);
  116. $(this).html("");
  117. new jQuery.SelectTreeBox(this)
  118. })
  119. }
  120. });
  121. var depth = 500;
  122. var selectTree_id = 1;
  123. jQuery.SelectTreeBox = function (F, H) {
  124. var o = {};
  125. o.inputClass = o.inputClass || "selectbox";
  126. o.containerClass = o.containerClass || "selectbox-tree";
  127. o.hoverClass = o.hoverClass || "current";
  128. o.currentClass = o.selectedClass || "selected";
  129. o.debug = o.debug || false;
  130. selectTree_id++;
  131. var g = "请选择";
  132. var k = "0_input";
  133. var w = "0_button";
  134. var u = false;
  135. var I = $(F);
  136. I.addClass("mainCon");
  137. if (I.attr("prompt") != null) {
  138. g = I.attr("prompt")
  139. }
  140. var f = t(o);
  141. var c = $('<ul class="ztree"></ul>');
  142. c.attr("id", "selectTree" + selectTree_id + "_tree");
  143. var v = G(o);
  144. var p = D(o);
  145. var s;
  146. s = $("<input type='button' value=' ' class='selBtn'/>");
  147. var a = false;
  148. if (I.attr("multiMode") != null) {
  149. if (I.attr("multiMode") == "true" || I.attr("multiMode") == true) {
  150. a = true;
  151. s.addClass("selBtnMuiti");
  152. if (I.attr("noGroup") == "true" || I.attr("noGroup") == true) {
  153. c.addClass("noGroupZtree")
  154. } else {
  155. c.addClass("multiSelectZtree")
  156. }
  157. } else {
  158. a = false
  159. }
  160. }
  161. if (I.attr("disabled") == "disabled" || I.attr("disabled") == "true" || I.attr("disabled") == true) {
  162. s.attr("disabled", true);
  163. if (a == true) {
  164. s.addClass("selBtn_disabledMuiti")
  165. } else {
  166. s.addClass("selBtn_disabled")
  167. }
  168. v.addClass("selectbox_disabled")
  169. }
  170. s.attr("id", "selectTree" + selectTree_id + "_button");
  171. var m = 135;
  172. if (I.attr("selWidth") != null) {
  173. m = Number(I.attr("selWidth")) - 22
  174. }
  175. v.width(m);
  176. var L = $('<table cellspacing="0" cellpadding="0" style="border-style:none;"><tr><td class="ali01" style="border-style:none;padding:0;margin:0;"></td><td class="ali01" style="border-style:none;;padding:0;margin:0;"></td></tr></table>');
  177. L.find("td").eq(0).append(v);
  178. L.find("td").eq(1).append(s);
  179. I.append(L);
  180. I.append(f);
  181. I.append(p);
  182. f.append(c);
  183. var C = "";
  184. if (I.attr("selectedValue")) {
  185. C = I.attr("selectedValue")
  186. }
  187. var q = false;
  188. if (I.attr("editable") != null) {
  189. if (I.attr("editable") == "true") {
  190. q = true
  191. } else {
  192. q = false
  193. }
  194. }
  195. f.hide();
  196. var z = "treeNodes";
  197. if (I.attr("dataRoot")) {
  198. z = I.attr("dataRoot")
  199. }
  200. var n = I.attr("params");
  201. var A;
  202. if (n) {
  203. try {
  204. A = JSON.parse(n)
  205. } catch (K) {
  206. A = "";
  207. alert("树形下拉框参数格式有误!(提示:json数据的属性和名称必须以双引号包围)")
  208. }
  209. } else {
  210. A = ""
  211. }
  212. var d = "";
  213. var E = I.attr("url");
  214. var x = I.attr("data");
  215. var l = I.data("data");
  216. if (l) {
  217. B(l)
  218. } else {
  219. if (x) {
  220. try {
  221. d = JSON.parse(x)
  222. } catch (K) {
  223. d = "";
  224. alert("树形下拉框参数格式有误!(提示:放在标签中的json数据的属性和名称必须以双引号包围)")
  225. }
  226. I.data("data", d);
  227. B(d)
  228. } else {
  229. if (E) {
  230. $.ajax({
  231. url: I.attr("url"),
  232. dataType: "json",
  233. data: A,
  234. error: function () {
  235. alert("树形下拉框数据源出错,请检查url路径")
  236. },
  237. success: function (e) {
  238. I.data("data", e);
  239. d = e;
  240. B(e)
  241. }
  242. })
  243. }
  244. }
  245. }
  246. if (!q) {
  247. v.css({
  248. cursor: "pointer"
  249. });
  250. v.click(function (e) {
  251. k = $(e.target).attr("id");
  252. y();
  253. depth++;
  254. I.css({
  255. zIndex: depth
  256. });
  257. if (f.attr("hasfocus") == 0) {
  258. r()
  259. } else {
  260. b()
  261. }
  262. })
  263. } else {
  264. v.css({
  265. cursor: "text"
  266. });
  267. v.change(function () {
  268. I.attr("editValue", $(this).val());
  269. p.val($(this).val())
  270. })
  271. }
  272. s.click(function (e) {
  273. w = $(e.target).attr("id");
  274. y();
  275. depth++;
  276. I.css({
  277. zIndex: depth
  278. });
  279. if (f.attr("hasfocus") == 0) {
  280. r()
  281. } else {
  282. b()
  283. }
  284. });
  285. function y() {
  286. f.css({
  287. overflowY: "visible",
  288. overflowX: "visible"
  289. });
  290. f.width("");
  291. var e = 200;
  292. if (parentTopHeight > 0) {
  293. var N = window.top.document.documentElement.clientHeight;
  294. e = N - parentTopHeight - parentBottomHeight - I.offset().top - 30
  295. } else {
  296. e = window.document.documentElement.clientHeight - (I.offset().top - $(window).scrollTop()) - 30
  297. }
  298. var P;
  299. if (!I.attr("boxWidth")) {
  300. P = f.width()
  301. }
  302. f.css({
  303. overflowY: "auto",
  304. overflowX: "hidden"
  305. });
  306. if (!I.attr("boxWidth")) {
  307. f.width(P)
  308. } else {
  309. f.width(Number(I.attr("boxWidth")))
  310. }
  311. var O = 0;
  312. if (I.attr("boxHeight")) {
  313. O = Number(I.attr("boxHeight"))
  314. }
  315. if (O != 0) {
  316. f.height(O);
  317. if (I.attr("openDirection") == "top") {
  318. f.css({
  319. top: -O
  320. })
  321. } else {
  322. if (I.attr("openDirection") == "bottom") {
  323. f.css({
  324. top: 24
  325. })
  326. } else {
  327. if (e < O) {
  328. if (I.offset().top > O) {
  329. f.css({
  330. top: -O
  331. })
  332. } else {
  333. if (e < 100 && I.offset().top > e && I.offset().top > 100) {
  334. f.css({
  335. top: -O
  336. })
  337. } else {
  338. f.css({
  339. top: 24
  340. })
  341. }
  342. }
  343. } else {
  344. f.css({
  345. top: 24
  346. })
  347. }
  348. }
  349. }
  350. } else {
  351. if (I.attr("openDirection") == "top") {
  352. if (I.offset().top > f.height()) {
  353. f.css({
  354. top: -f.height()
  355. })
  356. } else {
  357. f.height($mainCon.offset().top);
  358. f.css({
  359. top: -$mainCon.offset().top
  360. })
  361. }
  362. } else {
  363. if (I.attr("openDirection") == "bottom") {
  364. if (e < f.height()) {
  365. f.css({
  366. top: 24
  367. });
  368. f.height(e)
  369. } else {
  370. f.css({
  371. top: 24
  372. })
  373. }
  374. } else {
  375. if (e < f.height()) {
  376. if (I.offset().top > f.height()) {
  377. f.css({
  378. top: -f.height()
  379. })
  380. } else {
  381. if (e < 100 && I.offset().top > e && I.offset().top > 100) {
  382. f.height(I.offset().top);
  383. f.css({
  384. top: -I.offset().top
  385. })
  386. } else {
  387. f.css({
  388. top: 24
  389. });
  390. f.height(e)
  391. }
  392. }
  393. } else {
  394. f.css({
  395. top: 24
  396. })
  397. }
  398. }
  399. }
  400. }
  401. if (!I.attr("boxWidth")) {
  402. if (f.width() < m + 24) {
  403. f.width(m + 24)
  404. }
  405. }
  406. }
  407. function i() {
  408. var e = $("<div></div>");
  409. e.addClass("mainCon");
  410. return e
  411. }
  412. function t(e) {
  413. var N = $("<div></div>");
  414. N.attr("id", "selectTree" + selectTree_id + "_container");
  415. N.addClass(e.containerClass);
  416. N.attr("hasfocus", 0);
  417. return N
  418. }
  419. function G(N) {
  420. var e = document.createElement("input");
  421. var P = $(e);
  422. P.attr("id", "selectTree" + selectTree_id + "_input");
  423. P.attr("type", "text");
  424. P.addClass(N.inputClass);
  425. P.attr("autocomplete", "off");
  426. var O = false;
  427. if (I.attr("editable") != null) {
  428. if (I.attr("editable") == "true") {
  429. O = true
  430. } else {
  431. O = false
  432. }
  433. }
  434. if (!O) {
  435. P.attr("readonly", "readonly")
  436. } else {
  437. P.attr("readonly", false)
  438. }
  439. if (I.attr("disabled") == "disabled" || I.attr("disabled") == "true" || I.attr("disabled") == true) {
  440. P.attr("disabled", true);
  441. P.addClass("inputDisabled")
  442. }
  443. return P
  444. }
  445. function D(N) {
  446. var e = document.createElement("input");
  447. var O = $(e);
  448. O.attr("type", "hidden");
  449. if (I.attr("name") != null) {
  450. O.attr("name", I.attr("name"))
  451. }
  452. return O
  453. }
  454. function j(N, e) {
  455. I.attr("relText", N);
  456. I.attr("relValue", e);
  457. p.val(e);
  458. v.val(N);
  459. if (q == "true" || q == true) {
  460. I.attr("editValue", v.val());
  461. p.val(v.val())
  462. }
  463. I.focus();
  464. return true
  465. }
  466. function B(N) {
  467. if (!N) {
  468. return
  469. }
  470. if (a == true) {
  471. if (C == "") {
  472. v.val(g);
  473. I.attr("relText", g);
  474. I.attr("relValue", "");
  475. p.val("")
  476. } else {
  477. var V = C.split(",");
  478. var Q = "";
  479. for (var S = 0; S < V.length; S++) {
  480. for (var R = 0; R < N[z].length; R++) {
  481. if (N[z][R].id.toString() == V[S]) {
  482. N[z][R].checked = true;
  483. Q = Q + N[z][R].name + ",";
  484. continue
  485. }
  486. }
  487. }
  488. if (Q.length > 0) {
  489. Q = Q.substring(0, Q.length - 1)
  490. }
  491. j(Q, C);
  492. v.attr("title", Q);
  493. try {
  494. enableTooltips()
  495. } catch (U) {}
  496. }
  497. if (I.attr("noGroup") == "true" || I.attr("noGroup") == true) {
  498. if (H) {
  499. $.fn.zTree.init(c, H, N[z])
  500. } else {
  501. $.fn.zTree.init(c, multiNoGroupSelectionSetting, N[z])
  502. }
  503. } else {
  504. if (I.attr("allSelectable") == "true" || I.attr("allSelectable") == true) {
  505. if (H) {
  506. $.fn.zTree.init(c, H, N[z])
  507. } else {
  508. $.fn.zTree.init(c, multiTreeSelectionSetting2, N[z])
  509. }
  510. } else {
  511. if (H) {
  512. $.fn.zTree.init(c, H, N[z])
  513. } else {
  514. $.fn.zTree.init(c, multiTreeSelectionSetting, N[z])
  515. }
  516. }
  517. }
  518. } else {
  519. if (H) {
  520. $.fn.zTree.init(c, H, N[z])
  521. } else {
  522. $.fn.zTree.init(c, sigleSelectionSetting, N[z])
  523. }
  524. if (C == "") {
  525. v.val(g);
  526. I.attr("relText", g);
  527. I.attr("relValue", "");
  528. p.val("")
  529. } else {
  530. I.attr("relValue", C);
  531. p.val(C);
  532. var O = $.fn.zTree.getZTreeObj(c.attr("id"));
  533. var P = O.transformToArray(O.getNodes());
  534. for (var T = 0; T < P.length; T++) {
  535. if (P[T].id.toString() == C) {
  536. O.selectNode(P[T]);
  537. I.attr("relText", P[T].name);
  538. v.val(P[T].name)
  539. }
  540. }
  541. }
  542. }
  543. if (q == true) {
  544. if (C == "") {
  545. I.attr("editValue", g)
  546. } else {
  547. I.attr("editValue", I.attr("relText"))
  548. }
  549. }
  550. }
  551. function b() {
  552. f.attr("hasfocus", 0);
  553. f.hide();
  554. $("body").unbind("mousedown", J);
  555. if (a == true) {
  556. var O = $.fn.zTree.getZTreeObj(c.attr("id"));
  557. if (O) {
  558. var N = O.getCheckedNodes(true);
  559. var R = "";
  560. var Q = "";
  561. for (var P = 0; P < N.length; P++) {
  562. if (I.attr("exceptParent") == "true" || I.attr("exceptParent") == true) {
  563. if (N[P].isParent) {
  564. continue
  565. }
  566. }
  567. R = R + N[P].name + ",";
  568. Q = Q + N[P].id + ","
  569. }
  570. if (R.length > 0) {
  571. R = R.substring(0, R.length - 1)
  572. }
  573. if (Q.length > 0) {
  574. Q = Q.substring(0, Q.length - 1)
  575. }
  576. if (R == "") {
  577. R = g
  578. }
  579. j(R, Q);
  580. if (R == g) {
  581. v.attr("title", " ")
  582. } else {
  583. v.attr("title", R)
  584. }
  585. try {
  586. enableTooltips()
  587. } catch (S) {}
  588. }
  589. }
  590. try {
  591. I.trigger("change")
  592. } catch (S) {}
  593. }
  594. function r() {
  595. f.attr("hasfocus", 1);
  596. depth++;
  597. I.css({
  598. zIndex: depth
  599. });
  600. f.show();
  601. $("body").bind("mousedown", J)
  602. }
  603. function J(e) {
  604. if (f.attr("hasfocus") == 0) {} else {
  605. if ($(e.target).attr("id") == k || $(e.target).attr("id") == w || $(e.target).parent().attr("class") == "ztree" || $(e.target).attr("class") == "ztree" || $(e.target).parents(".ztree").length > 0 || $(e.target).attr("class") == "selectbox-tree") {
  606. if ($(e.target).parents(".ztree").length > 0) {
  607. setTimeout(function () {
  608. y()
  609. }, 500)
  610. }
  611. } else {
  612. b()
  613. }
  614. }
  615. }
  616. function h() {
  617. return I.val()
  618. }
  619. function M() {
  620. return v.val()
  621. }
  622. };
  623. function getPosition(b, c) {
  624. for (var a = 0; a < c.length; a++) {
  625. if (b == c[a]) {
  626. return a;
  627. break
  628. }
  629. }
  630. }
  631. String.prototype.trim = function () {
  632. return this.replace(/(^\s*)|(\s*$)/g, "")
  633. };
  634. function zTreeSelectItemClick(b, d, i) {
  635. var c = $("#" + d).parents(".selectTree");
  636. var a = $("#" + d).parents(".mainCon").find('input[type="hidden"]');
  637. var h = $.fn.zTree.getZTreeObj(d);
  638. if (c.attr("multiMode") == "true" || c.attr("multiMode") == true) {
  639. if (i.clickExpand == true || i.clickExpand == "true") {
  640. if (c.attr("allSelectable") == "true" || c.attr("allSelectable") == true) {
  641. h.checkNode(i, "", true);
  642. h.expandNode(i, true)
  643. } else {
  644. h.expandNode(i)
  645. }
  646. } else {
  647. h.checkNode(i)
  648. }
  649. } else {
  650. if (i.clickExpand == true || i.clickExpand == "true") {
  651. h.expandNode(i)
  652. } else {
  653. var g;
  654. g = $("#" + d).parents(".mainCon").find("input[class*=selectbox]");
  655. g.val(i.name);
  656. c.attr("relText", i.name);
  657. c.attr("relValue", i.id);
  658. a.val(i.id);
  659. if (c.attr("editable") == "true" || c.attr("editable") == true) {
  660. c.attr("editValue", g.val());
  661. a.val(g.val())
  662. }
  663. c.focus();
  664. var j = $("#" + d).parents(".mainCon").find("div[class=selectbox-tree]");
  665. j.hide();
  666. j.attr("hasfocus", 0);
  667. try {
  668. c.trigger("change")
  669. } catch (f) {}
  670. }
  671. }
  672. }
  673. function zTreeSelectAddItem(e, c, g, d) {
  674. var b = $.fn.zTree.getZTreeObj(e.find("ul").eq(0).attr("id"));
  675. var a = b.transformToArray(b.getNodes());
  676. for (var f = 0; f < a.length; f++) {
  677. if (a[f].id == c) {
  678. b.addNodes(a[f], {
  679. id: g,
  680. pId: a[f].id,
  681. name: d
  682. })
  683. }
  684. }
  685. }
  686. function zTreeSelectRemoveItem(c, e) {
  687. var b = $.fn.zTree.getZTreeObj(c.find("ul").eq(0).attr("id"));
  688. var a = b.transformToArray(b.getNodes());
  689. for (var d = 0; d < a.length; d++) {
  690. if (a[d].id == e) {
  691. b.removeNode(a[d])
  692. }
  693. }
  694. };