select-table.js 8.3 KB

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