require-table.js 51 KB

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