select-table.js 8.9 KB

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