require-table.js 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table', 'bootstrap-table-lang', 'bootstrap-table-export', 'bootstrap-table-commonsearch', 'bootstrap-table-template', 'bootstrap-table-jumpto', 'bootstrap-table-fixed-columns'], function ($, undefined, Moment) {
  2. var Table = {
  3. list: {},
  4. // Bootstrap-table 基础配置
  5. defaults: {
  6. url: '',
  7. sidePagination: 'server',
  8. method: 'get', //请求方法
  9. toolbar: ".toolbar", //工具栏
  10. search: true, //是否启用快速搜索
  11. cache: false,
  12. commonSearch: true, //是否启用通用搜索
  13. searchFormVisible: false, //是否始终显示搜索表单
  14. titleForm: '', //为空则不显示标题,不定义默认显示:普通搜索
  15. idTable: 'commonTable',
  16. showExport: true,
  17. exportDataType: "auto",
  18. exportTypes: ['json', 'xml', 'csv', 'txt', 'doc', 'excel'],
  19. exportOptions: {
  20. fileName: 'export_' + Moment().format("YYYY-MM-DD"),
  21. preventInjection: false,
  22. mso: {
  23. onMsoNumberFormat: function (cell, row, col) {
  24. return !isNaN($(cell).text()) ? '\\@' : '';
  25. },
  26. },
  27. ignoreColumn: [0, 'operate'] //默认不导出第一列(checkbox)与操作(operate)列
  28. },
  29. pageSize: Config.pagesize || localStorage.getItem("pagesize") || 10,
  30. pageList: [10, 15, 20, 25, 50, 'All'],
  31. pagination: true,
  32. clickToSelect: true, //是否启用点击选中
  33. dblClickToEdit: true, //是否启用双击编辑
  34. singleSelect: false, //是否启用单选
  35. showRefresh: false,
  36. showJumpto: true,
  37. locale: Config.language == 'zh-cn' ? 'zh-CN' : 'en-US',
  38. showToggle: true,
  39. showColumns: true,
  40. pk: 'id',
  41. sortName: 'id',
  42. sortOrder: 'desc',
  43. paginationFirstText: __("First"),
  44. paginationPreText: __("Previous"),
  45. paginationNextText: __("Next"),
  46. paginationLastText: __("Last"),
  47. cardView: false, //卡片视图
  48. iosCardView: true, //ios卡片视图
  49. checkOnInit: true, //是否在初始化时判断
  50. escape: true, //是否对内容进行转义
  51. fixDropdownPosition: true, //是否修复下拉的定位
  52. selectedIds: [],
  53. selectedData: [],
  54. extend: {
  55. index_url: '',
  56. add_url: '',
  57. edit_url: '',
  58. del_url: '',
  59. import_url: '',
  60. multi_url: '',
  61. dragsort_url: 'ajax/weigh',
  62. }
  63. },
  64. // Bootstrap-table 列配置
  65. columnDefaults: {
  66. align: 'center',
  67. valign: 'middle',
  68. },
  69. config: {
  70. checkboxtd: 'tbody>tr>td.bs-checkbox',
  71. toolbar: '.toolbar',
  72. refreshbtn: '.btn-refresh',
  73. addbtn: '.btn-add',
  74. editbtn: '.btn-edit',
  75. delbtn: '.btn-del',
  76. importbtn: '.btn-import',
  77. multibtn: '.btn-multi',
  78. disabledbtn: '.btn-disabled',
  79. editonebtn: '.btn-editone',
  80. restoreonebtn: '.btn-restoreone',
  81. destroyonebtn: '.btn-destroyone',
  82. restoreallbtn: '.btn-restoreall',
  83. destroyallbtn: '.btn-destroyall',
  84. dragsortfield: 'weigh',
  85. printingbtn: '.btn-printing',
  86. reportsbtn: '.btn-reports',
  87. cousebtn: '.btn-couse',
  88. },
  89. button: {
  90. edit: {
  91. name: 'edit',
  92. icon: 'fa fa-pencil',
  93. title: __('Edit'),
  94. extend: 'data-toggle="tooltip"',
  95. classname: 'btn btn-xs btn-success btn-editone'
  96. },
  97. del: {
  98. name: 'del',
  99. icon: 'fa fa-trash',
  100. title: __('Del'),
  101. extend: 'data-toggle="tooltip"',
  102. classname: 'btn btn-xs btn-danger btn-delone'
  103. },
  104. dragsort: {
  105. name: 'dragsort',
  106. icon: 'fa fa-arrows',
  107. title: __('Drag to sort'),
  108. extend: 'data-toggle="tooltip"',
  109. classname: 'btn btn-xs btn-primary btn-dragsort'
  110. }
  111. },
  112. api: {
  113. init: function (defaults, columnDefaults, locales) {
  114. defaults = defaults ? defaults : {};
  115. columnDefaults = columnDefaults ? columnDefaults : {};
  116. locales = locales ? locales : {};
  117. $.fn.bootstrapTable.Constructor.prototype.getSelectItem = function () {
  118. return this.$selectItem;
  119. };
  120. var _onPageListChange = $.fn.bootstrapTable.Constructor.prototype.onPageListChange;
  121. $.fn.bootstrapTable.Constructor.prototype.onPageListChange = function () {
  122. _onPageListChange.apply(this, Array.prototype.slice.apply(arguments));
  123. localStorage.setItem('pagesize', this.options.pageSize);
  124. return false;
  125. };
  126. // 写入bootstrap-table默认配置
  127. $.extend(true, $.fn.bootstrapTable.defaults, Table.defaults, defaults);
  128. // 写入bootstrap-table column配置
  129. $.extend($.fn.bootstrapTable.columnDefaults, Table.columnDefaults, columnDefaults);
  130. // 写入bootstrap-table locale配置
  131. $.extend($.fn.bootstrapTable.locales[Table.defaults.locale], {
  132. formatCommonSearch: function () {
  133. return __('Common search');
  134. },
  135. formatCommonSubmitButton: function () {
  136. return __('Submit');
  137. },
  138. formatCommonResetButton: function () {
  139. return __('Reset');
  140. },
  141. formatCommonCloseButton: function () {
  142. return __('Close');
  143. },
  144. formatCommonChoose: function () {
  145. return __('Choose');
  146. },
  147. formatJumpto: function () {
  148. return __('Go');
  149. }
  150. }, locales);
  151. // 如果是iOS设备则判断是否启用卡片视图
  152. if ($.fn.bootstrapTable.defaults.iosCardView && navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
  153. Table.defaults.cardView = true;
  154. $.fn.bootstrapTable.defaults.cardView = true;
  155. }
  156. if (typeof defaults.exportTypes != 'undefined') {
  157. $.fn.bootstrapTable.defaults.exportTypes = defaults.exportTypes;
  158. }
  159. },
  160. // 绑定事件
  161. bindevent: function (table) {
  162. //Bootstrap-table的父元素,包含table,toolbar,pagnation
  163. var parenttable = table.closest('.bootstrap-table');
  164. //Bootstrap-table配置
  165. var options = table.bootstrapTable('getOptions');
  166. //Bootstrap操作区
  167. var toolbar = $(options.toolbar, parenttable);
  168. //跨页提示按钮
  169. var tipsBtn = $(".btn-selected-tips", parenttable);
  170. if (tipsBtn.length === 0) {
  171. tipsBtn = $('<a href="javascript:" class="btn btn-warning-light btn-selected-tips hide" data-animation="false" data-toggle="tooltip" data-title="' + __("Click to uncheck all") + '"><i class="fa fa-info-circle"></i> ' + __("Multiple selection mode: %s checked", "<b>0</b>") + '</a>').appendTo(toolbar);
  172. }
  173. //点击提示按钮
  174. tipsBtn.off("click").on("click", function (e) {
  175. table.trigger("uncheckbox");
  176. table.bootstrapTable("refresh");
  177. });
  178. //当刷新表格时
  179. table.on('uncheckbox', function (status, res, e) {
  180. options.selectedIds = [];
  181. options.selectedData = [];
  182. tipsBtn.tooltip('hide');
  183. tipsBtn.addClass('hide');
  184. });
  185. //表格加载出错时
  186. table.on('load-error.bs.table', function (status, res, e) {
  187. if (e.status === 0) {
  188. return;
  189. }
  190. Toastr.error(__('Unknown data format'));
  191. });
  192. //当加载数据成功时
  193. table.on('load-success.bs.table', function (e, data) {
  194. if (typeof data.rows === 'undefined' && typeof data.code != 'undefined') {
  195. Toastr.error(data.msg);
  196. }
  197. });
  198. //当刷新表格时
  199. table.on('refresh.bs.table', function (e, settings, data) {
  200. $(Table.config.refreshbtn, toolbar).find(".fa").addClass("fa-spin");
  201. });
  202. //当执行搜索时
  203. table.on('search.bs.table common-search.bs.table', function (e, settings, data) {
  204. table.trigger("uncheckbox");
  205. });
  206. if (options.dblClickToEdit) {
  207. //当双击单元格时
  208. table.on('dbl-click-row.bs.table', function (e, row, element, field) {
  209. $(Table.config.editonebtn, element).trigger("click");
  210. });
  211. }
  212. //渲染内容前
  213. table.on('pre-body.bs.table', function (e, data) {
  214. if (options.maintainSelected) {
  215. $.each(data, function (i, row) {
  216. row[options.stateField] = $.inArray(row[options.pk], options.selectedIds) > -1;
  217. });
  218. }
  219. });
  220. //当内容渲染完成后
  221. table.on('post-body.bs.table', function (e, data) {
  222. $(Table.config.refreshbtn, toolbar).find(".fa").removeClass("fa-spin");
  223. if ($(Table.config.checkboxtd + ":first", table).find("input[type='checkbox'][data-index]").length > 0) {
  224. // 拖拽选择,需要重新绑定事件
  225. require(['drag', 'drop'], function () {
  226. var checkboxtd = $(Table.config.checkboxtd, table);
  227. checkboxtd.drag("start", function (ev, dd) {
  228. return $('<div class="selection" />').css('opacity', .65).appendTo(document.body);
  229. }).drag(function (ev, dd) {
  230. $(dd.proxy).css({
  231. top: Math.min(ev.pageY, dd.startY),
  232. left: Math.min(ev.pageX, dd.startX),
  233. height: Math.abs(ev.pageY - dd.startY),
  234. width: Math.abs(ev.pageX - dd.startX)
  235. });
  236. }).drag("end", function (ev, dd) {
  237. $(dd.proxy).remove();
  238. });
  239. checkboxtd.drop("start", function () {
  240. Table.api.toggleattr(this);
  241. }).drop(function () {
  242. // Table.api.toggleattr(this);
  243. }).drop("end", function (e) {
  244. var that = this;
  245. setTimeout(function () {
  246. if (e.type === 'mousemove') {
  247. Table.api.toggleattr(that);
  248. }
  249. }, 0);
  250. });
  251. $.drop({
  252. multi: true
  253. });
  254. });
  255. }
  256. });
  257. var exportDataType = options.exportDataType;
  258. // 处理选中筛选框后按钮的状态统一变更
  259. table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table post-body.bs.table', function (e) {
  260. var allIds = [];
  261. $.each(table.bootstrapTable("getData"), function (i, item) {
  262. allIds.push(typeof item[options.pk] != 'undefined' ? item[options.pk] : '');
  263. });
  264. var selectedIds = Table.api.selectedids(table, true),
  265. selectedData = Table.api.selecteddata(table, true);
  266. //开启分页checkbox分页记忆
  267. if (options.maintainSelected) {
  268. options.selectedIds = options.selectedIds.filter(function (element, index, self) {
  269. return $.inArray(element, allIds) === -1;
  270. }).concat(selectedIds);
  271. options.selectedData = options.selectedData.filter(function (element, index, self) {
  272. return $.inArray(element[options.pk], allIds) === -1;
  273. }).concat(selectedData);
  274. if (options.selectedIds.length > selectedIds.length) {
  275. $("b", tipsBtn).text(options.selectedIds.length);
  276. tipsBtn.removeClass('hide');
  277. } else {
  278. tipsBtn.addClass('hide');
  279. }
  280. } else {
  281. options.selectedIds = selectedIds;
  282. options.selectedData = selectedData;
  283. }
  284. //如果导出类型为auto时则自动判断
  285. if (exportDataType === 'auto') {
  286. options.exportDataType = selectedIds.length > 0 ? 'selected' : 'all';
  287. }
  288. $(Table.config.disabledbtn, toolbar).toggleClass('disabled', !options.selectedIds.length);
  289. });
  290. // 绑定TAB事件
  291. $('.panel-heading [data-field] a[data-toggle="tab"]', table.closest(".panel-intro")).on('shown.bs.tab', function (e) {
  292. var field = $(this).closest("[data-field]").data("field");
  293. var value = $(this).data("value");
  294. var object = $("[name='" + field + "']", table.closest(".bootstrap-table").find(".commonsearch-table"));
  295. if (object.prop('tagName') == "SELECT") {
  296. $("option[value='" + value + "']", object).prop("selected", true);
  297. } else {
  298. object.val(value);
  299. }
  300. table.trigger("uncheckbox");
  301. table.bootstrapTable('refresh', {pageNumber: 1});
  302. return false;
  303. });
  304. // 修复重置事件
  305. $("form", table.closest(".bootstrap-table").find(".commonsearch-table")).on('reset', function () {
  306. setTimeout(function () {
  307. // $('.panel-heading [data-field] li.active a[data-toggle="tab"]').trigger('shown.bs.tab');
  308. }, 0);
  309. $('.panel-heading [data-field] li', table.closest(".panel-intro")).removeClass('active');
  310. $('.panel-heading [data-field] li:first', table.closest(".panel-intro")).addClass('active');
  311. });
  312. // 刷新按钮事件
  313. toolbar.on('click', Table.config.refreshbtn, function () {
  314. table.bootstrapTable('refresh');
  315. });
  316. // 添加按钮事件
  317. toolbar.on('click', Table.config.addbtn, function () {
  318. var ids = Table.api.selectedids(table);
  319. var url = options.extend.add_url;
  320. if (url.indexOf("{ids}") !== -1) {
  321. url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
  322. }
  323. Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('Add'), $(this).data() || {});
  324. });
  325. // 导入按钮事件
  326. if ($(Table.config.importbtn, toolbar).length > 0) {
  327. require(['upload'], function (Upload) {
  328. Upload.api.upload($(Table.config.importbtn, toolbar), function (data, ret) {
  329. Fast.api.ajax({
  330. url: options.extend.import_url,
  331. data: {file: data.url},
  332. }, function (data, ret) {
  333. table.trigger("uncheckbox");
  334. table.bootstrapTable('refresh');
  335. });
  336. });
  337. });
  338. }
  339. // 批量编辑按钮事件
  340. toolbar.on('click', Table.config.editbtn, function () {
  341. var that = this;
  342. var ids = Table.api.selectedids(table);
  343. if (ids.length > 10) {
  344. return;
  345. }
  346. var title = $(that).data('title') || $(that).attr("title") || __('Edit');
  347. var data = $(that).data() || {};
  348. delete data.title;
  349. //循环弹出多个编辑框
  350. $.each(Table.api.selecteddata(table), function (index, row) {
  351. var url = options.extend.edit_url;
  352. row = $.extend({}, row ? row : {}, {ids: row[options.pk]});
  353. url = Table.api.replaceurl(url, row, table);
  354. Fast.api.open(url, typeof title === 'function' ? title.call(table, row) : title, data);
  355. });
  356. });
  357. // 批量打印按钮事件
  358. toolbar.on('click', Table.config.printingbtn, function () {
  359. var ids = Table.api.selectedids(table);
  360. if (ids.length > 5) {
  361. layer.confirm('最多选中五个');
  362. return;
  363. }
  364. var data = ids.toString();
  365. var url = options.extend.printing_url+'?ids='+data;
  366. Fast.api.open(url,'批量打印',{area:["100%","100%"]});
  367. });
  368. //批量导出
  369. toolbar.on('click', Table.config.reportsbtn, function () {
  370. var ids = Table.api.selectedids(table);
  371. var data = ids.toString();
  372. location.href = options.extend.reports_url+'?id='+data;
  373. });
  374. // 批量审核按钮事件
  375. toolbar.on('click', Table.config.cousebtn, function () {
  376. var ids = Table.api.selectedids(table);
  377. var data = ids.toString();
  378. var url = options.extend.couse_url+'?ids='+data;
  379. Fast.api.open(url,'批量审核',{area:["40%","40%"]});
  380. });
  381. //清空回收站
  382. $(document).on('click', Table.config.destroyallbtn, function () {
  383. var that = this;
  384. Layer.confirm(__('Are you sure you want to truncate?'), function () {
  385. var url = $(that).data("url") ? $(that).data("url") : $(that).attr("href");
  386. Fast.api.ajax(url, function () {
  387. Layer.closeAll();
  388. table.trigger("uncheckbox");
  389. table.bootstrapTable('refresh');
  390. }, function () {
  391. Layer.closeAll();
  392. });
  393. });
  394. return false;
  395. });
  396. //全部还原
  397. $(document).on('click', Table.config.restoreallbtn, function () {
  398. var that = this;
  399. var url = $(that).data("url") ? $(that).data("url") : $(that).attr("href");
  400. Fast.api.ajax(url, function () {
  401. Layer.closeAll();
  402. table.trigger("uncheckbox");
  403. table.bootstrapTable('refresh');
  404. }, function () {
  405. Layer.closeAll();
  406. });
  407. return false;
  408. });
  409. //销毁或删除
  410. $(document).on('click', Table.config.restoreonebtn + ',' + Table.config.destroyonebtn, function () {
  411. var that = this;
  412. var url = $(that).data("url") ? $(that).data("url") : $(that).attr("href");
  413. var row = Fast.api.getrowbyindex(table, $(that).data("row-index"));
  414. Fast.api.ajax({
  415. url: url,
  416. data: {ids: row[options.pk]}
  417. }, function () {
  418. table.trigger("uncheckbox");
  419. table.bootstrapTable('refresh');
  420. });
  421. return false;
  422. });
  423. // 批量操作按钮事件
  424. toolbar.on('click', Table.config.multibtn, function () {
  425. var ids = Table.api.selectedids(table);
  426. Table.api.multi($(this).data("action"), ids, table, this);
  427. });
  428. // 批量删除按钮事件
  429. toolbar.on('click', Table.config.delbtn, function () {
  430. var that = this;
  431. var ids = Table.api.selectedids(table);
  432. Layer.confirm(
  433. __('Are you sure you want to delete the %s selected item?', ids.length),
  434. {icon: 3, title: __('Warning'), offset: 0, shadeClose: true, btn: [__('OK'), __('Cancel')]},
  435. function (index) {
  436. Table.api.multi("del", ids, table, that);
  437. Layer.close(index);
  438. }
  439. );
  440. });
  441. // 拖拽排序
  442. require(['dragsort'], function () {
  443. //绑定拖动排序
  444. $("tbody", table).dragsort({
  445. itemSelector: 'tr:visible',
  446. dragSelector: "a.btn-dragsort",
  447. dragEnd: function (a, b) {
  448. var element = $("a.btn-dragsort", this);
  449. var data = table.bootstrapTable('getData');
  450. var current = data[parseInt($(this).data("index"))];
  451. var options = table.bootstrapTable('getOptions');
  452. //改变的值和改变的ID集合
  453. var ids = $.map($("tbody tr:visible", table), function (tr) {
  454. return data[parseInt($(tr).data("index"))][options.pk];
  455. });
  456. var changeid = current[options.pk];
  457. var pid = typeof current.pid != 'undefined' ? current.pid : '';
  458. var params = {
  459. url: table.bootstrapTable('getOptions').extend.dragsort_url,
  460. data: {
  461. ids: ids.join(','),
  462. changeid: changeid,
  463. pid: pid,
  464. field: Table.config.dragsortfield,
  465. orderway: options.sortOrder,
  466. table: options.extend.table,
  467. pk: options.pk
  468. }
  469. };
  470. Fast.api.ajax(params, function (data, ret) {
  471. var success = $(element).data("success") || $.noop;
  472. if (typeof success === 'function') {
  473. if (false === success.call(element, data, ret)) {
  474. return false;
  475. }
  476. }
  477. table.bootstrapTable('refresh');
  478. }, function (data, ret) {
  479. var error = $(element).data("error") || $.noop;
  480. if (typeof error === 'function') {
  481. if (false === error.call(element, data, ret)) {
  482. return false;
  483. }
  484. }
  485. table.bootstrapTable('refresh');
  486. });
  487. },
  488. placeHolderTemplate: ""
  489. });
  490. });
  491. table.on("click", "input[data-id][name='checkbox']", function (e) {
  492. var ids = $(this).data("id");
  493. table.bootstrapTable($(this).prop("checked") ? 'checkBy' : 'uncheckBy', {field: options.pk, values: [ids]});
  494. });
  495. table.on("click", "[data-id].btn-change", function (e) {
  496. e.preventDefault();
  497. var changer = $.proxy(function () {
  498. Table.api.multi($(this).data("action") ? $(this).data("action") : '', [$(this).data("id")], table, this);
  499. }, this);
  500. if (typeof $(this).data("confirm") !== 'undefined') {
  501. Layer.confirm($(this).data("confirm"), function (index) {
  502. changer();
  503. Layer.close(index);
  504. });
  505. } else {
  506. changer();
  507. }
  508. });
  509. table.on("click", "[data-id].btn-edit", function (e) {
  510. e.preventDefault();
  511. var ids = $(this).data("id");
  512. var row = Table.api.getrowbyid(table, ids);
  513. row.ids = ids;
  514. var url = Table.api.replaceurl(options.extend.edit_url, row, table);
  515. Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('Edit'), $(this).data() || {});
  516. });
  517. table.on("click", "[data-id].btn-del", function (e) {
  518. e.preventDefault();
  519. var id = $(this).data("id");
  520. var that = this;
  521. Layer.confirm(
  522. __('Are you sure you want to delete this item?'),
  523. {icon: 3, title: __('Warning'), shadeClose: true, btn: [__('OK'), __('Cancel')]},
  524. function (index) {
  525. Table.api.multi("del", id, table, that);
  526. Layer.close(index);
  527. }
  528. );
  529. });
  530. //修复dropdown定位溢出的情况
  531. if (options.fixDropdownPosition) {
  532. var tableBody = table.closest(".fixed-table-body");
  533. table.on('show.bs.dropdown fa.event.refreshdropdown', ".btn-group", function (e) {
  534. var dropdownMenu = $(".dropdown-menu", this);
  535. var btnGroup = $(this);
  536. var isPullRight = dropdownMenu.hasClass("pull-right") || dropdownMenu.hasClass("dropdown-menu-right");
  537. var left, top, position;
  538. if (true || dropdownMenu.outerHeight() + btnGroup.outerHeight() > tableBody.outerHeight() - 41) {
  539. position = 'fixed';
  540. top = btnGroup.offset().top - $(window).scrollTop() + btnGroup.outerHeight();
  541. if ((top + dropdownMenu.outerHeight()) > $(window).height()) {
  542. top = btnGroup.offset().top - dropdownMenu.outerHeight() - 5;
  543. }
  544. left = isPullRight ? btnGroup.offset().left + btnGroup.outerWidth() - dropdownMenu.outerWidth() : btnGroup.offset().left;
  545. }
  546. if (left || top) {
  547. dropdownMenu.css({
  548. position: position, left: left, top: top, right: 'inherit'
  549. });
  550. }
  551. });
  552. var checkdropdown = function () {
  553. if ($(".btn-group.open", table).length > 0 && $(".btn-group.open .dropdown-menu", table).css("position") == 'fixed') {
  554. $(".btn-group.open", table).trigger("fa.event.refreshdropdown");
  555. }
  556. };
  557. $(window).on("scroll", function () {
  558. checkdropdown();
  559. });
  560. tableBody.on("scroll", function () {
  561. checkdropdown();
  562. });
  563. }
  564. var id = table.attr("id");
  565. Table.list[id] = table;
  566. return table;
  567. },
  568. // 批量操作请求
  569. multi: function (action, ids, table, element) {
  570. var options = table.bootstrapTable('getOptions');
  571. var data = element ? $(element).data() : {};
  572. ids = ($.isArray(ids) ? ids.join(",") : ids);
  573. var url = typeof data.url !== "undefined" ? data.url : (action == "del" ? options.extend.del_url : options.extend.multi_url);
  574. var params = typeof data.params !== "undefined" ? (typeof data.params == 'object' ? $.param(data.params) : data.params) : '';
  575. options = {url: url, data: {action: action, ids: ids, params: params}};
  576. Fast.api.ajax(options, function (data, ret) {
  577. table.trigger("uncheckbox");
  578. var success = $(element).data("success") || $.noop;
  579. if (typeof success === 'function') {
  580. if (false === success.call(element, data, ret)) {
  581. return false;
  582. }
  583. }
  584. table.bootstrapTable('refresh');
  585. }, function (data, ret) {
  586. var error = $(element).data("error") || $.noop;
  587. if (typeof error === 'function') {
  588. if (false === error.call(element, data, ret)) {
  589. return false;
  590. }
  591. }
  592. });
  593. },
  594. // 单元格元素事件
  595. events: {
  596. operate: {
  597. 'click .btn-editone': function (e, value, row, index) {
  598. e.stopPropagation();
  599. e.preventDefault();
  600. var table = $(this).closest('table');
  601. var options = table.bootstrapTable('getOptions');
  602. var ids = row[options.pk];
  603. row = $.extend({}, row ? row : {}, {ids: ids});
  604. var url = options.extend.edit_url;
  605. Fast.api.open(Table.api.replaceurl(url, row, table), $(this).data("original-title") || $(this).attr("title") || __('Edit'), $(this).data() || {});
  606. },
  607. 'click .btn-delone': function (e, value, row, index) {
  608. e.stopPropagation();
  609. e.preventDefault();
  610. var that = this;
  611. var top = $(that).offset().top - $(window).scrollTop();
  612. var left = $(that).offset().left - $(window).scrollLeft() - 260;
  613. if (top + 154 > $(window).height()) {
  614. top = top - 154;
  615. }
  616. if ($(window).width() < 480) {
  617. top = left = undefined;
  618. }
  619. Layer.confirm(
  620. __('Are you sure you want to delete this item?'),
  621. {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true, btn: [__('OK'), __('Cancel')]},
  622. function (index) {
  623. var table = $(that).closest('table');
  624. var options = table.bootstrapTable('getOptions');
  625. Table.api.multi("del", row[options.pk], table, that);
  626. Layer.close(index);
  627. }
  628. );
  629. }
  630. },//单元格图片预览
  631. image: {
  632. 'click .img-center': function (e, value, row, index) {
  633. var data = [];
  634. value = value === null ? '' : value.toString();
  635. var arr = value != '' ? value.split(",") : [];
  636. var url;
  637. $.each(arr, function (index, value) {
  638. url = Fast.api.cdnurl(value);
  639. data.push({
  640. src: url,
  641. thumb: url.match(/^(\/|data:image\\)/) ? url : url + Config.upload.thumbstyle
  642. });
  643. });
  644. Layer.photos({
  645. photos: {
  646. "start": $(this).parent().index(),
  647. "data": data
  648. },
  649. anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机(请注意,3.0之前的版本用shift参数)
  650. });
  651. },
  652. }
  653. },
  654. // 单元格数据格式化
  655. formatter: {
  656. icon: function (value, row, index) {
  657. value = value === null ? '' : value.toString();
  658. value = value.indexOf(" ") > -1 ? value : "fa fa-" + value;
  659. //渲染fontawesome图标
  660. return '<i class="' + value + '"></i> ' + value;
  661. },
  662. image: function (value, row, index) {
  663. value = value == null || value.length === 0 ? '' : value.toString();
  664. value = value ? value : '/assets/img/blank.gif';
  665. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  666. var url = Fast.api.cdnurl(value, true);
  667. url = url.match(/^(\/|data:image\\)/) ? url : url + Config.upload.thumbstyle;
  668. return '<a href="javascript:"><img class="' + classname + '" src="' + url + '" /></a>';
  669. },
  670. images: function (value, row, index) {
  671. value = value == null || value.length === 0 ? '' : value.toString();
  672. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  673. var arr = value != '' ? value.split(',') : [];
  674. var html = [];
  675. var url;
  676. $.each(arr, function (i, value) {
  677. value = value ? value : '/assets/img/blank.gif';
  678. url = Fast.api.cdnurl(value, true);
  679. url = url.match(/^(\/|data:image\\)/) ? url : url + Config.upload.thumbstyle;
  680. html.push('<a href="javascript:"><img class="' + classname + '" src="' + url + '" /></a>');
  681. });
  682. return html.join(' ');
  683. },
  684. file: function (value, row, index) {
  685. value = value == null || value.length === 0 ? '' : value.toString();
  686. value = Fast.api.cdnurl(value, true);
  687. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  688. var suffix = /[\.]?([a-zA-Z0-9]+)$/.exec(value);
  689. suffix = suffix ? suffix[1] : 'file';
  690. var url = Fast.api.fixurl("ajax/icon?suffix=" + suffix);
  691. return '<a href="' + value + '" target="_blank"><img src="' + url + '" class="' + classname + '"></a>';
  692. },
  693. files: function (value, row, index) {
  694. value = value == null || value.length === 0 ? '' : value.toString();
  695. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  696. var arr = value != '' ? value.split(',') : [];
  697. var html = [];
  698. var suffix, url;
  699. $.each(arr, function (i, value) {
  700. value = Fast.api.cdnurl(value, true);
  701. suffix = /[\.]?([a-zA-Z0-9]+)$/.exec(value);
  702. suffix = suffix ? suffix[1] : 'file';
  703. url = Fast.api.fixurl("ajax/icon?suffix=" + suffix);
  704. html.push('<a href="' + value + '" target="_blank"><img src="' + url + '" class="' + classname + '"></a>');
  705. });
  706. return html.join(' ');
  707. },
  708. content: function (value, row, index) {
  709. var width = this.width != undefined ? (this.width.match(/^\d+$/) ? this.width + "px" : this.width) : "250px";
  710. return "<div style='white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:" + width + ";'>" + value + "</div>";
  711. },
  712. status: function (value, row, index) {
  713. var custom = {normal: 'success', hidden: 'gray', deleted: 'danger', locked: 'info'};
  714. if (typeof this.custom !== 'undefined') {
  715. custom = $.extend(custom, this.custom);
  716. }
  717. this.custom = custom;
  718. this.icon = 'fa fa-circle';
  719. return Table.api.formatter.normal.call(this, value, row, index);
  720. },
  721. normal: function (value, row, index) {
  722. var colorArr = ["primary", "success", "danger", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
  723. var custom = {};
  724. if (typeof this.custom !== 'undefined') {
  725. custom = $.extend(custom, this.custom);
  726. }
  727. value = value == null || value.length === 0 ? '' : value.toString();
  728. var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
  729. var index = keys.indexOf(value);
  730. var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
  731. var display = index > -1 ? this.searchList[value] : null;
  732. var icon = typeof this.icon !== 'undefined' ? this.icon : null;
  733. if (!color) {
  734. color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
  735. }
  736. if (!display) {
  737. display = __(value.charAt(0).toUpperCase() + value.slice(1));
  738. }
  739. var html = '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
  740. if (this.operate != false) {
  741. html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
  742. }
  743. return html;
  744. },
  745. toggle: function (value, row, index) {
  746. var table = this.table;
  747. var options = table ? table.bootstrapTable('getOptions') : {};
  748. var pk = options.pk || "id";
  749. var color = typeof this.color !== 'undefined' ? this.color : 'success';
  750. var yes = typeof this.yes !== 'undefined' ? this.yes : 1;
  751. var no = typeof this.no !== 'undefined' ? this.no : 0;
  752. var url = typeof this.url !== 'undefined' ? this.url : '';
  753. var confirm = '';
  754. var disable = false;
  755. if (typeof this.confirm !== "undefined") {
  756. confirm = typeof this.confirm === "function" ? this.confirm.call(this, value, row, index) : this.confirm;
  757. }
  758. if (typeof this.disable !== "undefined") {
  759. disable = typeof this.disable === "function" ? this.disable.call(this, value, row, index) : this.disable;
  760. }
  761. return "<a href='javascript:;' data-toggle='tooltip' title='" + __('Click to toggle') + "' class='btn-change " + (disable ? 'btn disabled no-padding' : '') + "' data-index='" + index + "' data-id='"
  762. + row[pk] + "' " + (url ? "data-url='" + url + "'" : "") + (confirm ? "data-confirm='" + confirm + "'" : "") + " data-params='" + this.field + "=" + (value == yes ? no : yes) + "'><i class='fa fa-toggle-on text-success text-" + color + " " + (value == yes ? '' : 'fa-flip-horizontal text-gray') + " fa-2x'></i></a>";
  763. },
  764. url: function (value, row, index) {
  765. value = value == null || value.length === 0 ? '' : value.toString();
  766. return '<div class="input-group input-group-sm" style="width:250px;margin:0 auto;"><input type="text" class="form-control input-sm" value="' + value + '"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
  767. },
  768. search: function (value, row, index) {
  769. var field = this.field;
  770. if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
  771. value = row[this.customField];
  772. field = this.customField;
  773. }
  774. return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', value) + '" data-field="' + field + '" data-value="' + value + '">' + value + '</a>';
  775. },
  776. addtabs: function (value, row, index) {
  777. var url = Table.api.replaceurl(this.url || '', row, this.table);
  778. var title = this.atitle ? this.atitle : __("Search %s", value);
  779. return '<a href="' + Fast.api.fixurl(url) + '" class="addtabsit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  780. },
  781. dialog: function (value, row, index) {
  782. var url = Table.api.replaceurl(this.url || '', row, this.table);
  783. var title = this.atitle ? this.atitle : __("View %s", value);
  784. return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  785. },
  786. flag: function (value, row, index) {
  787. var that = this;
  788. value = value == null || value.length === 0 ? '' : value.toString();
  789. var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'};
  790. //如果字段列有定义custom
  791. if (typeof this.custom !== 'undefined') {
  792. colorArr = $.extend(colorArr, this.custom);
  793. }
  794. var field = this.field;
  795. if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
  796. value = row[this.customField];
  797. field = this.customField;
  798. }
  799. if (typeof that.searchList === 'object' && typeof that.custom === 'undefined') {
  800. var i = 0;
  801. var searchValues = Object.values(colorArr);
  802. $.each(that.searchList, function (key, val) {
  803. if (typeof colorArr[key] == 'undefined') {
  804. colorArr[key] = searchValues[i];
  805. i = typeof searchValues[i + 1] === 'undefined' ? 0 : i + 1;
  806. }
  807. });
  808. }
  809. //渲染Flag
  810. var html = [];
  811. var arr = value != '' ? value.split(',') : [];
  812. var color, display, label;
  813. $.each(arr, function (i, value) {
  814. value = value == null || value.length === 0 ? '' : value.toString();
  815. if (value == '')
  816. return true;
  817. color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
  818. display = typeof that.searchList !== 'undefined' && typeof that.searchList[value] !== 'undefined' ? that.searchList[value] : __(value.charAt(0).toUpperCase() + value.slice(1));
  819. label = '<span class="label label-' + color + '">' + display + '</span>';
  820. if (that.operate) {
  821. html.push('<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + field + '" data-value="' + value + '">' + label + '</a>');
  822. } else {
  823. html.push(label);
  824. }
  825. });
  826. return html.join(' ');
  827. },
  828. label: function (value, row, index) {
  829. return Table.api.formatter.flag.call(this, value, row, index);
  830. },
  831. datetime: function (value, row, index) {
  832. var datetimeFormat = typeof this.datetimeFormat === 'undefined' ? 'YYYY-MM-DD HH:mm:ss' : this.datetimeFormat;
  833. if (isNaN(value)) {
  834. return value ? Moment(value).format(datetimeFormat) : __('None');
  835. } else {
  836. return value ? Moment(parseInt(value) * 1000).format(datetimeFormat) : __('None');
  837. }
  838. },
  839. operate: function (value, row, index) {
  840. var table = this.table;
  841. // 操作配置
  842. var options = table ? table.bootstrapTable('getOptions') : {};
  843. // 默认按钮组
  844. var buttons = $.extend([], this.buttons || []);
  845. // 所有按钮名称
  846. var names = [];
  847. buttons.forEach(function (item) {
  848. names.push(item.name);
  849. });
  850. if (options.extend.dragsort_url !== '' && names.indexOf('dragsort') === -1) {
  851. buttons.push(Table.button.dragsort);
  852. }
  853. if (options.extend.edit_url !== '' && names.indexOf('edit') === -1) {
  854. Table.button.edit.url = options.extend.edit_url;
  855. buttons.push(Table.button.edit);
  856. }
  857. if (options.extend.del_url !== '' && names.indexOf('del') === -1) {
  858. buttons.push(Table.button.del);
  859. }
  860. return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
  861. }
  862. ,
  863. buttons: function (value, row, index) {
  864. // 默认按钮组
  865. var buttons = $.extend([], this.buttons || []);
  866. return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');
  867. }
  868. },
  869. buttonlink: function (column, buttons, value, row, index, type) {
  870. var table = column.table;
  871. column.clickToSelect = false;
  872. type = typeof type === 'undefined' ? 'buttons' : type;
  873. var options = table ? table.bootstrapTable('getOptions') : {};
  874. var html = [];
  875. var hidden, visible, disable, url, classname, icon, text, title, refresh, confirm, extend,
  876. dropdown, link;
  877. var fieldIndex = column.fieldIndex;
  878. var dropdowns = {};
  879. $.each(buttons, function (i, j) {
  880. if (type === 'operate') {
  881. if (j.name === 'dragsort' && typeof row[Table.config.dragsortfield] === 'undefined') {
  882. return true;
  883. }
  884. if (['add', 'edit', 'del', 'multi', 'dragsort'].indexOf(j.name) > -1 && !options.extend[j.name + "_url"]) {
  885. return true;
  886. }
  887. }
  888. var attr = table.data(type + "-" + j.name);
  889. if (typeof attr === 'undefined' || attr) {
  890. hidden = typeof j.hidden === 'function' ? j.hidden.call(table, row, j) : (typeof j.hidden !== 'undefined' ? j.hidden : false);
  891. if (hidden) {
  892. return true;
  893. }
  894. visible = typeof j.visible === 'function' ? j.visible.call(table, row, j) : (typeof j.visible !== 'undefined' ? j.visible : true);
  895. if (!visible) {
  896. return true;
  897. }
  898. dropdown = j.dropdown ? j.dropdown : '';
  899. url = j.url ? j.url : '';
  900. url = typeof url === 'function' ? url.call(table, row, j) : (url ? Fast.api.fixurl(Table.api.replaceurl(url, row, table)) : 'javascript:;');
  901. classname = j.classname ? j.classname : (dropdown ? 'btn-' + name + 'one' : 'btn-primary btn-' + name + 'one');
  902. icon = j.icon ? j.icon : '';
  903. text = typeof j.text === 'function' ? j.text.call(table, row, j) : j.text ? j.text : '';
  904. title = typeof j.title === 'function' ? j.title.call(table, row, j) : j.title ? j.title : text;
  905. refresh = j.refresh ? 'data-refresh="' + j.refresh + '"' : '';
  906. confirm = typeof j.confirm === 'function' ? j.confirm.call(table, row, j) : (typeof j.confirm !== 'undefined' ? j.confirm : false);
  907. confirm = confirm ? 'data-confirm="' + confirm + '"' : '';
  908. extend = j.extend ? j.extend : '';
  909. disable = typeof j.disable === 'function' ? j.disable.call(table, row, j) : (typeof j.disable !== 'undefined' ? j.disable : false);
  910. if (disable) {
  911. classname = classname + ' disabled';
  912. }
  913. link = '<a href="' + url + '" class="' + classname + '" ' + (confirm ? confirm + ' ' : '') + (refresh ? refresh + ' ' : '') + extend + ' title="' + title + '" data-table-id="' + (table ? table.attr("id") : '') + '" data-field-index="' + fieldIndex + '" data-row-index="' + index + '" data-button-index="' + i + '"><i class="' + icon + '"></i>' + (text ? ' ' + text : '') + '</a>';
  914. if (dropdown) {
  915. if (typeof dropdowns[dropdown] == 'undefined') {
  916. dropdowns[dropdown] = [];
  917. }
  918. dropdowns[dropdown].push(link);
  919. } else {
  920. html.push(link);
  921. }
  922. }
  923. });
  924. if (!$.isEmptyObject(dropdowns)) {
  925. var dropdownHtml = [];
  926. $.each(dropdowns, function (i, j) {
  927. dropdownHtml.push('<div class="btn-group"><button type="button" class="btn btn-primary dropdown-toggle btn-xs" data-toggle="dropdown">' + i + '</button><button type="button" class="btn btn-primary dropdown-toggle btn-xs" data-toggle="dropdown"><span class="caret"></span></button><ul class="dropdown-menu dropdown-menu-right"><li>' + j.join('</li><li>') + '</li></ul></div>');
  928. });
  929. html.unshift(dropdownHtml);
  930. }
  931. return html.join(' ');
  932. },
  933. //替换URL中的数据
  934. replaceurl: function (url, row, table) {
  935. var options = table ? table.bootstrapTable('getOptions') : null;
  936. var ids = options ? row[options.pk] : 0;
  937. row.ids = ids ? ids : (typeof row.ids !== 'undefined' ? row.ids : 0);
  938. url = url == null || url.length === 0 ? '' : url.toString();
  939. //自动添加ids参数
  940. url = !url.match(/\{ids\}/i) ? url + (url.match(/(\?|&)+/) ? "&ids=" : "/ids/") + '{ids}' : url;
  941. url = url.replace(/\{(.*?)\}/gi, function (matched) {
  942. matched = matched.substring(1, matched.length - 1);
  943. if (matched.indexOf(".") !== -1) {
  944. var temp = row;
  945. var arr = matched.split(/\./);
  946. for (var i = 0; i < arr.length; i++) {
  947. if (typeof temp[arr[i]] !== 'undefined') {
  948. temp = temp[arr[i]];
  949. }
  950. }
  951. return typeof temp === 'object' ? '' : temp;
  952. }
  953. return row[matched];
  954. });
  955. return url;
  956. },
  957. // 获取选中的条目ID集合
  958. selectedids: function (table, current) {
  959. var options = table.bootstrapTable('getOptions');
  960. //如果有设置翻页记忆模式
  961. if (!current && options.maintainSelected) {
  962. return options.selectedIds;
  963. }
  964. return $.map(table.bootstrapTable('getSelections'), function (row) {
  965. return row[options.pk];
  966. });
  967. },
  968. //获取选中的数据
  969. selecteddata: function (table, current) {
  970. var options = table.bootstrapTable('getOptions');
  971. //如果有设置翻页记忆模式
  972. if (!current && options.maintainSelected) {
  973. return options.selectedData;
  974. }
  975. return table.bootstrapTable('getSelections');
  976. },
  977. // 切换复选框状态
  978. toggleattr: function (table) {
  979. $("input[type='checkbox']", table).trigger('click');
  980. },
  981. // 根据行索引获取行数据
  982. getrowdata: function (table, index) {
  983. index = parseInt(index);
  984. var data = table.bootstrapTable('getData');
  985. return typeof data[index] !== 'undefined' ? data[index] : null;
  986. },
  987. // 根据行索引获取行数据
  988. getrowbyindex: function (table, index) {
  989. return Table.api.getrowdata(table, index);
  990. },
  991. // 根据主键ID获取行数据
  992. getrowbyid: function (table, id) {
  993. var row = {};
  994. var options = table.bootstrapTable("getOptions");
  995. $.each(Table.api.selecteddata(table), function (i, j) {
  996. if (j[options.pk] == id) {
  997. row = j;
  998. return false;
  999. }
  1000. });
  1001. return row;
  1002. }
  1003. },
  1004. };
  1005. return Table;
  1006. });