select-table.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. (function (w) {
  2. function SelectTable(options) {
  3. options = $.extend({
  4. dialog: null,
  5. container: null,
  6. input: null,
  7. button: '.submit-btn',
  8. cancel: '.cancel-btn',
  9. table: '.async-table',
  10. multiple: false,
  11. max: 0,
  12. values: [],
  13. lang: {
  14. exceed_max_item: Dcat.lang.exceed_max_item || '已超出最大可选择的数量',
  15. },
  16. }, options);
  17. let self = this;
  18. self.options = options;
  19. self.$input = $(options.input);
  20. self.init();
  21. }
  22. SelectTable.prototype = {
  23. init() {
  24. let self = this,
  25. options = self.options,
  26. values = options.values;
  27. self.labels = {};
  28. for (let i in values) {
  29. self.labels[values[i]['id']] = values[i]['label']
  30. }
  31. // 保存临时选中的值
  32. self.resetSelected();
  33. $(document).on('dialog:shown', options.dialog, function () {
  34. self.$dialog = $(options.dialog);
  35. self.$button = self.$dialog.find(options.button);
  36. self.$cancel = self.$dialog.find(options.cancel);
  37. // 提交按钮
  38. self.$button.on('click', function () {
  39. var selected = self.getSelectedRows();
  40. self.setKeys(selected[1]);
  41. self.render(selected[0]);
  42. self.$dialog.trigger('dialog:close');
  43. });
  44. self.$cancel.on('click', function () {
  45. self.$dialog.trigger('dialog:close');
  46. });
  47. self.bind();
  48. });
  49. self.render(values);
  50. },
  51. bind() {
  52. let self = this, options = self.options;
  53. // 表格加载完成事件
  54. self.$dialog.find(options.table).on('table:loaded', function () {
  55. let checkbox = self.getCheckbox();
  56. if (! options.multiple) {
  57. // 移除全选按钮
  58. $(this).find('.checkbox-grid-header').remove();
  59. }
  60. // 重置已选中数据
  61. self.resetSelected();
  62. checkbox.on('change', function () {
  63. let id = $(this).data('id'),
  64. label = $(this).data('label');
  65. if (this.checked) {
  66. if (! options.multiple) {
  67. self.selected = {};
  68. }
  69. self.selected[id] = {id: id, label: label};
  70. // 多选
  71. if (options.max && (self.getSelectedRows()[0].length > options.max)) {
  72. $(this).prop('checked', false);
  73. delete self.selected[id];
  74. return Dcat.warning(self.options.lang.exceed_max_item);
  75. }
  76. } else {
  77. delete self.selected[id];
  78. }
  79. if (! options.multiple) {
  80. if (this.checked) {
  81. // 单选效果
  82. checkbox.each(function () {
  83. if ($(this).data('id') != id) {
  84. $(this).prop('checked', false);
  85. $(this).parents('tr').css('background-color', '');
  86. }
  87. });
  88. }
  89. }
  90. });
  91. // 选中默认选项
  92. checkbox.each(function () {
  93. let $this = $(this),
  94. current = $this.data('id');
  95. // 保存label字段
  96. self.labels[current] = $this.data('label');
  97. for (let i in self.selected) {
  98. if (current == i) {
  99. $this.prop('checked', true).trigger('change');
  100. continue;
  101. }
  102. }
  103. $this.trigger('change');
  104. });
  105. })
  106. },
  107. // 重置已选中数据
  108. resetSelected() {
  109. let self = this,
  110. keys = self.getKeys();
  111. self.selected = [];
  112. for (let i in keys) {
  113. self.selected[keys[i]] = {id: keys[i], label: self.labels[keys[i]]};
  114. }
  115. },
  116. getCheckbox() {
  117. return this.$dialog.find('.checkbox-grid-column input[type="checkbox"]');
  118. },
  119. getSelectedRows() {
  120. let self = this,
  121. selected = [],
  122. ids = [];
  123. for (let i in self.selected) {
  124. if (! self.selected[i]) {
  125. continue;
  126. }
  127. ids.push(i);
  128. selected.push(self.selected[i])
  129. }
  130. return [selected, ids];
  131. },
  132. render(selected) {
  133. let self = this,
  134. options = self.options,
  135. box = $(options.container),
  136. placeholder = box.find('.default-text'),
  137. option = box.find('.option');
  138. if (! selected || ! selected.length) {
  139. placeholder.removeClass('d-none');
  140. option.addClass('d-none');
  141. if (options.multiple) {
  142. box.addClass('form-control');
  143. }
  144. return;
  145. }
  146. placeholder.addClass('d-none');
  147. option.removeClass('d-none');
  148. if (! options.multiple) {
  149. return renderDefault(selected, self, options);
  150. }
  151. return renderMultiple(selected, self, options);
  152. },
  153. setKeys(keys) {
  154. this.$input.val(keys.length ? keys.join(',') : '');
  155. },
  156. deleteKey(key) {
  157. let val = this.getKeys(),
  158. results = [];
  159. for (let i in val) {
  160. if (val[i] != key) {
  161. results.push(val[i])
  162. }
  163. }
  164. this.setKeys(results)
  165. },
  166. getKeys() {
  167. let val = this.$input.val();
  168. if (! val) return [];
  169. return String(val).split(',');
  170. },
  171. };
  172. // 多选
  173. function renderMultiple(selected, self, options) {
  174. let html = [],
  175. box = $(options.container),
  176. placeholder = box.find('.default-text'),
  177. option = box.find('.option');
  178. if (! box.hasClass('select2')) {
  179. box.addClass('select2 select2-container select2-container--default select2-container--below');
  180. }
  181. box.removeClass('form-control');
  182. for (let i in selected) {
  183. html.push(`<li class="select2-selection__choice" >
  184. ${selected[i]['label']} <span data-id="${selected[i]['id']}" class="select2-selection__choice__remove remove " role="presentation"> ×</span>
  185. </li>`);
  186. }
  187. html.unshift('<span class="select2-selection__clear remove-all">×</span>');
  188. html = `<span class="select2-selection select2-selection--multiple">
  189. <ul class="select2-selection__rendered">${html.join('')}</ul>
  190. </span>`;
  191. var $tags = $(html);
  192. option.html($tags);
  193. $tags.find('.remove').on('click', function () {
  194. var $this = $(this);
  195. self.deleteKey($this.data('id'));
  196. $this.parent().remove();
  197. if (! self.getKeys().length) {
  198. removeAll();
  199. }
  200. });
  201. function removeAll() {
  202. option.html('');
  203. placeholder.removeClass('d-none');
  204. option.addClass('d-none');
  205. box.addClass('form-control');
  206. self.setKeys([]);
  207. }
  208. $tags.find('.remove-all').on('click', removeAll);
  209. }
  210. // 单选
  211. function renderDefault(selected, self, options) {
  212. let box = $(options.container),
  213. placeholder = box.find('.default-text'),
  214. option = box.find('.option');
  215. var remove = $("<div class='pull-right ' style='font-weight:bold;cursor:pointer'>×</div>");
  216. option.text(selected[0]['label']);
  217. option.append(remove);
  218. remove.on('click', function () {
  219. self.setKeys([]);
  220. placeholder.removeClass('d-none');
  221. option.addClass('d-none');
  222. });
  223. }
  224. Dcat.grid.SelectTable = function (opts) {
  225. return new SelectTable(opts)
  226. };
  227. })(window)