userpicker.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. var createUserPicker = function(conf) {
  2. conf = conf ? conf : {};
  3. var defaults = {
  4. modalId: 'userPicker',
  5. multiple: false,
  6. searchUrl: '/mossle-app-lemon/rs/party/searchUser',
  7. treeUrl: '/mossle-app-lemon/rs/party/tree?partyStructTypeId=1'
  8. };
  9. for (var key in defaults) {
  10. if (!conf[key]) {
  11. conf[key] = defaults[key];
  12. }
  13. }
  14. if ($('#' + conf.modalId).length == 0) {
  15. $(document.body).append(
  16. '<div id="' + conf.modalId + '" class="modal fade">'
  17. +' <div class="modal-dialog">'
  18. +' <div class="modal-content">'
  19. +' <div class="modal-header">'
  20. +' <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>'
  21. +' <h3>选择用户</h3>'
  22. +' </div>'
  23. +' <div class="modal-body">'
  24. +' <div class="row">'
  25. +' <div class="col-md-4">'
  26. +' <div style="padding-top:20px;">&nbsp;</div>'
  27. +' <ul id="' + conf.modalId + 'treeMenu" class="ztree"></ul>'
  28. +' </div>'
  29. +' <div class="col-md-8">'
  30. +' <div>'
  31. +' <label for="' + conf.modalId + '_username" style="display:inline" class="">账号:</label>'
  32. +' <input type="text" id="' + conf.modalId + '_username" value="" style="margin-bottom:0px; width:auto; display:inline;" class="form-control">'
  33. +' <button id="' + conf.modalId + '_search" class="btn btn-default">查询</button>'
  34. +' </div>'
  35. +' <div class="panel panel-default" style="max-height:300px;overflow:auto;">'
  36. +' <div class="panel-heading">'
  37. +' <h3 class="panel-title">用户</h3>'
  38. +' </div>'
  39. +' <table id="' + conf.modalId + '_grid" class="table table-hover">'
  40. +' <thead>'
  41. +' <tr>'
  42. +' <th width="10" class="m-table-check">&nbsp;</th>'
  43. +' <th>姓名</th>'
  44. +' </tr>'
  45. +' </thead>'
  46. +' <tbody id="' + conf.modalId + '_body">'
  47. /*
  48. +' <tr>'
  49. +' <td><input id="' + conf.modalId + '_item_1" type="' + (conf.multiple ? 'checkbox' : 'radio') + '" name="selectedItem" class="selectedItem" value="1" title="admin" style="margin-top:0px;"></td>'
  50. +' <td>admin</td>'
  51. +' </tr>'
  52. +' <tr>'
  53. +' <td><input id="' + conf.modalId + '_item_2" type="' + (conf.multiple ? 'checkbox' : 'radio') + '" name="selectedItem" class="selectedItem" value="2" title="user" style="margin-top:0px;"></td>'
  54. +' <td>user</td>'
  55. +' </tr>'
  56. */
  57. +' </tbody>'
  58. +' </table>'
  59. +' </div>'
  60. +' </div>'
  61. +' </div>'
  62. +' </div>'
  63. +' <div class="modal-footer">'
  64. +' <span id="' + conf.modalId + '_result" style="float:left;"></span>'
  65. +' <a id="' + conf.modalId + '_close" href="#" class="btn" data-dismiss="modal">关闭</a>'
  66. +' <a id="' + conf.modalId + '_select" href="#" class="btn btn-primary">选择</a>'
  67. +' </div>'
  68. +' </div>'
  69. +' </div>'
  70. +'</div>');
  71. }
  72. var doSearch = function(username) {
  73. $.ajax({
  74. url: conf.searchUrl,
  75. data: {
  76. username: username
  77. },
  78. success: function(data) {
  79. var html = '';
  80. for (var i = 0; i < data.length; i++) {
  81. var item = data[i];
  82. html +=
  83. '<tr>'
  84. +'<td><input id="' + conf.modalId + '_item_' + i + '" type="' + (conf.multiple ? 'checkbox' : 'radio')
  85. + '" class="selectedItem" name="name" value="'
  86. + item.id + '" title="' + item.displayName + '"></td>'
  87. +'<td><label for="' + conf.modalId + '_item_' + i + '">' + item.displayName + '</label></td>'
  88. +'</tr>'
  89. }
  90. $('#' + conf.modalId + '_body').html(html);
  91. }
  92. });
  93. }
  94. var doSearchChild = function(parentId) {
  95. $.ajax({
  96. url: conf.childUrl,
  97. data: {
  98. parentId: parentId
  99. },
  100. success: function(data) {
  101. var html = '';
  102. for (var i = 0; i < data.length; i++) {
  103. var item = data[i];
  104. html +=
  105. '<tr>'
  106. +'<td><input id="' + conf.modalId + '_item_' + i + '" type="' + (conf.multiple ? 'checkbox' : 'radio')
  107. + '" class="selectedItem" name="name" value="'
  108. + item.id + '" title="' + item.displayName + '"></td>'
  109. +'<td><label for="' + conf.modalId + '_item_' + i + '">' + item.displayName + '</label></td>'
  110. +'</tr>'
  111. }
  112. $('#' + conf.modalId + '_body').html(html);
  113. }
  114. });
  115. }
  116. $(document).delegate('.userPicker .input-group-addon', 'click', function(e) {
  117. var multiple = $(this).parent().data('multiple');
  118. if (multiple) {
  119. conf.multiple = true;
  120. }
  121. var setting = {
  122. async: {
  123. enable: true,
  124. url: conf.treeUrl
  125. },
  126. callback: {
  127. onClick: function(event, treeId, treeNode) {
  128. // console.info(treeNode.id);
  129. doSearchChild(treeNode.id);
  130. }
  131. }
  132. };
  133. var zNodes = [];
  134. try {
  135. $.fn.zTree.init($("#" + conf.modalId + "treeMenu"), setting, zNodes);
  136. } catch(e) {
  137. console.error(e);
  138. }
  139. $('#' + conf.modalId).data('userPicker', $(this).parent());
  140. $('#' + conf.modalId).modal();
  141. // doSearch('');
  142. });
  143. // $(document).delegate('#' + conf.modalId + '_body tr', 'click', function(e) {
  144. // $('input[type=radio].selectedItem').prop('checked', false);
  145. // $(this).find('.selectedItem').prop('checked', true);
  146. // });
  147. $(document).delegate('#' + conf.modalId + '_body .selectedItem', 'click', function(e) {
  148. if (conf.multiple) {
  149. var el = $(this);
  150. if (el.prop('checked')) {
  151. var html = '&nbsp;<span class="label label-default" id="' + $(this).val() + '" title="' + $(this).attr('title') + '">' + $(this).attr('title') + '<i class="glyphicon glyphicon-remove" style="cursor:pointer;"></i></span>';
  152. $('#' + conf.modalId + '_result').append(html);
  153. } else {
  154. $('#' + conf.modalId + '_result #' + el.val()).remove();
  155. }
  156. } else {
  157. var html = '<span class="label label-default" id="' + $(this).val() + '" title="' + $(this).attr('title') + '">' + $(this).attr('title') + '<i class="glyphicon glyphicon-remove" style="cursor:pointer;"></i></span>';
  158. $('#' + conf.modalId + '_result').html(html);
  159. }
  160. });
  161. $(document).delegate('.glyphicon-remove', 'click', function(e) {
  162. var id = $(this).parent().attr('id');
  163. $('#' + conf.modalId + '_item_' + id).prop('checked', false);
  164. $(this).parent().remove();
  165. });
  166. $(document).delegate('#' + conf.modalId + '_search', 'click', function(e) {
  167. doSearch($('#' + conf.modalId + '_username').val());
  168. });
  169. $(document).delegate('#' + conf.modalId + '_username', 'keypress', function(e) {
  170. if (e.which == 13) {
  171. doSearch($('#' + conf.modalId + '_username').val());
  172. }
  173. });
  174. $(document).delegate('#' + conf.modalId + '_select', 'click', function(e) {
  175. $('#' + conf.modalId).modal('hide');
  176. var userPickerElement = $('#' + conf.modalId).data('userPicker');
  177. if (conf.multiple) {
  178. var el = $('#' + conf.modalId + '_result .label');
  179. var ids = [];
  180. var names = [];
  181. el.each(function(index, item) {
  182. ids.push($(item).attr('id'));
  183. names.push($(item).attr('title'));
  184. });
  185. userPickerElement.children('input[type=hidden]').val(ids.join(','));
  186. userPickerElement.children('input[type=text]').val(names.join(','));
  187. } else {
  188. var el = $('#' + conf.modalId + '_result .label');
  189. userPickerElement.children('input[type=hidden]').val(el.attr('id'));
  190. userPickerElement.children('input[type=text]').val(el.attr('title'));
  191. }
  192. });
  193. }