jquery.select.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. (function ($) {
  2. $.selectSuggest = function (target, data, itemSelectFunction) {
  3. var defaulOption = {
  4. suggestMaxHeight: '400px',//弹出框最大高度
  5. itemColor: '#000000',//默认字体颜色
  6. itemBackgroundColor: 'RGB(253,250,250)',//默认背景颜色
  7. itemOverColor: '#ffffff',//选中字体颜色
  8. itemOverBackgroundColor: '#ea0c3c',//选中背景颜色
  9. itemPadding: 5,//item间距
  10. fontSize: 15,//默认字体大小
  11. alwaysShowALL: true //点击input是否展示所有可选项
  12. };
  13. var timer = null;
  14. var maxFontNumber = 0;//最大字数
  15. var currentItem;
  16. var suggestContainerId = target + "-suggest";
  17. var suggestContainerWidth = $('#' + target).innerWidth();
  18. var suggestContainerLeft = $('#' + target).offset().left;
  19. var suggestContainerTop = $('#' + target).offset().top + $('#' + target).outerHeight();
  20. var showClickTextFunction = function () {
  21. $('#' + target).val(this.innerText);
  22. currentItem = null;
  23. $('#' + suggestContainerId).hide();
  24. }
  25. var suggestContainer;
  26. if ($('#' + suggestContainerId)[0]) {
  27. suggestContainer = $('#' + suggestContainerId);
  28. suggestContainer.empty();
  29. } else {
  30. suggestContainer = $('<div></div>'); //创建一个子<div>
  31. }
  32. suggestContainer.attr('id', suggestContainerId);
  33. suggestContainer.attr('tabindex', '0');
  34. suggestContainer.hide();
  35. var _initItems = function (items) {
  36. suggestContainer.empty();
  37. if (items.length >= 5000) {
  38. alert("插叙模糊查询到数据超过5000笔,请重新输入精确查询条件")
  39. return
  40. }
  41. for (var i = 0; i < items.length; i++) {
  42. if (items[i].text.length > maxFontNumber) {
  43. maxFontNumber = items[i].text.length;
  44. }
  45. // if (Math.round(i % 1000) == 0) {
  46. // console.log("被整除了")
  47. // timer && clearTimeout(timer);
  48. // timer = setTimeout(function () {
  49. // console.log("延迟")
  50. // }, 1000);
  51. // }
  52. var suggestItem = $('<div></div>'); //创建一个子<div>
  53. suggestItem.attr('id', items[i].id);
  54. suggestItem.append(items[i].text);
  55. suggestItem.css({
  56. 'padding': defaulOption.itemPadding + 'px',
  57. 'white-space': 'nowrap',
  58. 'cursor': 'pointer',
  59. 'background-color': defaulOption.itemBackgroundColor,
  60. 'color': defaulOption.itemColor
  61. });
  62. suggestItem.bind("mouseover",
  63. function () {
  64. $(this).css({
  65. 'background-color': defaulOption.itemOverBackgroundColor,
  66. 'color': defaulOption.itemOverColor
  67. });
  68. currentItem = $(this);
  69. });
  70. suggestItem.bind("mouseout",
  71. function () {
  72. $(this).css({
  73. 'background-color': defaulOption.itemBackgroundColor,
  74. 'color': defaulOption.itemColor
  75. });
  76. currentItem = null;
  77. });
  78. suggestItem.bind("click", showClickTextFunction);
  79. suggestItem.bind("click", itemSelectFunction);
  80. suggestItem.appendTo(suggestContainer);
  81. suggestContainer.appendTo(document.body);
  82. }
  83. }
  84. var inputChange = function () {
  85. var inputValue = $('#' + target).val();
  86. inputValue = inputValue.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
  87. var matcher = new RegExp(inputValue, "i");
  88. return $.grep(data,
  89. function (value) {
  90. return matcher.test(value.text);
  91. });
  92. }
  93. //这里是控制静态查找
  94. // $('#' + target).bind("keyup",
  95. // function () {
  96. // _initItems(inputChange());
  97. // });
  98. $('#' + target).bind("blur",
  99. function () {
  100. if (!$('#' + suggestContainerId).is(":focus")) {
  101. $('#' + suggestContainerId).hide();
  102. // console.log("啊啊啊啊被点击")
  103. if (currentItem) {
  104. currentItem.trigger("click");
  105. }
  106. currentItem = null;
  107. return;
  108. }
  109. });
  110. $('#' + target).bind("click",
  111. function () {
  112. if (defaulOption.alwaysShowALL) {
  113. // console.log("被点击数据没有变动")
  114. // _initItems(data);
  115. } else {
  116. _initItems(inputChange());
  117. }
  118. $('#' + suggestContainerId).removeAttr("style");
  119. var tempWidth = defaulOption.fontSize * maxFontNumber + 2 * defaulOption.itemPadding + 30;
  120. var containerWidth = Math.max(tempWidth, suggestContainerWidth);
  121. $('#' + suggestContainerId).css({
  122. 'border': '1px solid #ccc',
  123. 'max-height': '400px', //设置高度
  124. 'top': suggestContainerTop,
  125. 'left': suggestContainerLeft,
  126. 'width': containerWidth,
  127. 'position': 'absolute',
  128. 'font-size': defaulOption.fontSize + 'px',
  129. 'font-family': 'Arial',
  130. 'z-index': 99999,
  131. 'background-color': '#FFFFFF',
  132. 'overflow-y': 'auto',
  133. 'overflow-x': 'hidden',
  134. 'white-space': 'nowrap'
  135. });
  136. $('#' + suggestContainerId).show();
  137. });
  138. _initItems(data);
  139. $('#' + suggestContainerId).bind("blur",
  140. function () {
  141. $('#' + suggestContainerId).hide();
  142. });
  143. }
  144. })(jQuery);