require-table.js 48 KB

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