select2tree.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. (function ($) {
  2. $.fn.select2tree = function (options) {
  3. var defaults = {
  4. language: "zh-CN",
  5. minimumResultsForSearch: -1
  6. /*theme: "bootstrap"*/
  7. };
  8. var opts = $.extend(defaults, options);
  9. opts.templateResult = function (data, container) {
  10. if (data.element) {
  11. //insert span element and add 'parent' property
  12. var $wrapper = $("<span style=\"margin-right:6px;\"></span><span>" + data.text + "</span>");
  13. var $element = $(data.element);
  14. $(container).attr("val", $element.val());
  15. if ($element.attr("parent")) {
  16. $(container).attr("parent", $element.attr("parent"));
  17. }
  18. return $wrapper;
  19. } else {
  20. return data.text;
  21. }
  22. };
  23. var $that = $(this);
  24. $that.select2(opts).on("select2:open", function () {
  25. open();
  26. });
  27. function moveOption(parentId, index) {
  28. index = index || 0;
  29. if (parentId !== undefined) {
  30. $(".select2-results__options li[parent='" + parentId + "']").insertAfter(".select2-results__options li[val='" + parentId + "']");
  31. $(".select2-results__options li[parent='" + parentId + "']").each(function () {
  32. $(this).attr('level', index);
  33. moveOption($(this).attr("val"), index + 1);
  34. });
  35. } else {
  36. $(".select2-results__options li:not([parent])").appendTo(".select2-results__options ul");
  37. $(".select2-results__options li:not([parent])").each(function () {
  38. $(this).attr('level', index);
  39. moveOption($(this).attr("val"), index + 1);
  40. });
  41. }
  42. }
  43. //deal switch action
  44. function switchAction(parentId, open, isAll) {
  45. if (!parentId || $(".select2-results__options li[parent='" + parentId + "']").length <= 0) {
  46. return;
  47. }
  48. if (isAll && $(".select2-results__options li[val='" + parentId + "'][level='1']").length > 0) {
  49. return;
  50. }
  51. if (open) {
  52. $(".select2-results__options li[val='" + parentId + "'] span[class]:eq(0)").removeClass("fa-plus-square").addClass("fa-minus-square");
  53. } else {
  54. $(".select2-results__options li[val='" + parentId + "'] span[class]:eq(0)").addClass("fa-plus-square").removeClass("fa-minus-square");
  55. }
  56. $(".select2-results__options li[parent='" + parentId + "']").each(function () {
  57. var $that = $(this);
  58. open ? $that.slideDown() : $that.slideUp();
  59. if (!open) {
  60. switchAction($that.attr("val"), open, isAll);
  61. }
  62. });
  63. }
  64. function open() {
  65. setTimeout(function () {
  66. moveOption();
  67. $(".select2-results__options li").each(function () {
  68. var $this = $(this);
  69. $this.find("span:eq(0)").addClass('far fa-square');
  70. if ($this.attr("parent")) {
  71. $(this).siblings("li[val='" + $this.attr("parent") + "']").find("span:eq(0)")
  72. .removeClass("fa-square").addClass("fa-plus-square switch").css({ "cursor": "pointer" });
  73. }
  74. if (!$this.attr("style")) {
  75. var level = Number($this.attr("level")) || 0;
  76. var paddingLeft = (level + 1) * 7 + (Number($this.css("paddingLeft").replace("px", "")) || 0);
  77. $("li[parent='" + $this.attr("parent") + "']").css("display", "none").css("padding-left", paddingLeft + "px");
  78. }
  79. });
  80. $(".select2-results__options li[level='0']").each(function () {
  81. var $this = $(this);
  82. var root = $this.attr("val");
  83. switchAction(root, true, true);
  84. });
  85. openCurrent();
  86. //override mousedown for collapse/expand
  87. $(".switch").off("mousedown").on("mousedown", function (event) {
  88. var isOpen = $(this).hasClass("fa-plus-square");
  89. switchAction($(this).parent().attr("val"), isOpen);
  90. if (isOpen) {
  91. openCurrent();
  92. }
  93. event.stopPropagation();
  94. event.preventDefault();
  95. });
  96. //override mouseup to nothing
  97. $(".switch").off("mouseup").on("mouseup", function () {
  98. return false;
  99. });
  100. }, 100);
  101. }
  102. function openCurrent() {
  103. var id = $that.val();
  104. var $cur = $(".select2-results__options li[val='" + id + "']").find("span:eq(0)");
  105. if (id && $cur.length) {
  106. if ($cur.hasClass('fa-square')) {
  107. $cur.removeClass("fa-square").addClass("fa-check-square");
  108. }
  109. openParent($cur.parent().attr("parent"), true);
  110. }
  111. }
  112. function openParent(parentId) {
  113. var $this = $(".select2-results__options li[val='" + parentId + "']");
  114. if (!parentId || $this.length <= 0) {
  115. return;
  116. }
  117. $this.find("span[class]:eq(0)").removeClass("fa-plus-square").addClass("fa-minus-square");
  118. $(".select2-results__options li[parent='" + parentId + "']").each(function () {
  119. $(this).slideDown();
  120. });
  121. openParent($this.attr("parent"));
  122. }
  123. };
  124. })(jQuery);