select-table.js 8.8 KB

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