require-table.js 55 KB

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