require-table.js 56 KB

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