select-table.js 7.9 KB

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