table.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. define(['jquery'], function($) {
  2. $(document).on('change', 'table tfoot [type="checkbox"]', function(e) {
  3. e && e.preventDefault();
  4. var $table = $(e.target).closest('table'),
  5. $checked = $(e.target).is(':checked');
  6. $('thead [type="checkbox"]', $table).prop('checked', $checked);
  7. $('.page-table-header input[type="checkbox"]').prop('checked', $checked);
  8. $('tbody [type="checkbox"]', $table).prop('checked', $checked)
  9. });
  10. $(document).on('change', 'table thead [type="checkbox"]', function(e) {
  11. e && e.preventDefault();
  12. var $table = $(e.target).closest('table'),
  13. $checked = $(e.target).is(':checked');
  14. $('.page-table-header input[type="checkbox"]').prop('checked', $checked);
  15. $('tbody [type="checkbox"]:not(:disabled)', $table).prop('checked', $checked);
  16. $('tfoot [type="checkbox"]', $table).prop('checked', $checked)
  17. });
  18. $(document).on('change', '.page-table-header input[type="checkbox"]', function(e) {
  19. e && e.preventDefault();
  20. var $table = $(e.target).parent().next('table'),
  21. $checked = $(e.target).is(':checked');
  22. $('thead [type="checkbox"]', $table).prop('checked', $checked);
  23. $('tfoot [type="checkbox"]', $table).prop('checked', $checked);
  24. $('tbody [type="checkbox"]', $table).prop('checked', $checked)
  25. });
  26. var table_search = $('form.table-search');
  27. if (table_search.length > 0) {
  28. $('.daterange-time').addClass('btn-sm')
  29. }
  30. var div = $(".table-responsive"),
  31. checkboxs = $('tbody tr td:first-child [type="checkbox"]', div),
  32. batch = $('[data-toggle^="batch"]'),
  33. trigger = $('[data-trigger="batch"]');
  34. if (div.length > 0) {
  35. $('[data-toggle="ajaxDisplayorder"]').click(function() {
  36. var $this = $(this),
  37. href = $this.data("href"),
  38. html = $this.val() || $this.html(),
  39. buttontype = $this.val() ? 'input' : 'button';
  40. var postdata = {},
  41. selecteds = all_selects();
  42. postdata.displayorder = (function() {
  43. var displayorders = [];
  44. $.each(selecteds, function(i) {
  45. displayorders.push({
  46. id: this,
  47. displayorder: $(":input[name='displayorder[" + this + "]']").val()
  48. })
  49. });
  50. return displayorders
  51. })();
  52. $this.attr('disabled', true), buttontype == 'button' ? $this.html('<i class="fa fa-spinner fa-spin"></i> ' + tip.lang.processing) : $this.val(tip.lang.processing);
  53. $.post(href, postdata).done(function(data) {
  54. data = eval("(" + data + ")");
  55. if (data.status == 1) {
  56. tip.msgbox.suc(data.result.message || tip.lang.success);
  57. $this.removeAttr('disabled'), buttontype == 'button' ? $this.html(html) : $this.val(html)
  58. } else {
  59. buttontype == 'button' ? $this.html(html) : $this.val(html);
  60. $this.removeAttr('disabled'), tip.msgbox.err(data.result.message || tip.lang.error)
  61. }
  62. }).fail(function() {
  63. $this.removeAttr('disabled'), buttontype == 'button' ? $this.html(html) : $this.val(html), tip.msgbox.err(tip.lang.exception)
  64. })
  65. });
  66. checkboxs.click(function() {
  67. var $this = $(this);
  68. if ($this[0].checked) {
  69. $this.closest('tr').find('td').css('background', '#f7fbff')
  70. } else {
  71. $this.closest('tr').find('td').css('background', 'none')
  72. }
  73. });
  74. var all_selects = function() {
  75. var vals = checkboxs.map(function() {
  76. return $(this).val()
  77. }).get();
  78. return vals
  79. };
  80. var get_selecteds = function() {
  81. var selected_checkboxs = $('tbody tr td:first-child [type="checkbox"]:checked', div);
  82. selecteds = selected_checkboxs.map(function() {
  83. return $(this).val()
  84. }).get();
  85. return selecteds
  86. };
  87. var selecteds = get_selecteds();
  88. if (selecteds.length <= 0) {
  89. batch.attr("disabled", "disabled")
  90. }
  91. batch.on("click", function() {
  92. var $this = $(this),
  93. href = $this.data("href"),
  94. html = $this.val() || $this.html(),
  95. buttontype = $this.val() ? 'input' : 'button';
  96. if ($this.data('toggle') == 'batch-level' || $this.data('toggle') == 'batch-group') {
  97. return
  98. }
  99. $this.attr("disabled", "disabled");
  100. var chks = $('tbody tr td:first-child [type="checkbox"]:checked', div);
  101. var selecteds = get_selecteds();
  102. var submit = function() {
  103. buttontype == 'button' ? $this.html('<i class="fa fa-spinner fa-spin"></i> ' + tip.lang.processing) : $this.val(tip.lang.processing);
  104. $.post(href, {
  105. ids: selecteds
  106. }).done(function(data) {
  107. data = eval("(" + data + ")");
  108. if (data.status == 1) {
  109. if ($this.data('toggle') == 'batch-remove') {
  110. var deferred = $.Deferred(),
  111. removeHandler = function(def) {
  112. var c = 0;
  113. return chks.parents("tr").fadeOut(function() {
  114. $(this).remove(), c++, chks.length == c && def.resolve()
  115. }), def
  116. };
  117. $.when(removeHandler(deferred)).done(function() {
  118. batch.attr("disabled", 0 == $('tbody tr td:first-child [type="checkbox"]:checked', div).length), 0 == $("table tbody tr", div).length && window.location.reload()
  119. })
  120. } else {
  121. batch.attr("disabled", 0 == $('table tbody tr td:first-child [type="checkbox"]:checked', div).length);
  122. tip.msgbox.suc(data.result.message || tip.lang.success, data.result.url)
  123. }
  124. buttontype == 'button' ? $this.html(html) : $this.val(html)
  125. } else {
  126. buttontype == 'button' ? $this.html(html) : $this.val(html);
  127. tip.msgbox.err(data.result.message || tip.lang.error)
  128. }
  129. }).fail(function() {
  130. buttontype == 'button' ? $this.html(html) : $this.val(html), tip.msgbox.err(tip.lang.exception)
  131. })
  132. };
  133. if ($this.data('confirm')) {
  134. tip.confirm($this.data('confirm'), submit, function() {
  135. $this.removeAttr("disabled", "disabled")
  136. })
  137. } else {
  138. submit()
  139. }
  140. }), trigger.on("click", function() {
  141. var ids = all_selects();
  142. $(this).data("set", {
  143. ids: ids.join(',')
  144. })
  145. }), $(document).on("change", '.page-table-header input[type="checkbox"]', function(e) {
  146. e && e.preventDefault();
  147. var t = $(e.target).parent().next("table"),
  148. checked = $(e.target).is(":checked");
  149. $('.page-table-header input[type="checkbox"]', t).prop("checked", checked), batch.add(trigger).attr("disabled", !checked);
  150. $('tbody tr td:first-child [type="checkbox"]', t).each(function() {
  151. var $this = $(this);
  152. if (checked) {
  153. $this.closest('tr').find('td').css('background', '#f7fbff')
  154. } else {
  155. $this.closest('tr').find('td').css('background', 'none')
  156. }
  157. })
  158. }), $(document).on("change", '.table-responsive tfoot td:first [type="checkbox"]', function(e) {
  159. e && e.preventDefault();
  160. var t = $(e.target).closest("table"),
  161. checked = $(e.target).is(":checked");
  162. $('.page-table-header input[type="checkbox"]', t).prop("checked", checked), batch.add(trigger).attr("disabled", !checked);
  163. $('tbody tr td:first-child [type="checkbox"]', t).each(function() {
  164. var $this = $(this);
  165. if (checked) {
  166. $this.closest('tr').find('td').css('background', '#f7fbff')
  167. } else {
  168. $this.closest('tr').find('td').css('background', 'none')
  169. }
  170. })
  171. }), $(document).on("change", '.table-responsive thead th:first [type="checkbox"]', function(e) {
  172. e && e.preventDefault();
  173. var t = $(e.target).closest("table"),
  174. checked = $(e.target).is(":checked");
  175. $('.page-table-header input[type="checkbox"]', t).prop("checked", checked), batch.add(trigger).attr("disabled", !checked);
  176. $('tbody tr td:first-child [type="checkbox"]', t).each(function() {
  177. var $this = $(this);
  178. if (checked) {
  179. $this.closest('tr').find('td').css('background', '#f7fbff')
  180. } else {
  181. $this.closest('tr').find('td').css('background', 'none')
  182. }
  183. })
  184. }), $(document).on("change", '.table-responsive tbody td:first-child [type="checkbox"]', function(e) {
  185. e && e.preventDefault();
  186. var t = $(e.target).closest("table"),
  187. checked = $(e.target).is(":checked"),
  188. chk = $('tbody tr td:first-child [type="checkbox"]:checked', t);
  189. $('.page-table-header input[type="checkbox"]').prop("checked", checked && chk.length == checkboxs.length);
  190. $('tfoot td:first [type="checkbox"]', t).prop("checked", checked && chk.length == checkboxs.length);
  191. $('thead th:first [type="checkbox"]', t).prop("checked", checked && chk.length == checkboxs.length);
  192. if (chk.length > 0) {
  193. batch.add(trigger).removeAttr("disabled")
  194. } else {
  195. batch.add(trigger).attr("disabled", "disabled")
  196. }
  197. }), $(document).on("click", '.table-responsive tbody [data-toggle="ajaxRemove"]', function(e) {
  198. e.preventDefault();
  199. var obj = $(this),
  200. url = obj.attr('href') || obj.data('href') || obj.data('url'),
  201. confirm = obj.data('msg') || obj.data('confirm');
  202. var submit = function() {
  203. obj.html('<i class="fa fa-spinner fa-spin"></i> ' + tip.lang.processing);
  204. $.post(url).done(function(data) {
  205. data = eval("(" + data + ")");
  206. if (data.status == 1) {
  207. var tr = obj.parents("tr");
  208. tr.css({
  209. "background-color": "#dff0d8"
  210. }).find("td").css({
  211. "background-color": "#dff0d8"
  212. }), tr.fadeOut(function() {
  213. tr.remove(), 0 == $(obj.closest('tbody')).find('tr').length && window.location.reload()
  214. })
  215. } else {
  216. obj.button('reset'), tip.msgbox.err(data.result.message || tip.lang.error, data.result.url)
  217. }
  218. }).fail(function() {
  219. obj.button('reset');
  220. tip.msgbox.err(tip.lang.exception)
  221. })
  222. };
  223. if (confirm) {
  224. tip.confirm(confirm, submit, function() {
  225. obj.removeAttr("disabled", "disabled")
  226. })
  227. } else {
  228. submit()
  229. }
  230. })
  231. } else {
  232. batch.attr("disabled", "disabled")
  233. }
  234. }), $(document).on("click.dropdown-menu", ".dropdown-select > li > a", function(e) {
  235. e.preventDefault();
  236. var target = $(e.target);
  237. if (!target.is('a')) {
  238. target = target.closest('a')
  239. }
  240. var menu = target.closest('.dropdown-menu'),
  241. label = menu.parent().find('.dropdown-label');
  242. var input = target.find('input'),
  243. checked = input.is(':checked');
  244. if (!input.is(':disabled') && input.attr('type') == 'radio' && checked) {
  245. menu.find('li').removeClass('active')
  246. }
  247. target.parent().removeClass('active');
  248. if (!checked) {
  249. target.parent().addClass('active')
  250. }
  251. input.prop('checked', !input.prop('checked'));
  252. var textchange = label.data('change') == 'true' || label.data('change') === undefined;
  253. if (textchange) {
  254. var checkedinput = menu.find('li > a >input:checked');
  255. if (checkedinput.length > 0) {
  256. var texts = [];
  257. checkedinput.each(function() {
  258. var text = $(this).parent().text();
  259. if (text) {
  260. texts.push($.trim(text))
  261. }
  262. });
  263. if (texts.length < 6) {
  264. texts = texts.join(", ")
  265. } else {
  266. texts = " 选中" + texts.length + "项"
  267. }
  268. label.html(texts)
  269. } else {
  270. label.html(label.data('placeholder'))
  271. }
  272. }
  273. input.trigger('change', [input.val()]);
  274. var change = menu.data('change') || '';
  275. if (change == 'submit') {
  276. menu.closest('form').submit()
  277. }
  278. });