require-table.js 55 KB

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