select-table.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. return;
  130. }
  131. placeholder.addClass('d-none');
  132. option.removeClass('d-none');
  133. if (! options.multiple) {
  134. return renderDefault(selected, self, options);
  135. }
  136. return renderMultiple(selected, self, options);
  137. },
  138. setKeys(keys) {
  139. this.$input.val(keys.length ? keys.join(',') : '');
  140. },
  141. deleteKey(key) {
  142. let val = this.getKeys(),
  143. results = [];
  144. for (let i in val) {
  145. if (val[i] != key) {
  146. results.push(val[i])
  147. }
  148. }
  149. this.setKeys(results)
  150. },
  151. getKeys() {
  152. let val = this.$input.val();
  153. if (! val) return [];
  154. return String(val).split(',');
  155. },
  156. };
  157. // 多选
  158. function renderMultiple(selected, self, options) {
  159. let html = [],
  160. box = $(options.container),
  161. placeholder = box.find('.default-text'),
  162. option = box.find('.option');
  163. if (! box.hasClass('select2')) {
  164. box.addClass('select2 select2-container select2-container--default select2-container--below');
  165. }
  166. box.removeClass('form-control');
  167. for (let i in selected) {
  168. html.push(`<li class="select2-selection__choice" >
  169. ${selected[i]['label']} <span data-id="${selected[i]['id']}" class="select2-selection__choice__remove remove " role="presentation"> ×</span>
  170. </li>`);
  171. }
  172. html.unshift('<span class="select2-selection__clear remove-all">×</span>');
  173. html = `<span class="select2-selection select2-selection--multiple">
  174. <ul class="select2-selection__rendered">${html.join('')}</ul>
  175. </span>`;
  176. var $tags = $(html);
  177. option.html($tags);
  178. $tags.find('.remove').on('click', function () {
  179. var $this = $(this);
  180. self.deleteKey($this.data('id'));
  181. $this.parent().remove();
  182. if (! self.getKeys().length) {
  183. removeAll();
  184. }
  185. });
  186. function removeAll() {
  187. option.html('');
  188. placeholder.removeClass('d-none');
  189. option.addClass('d-none');
  190. box.addClass('form-control');
  191. self.setKeys([]);
  192. }
  193. $tags.find('.remove-all').on('click', removeAll);
  194. }
  195. // 单选
  196. function renderDefault(selected, self, options) {
  197. let box = $(options.container),
  198. placeholder = box.find('.default-text'),
  199. option = box.find('.option');
  200. var remove = $("<div class='pull-right ' style='font-weight:bold;cursor:pointer'>×</div>");
  201. option.text(selected[0]['label']);
  202. option.append(remove);
  203. remove.on('click', function () {
  204. self.setKeys([]);
  205. placeholder.removeClass('d-none');
  206. option.addClass('d-none');
  207. });
  208. }
  209. Dcat.grid.SelectTable = function (opts) {
  210. return new SelectTable(opts)
  211. };
  212. })(window)