require-table.js 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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 ids = Table.api.selectedids(table);
  386. Table.api.multi($(this).data("action"), ids, table, this);
  387. });
  388. // 批量删除按钮事件
  389. toolbar.on('click', Table.config.delbtn, function () {
  390. var that = this;
  391. var ids = Table.api.selectedids(table);
  392. Layer.confirm(
  393. __('Are you sure you want to delete the %s selected item?', ids.length),
  394. {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
  395. function (index) {
  396. Table.api.multi("del", ids, table, that);
  397. Layer.close(index);
  398. }
  399. );
  400. });
  401. // 拖拽排序
  402. require(['dragsort'], function () {
  403. //绑定拖动排序
  404. $("tbody", table).dragsort({
  405. itemSelector: 'tr:visible',
  406. dragSelector: "a.btn-dragsort",
  407. dragEnd: function (a, b) {
  408. var element = $("a.btn-dragsort", this);
  409. var data = table.bootstrapTable('getData');
  410. var current = data[parseInt($(this).data("index"))];
  411. var options = table.bootstrapTable('getOptions');
  412. //改变的值和改变的ID集合
  413. var ids = $.map($("tbody tr:visible", table), function (tr) {
  414. return data[parseInt($(tr).data("index"))][options.pk];
  415. });
  416. var changeid = current[options.pk];
  417. var pid = typeof current.pid != 'undefined' ? current.pid : '';
  418. var params = {
  419. url: table.bootstrapTable('getOptions').extend.dragsort_url,
  420. data: {
  421. ids: ids.join(','),
  422. changeid: changeid,
  423. pid: pid,
  424. field: Table.config.dragsortfield,
  425. orderway: options.sortOrder,
  426. table: options.extend.table,
  427. pk: options.pk
  428. }
  429. };
  430. Fast.api.ajax(params, function (data, ret) {
  431. var success = $(element).data("success") || $.noop;
  432. if (typeof success === 'function') {
  433. if (false === success.call(element, data, ret)) {
  434. return false;
  435. }
  436. }
  437. table.bootstrapTable('refresh');
  438. }, function (data, ret) {
  439. var error = $(element).data("error") || $.noop;
  440. if (typeof error === 'function') {
  441. if (false === error.call(element, data, ret)) {
  442. return false;
  443. }
  444. }
  445. table.bootstrapTable('refresh');
  446. });
  447. },
  448. placeHolderTemplate: ""
  449. });
  450. });
  451. table.on("click", "input[data-id][name='checkbox']", function (e) {
  452. var ids = $(this).data("id");
  453. table.bootstrapTable($(this).prop("checked") ? 'checkBy' : 'uncheckBy', {field: options.pk, values: [ids]});
  454. });
  455. table.on("click", "[data-id].btn-change", function (e) {
  456. e.preventDefault();
  457. var changer = $.proxy(function () {
  458. Table.api.multi($(this).data("action") ? $(this).data("action") : '', [$(this).data("id")], table, this);
  459. }, this);
  460. if (typeof $(this).data("confirm") !== 'undefined') {
  461. Layer.confirm($(this).data("confirm"), function (index) {
  462. changer();
  463. Layer.close(index);
  464. });
  465. } else {
  466. changer();
  467. }
  468. });
  469. table.on("click", "[data-id].btn-edit", function (e) {
  470. e.preventDefault();
  471. var ids = $(this).data("id");
  472. var row = Table.api.getrowbyid(table, ids);
  473. row.ids = ids;
  474. var url = Table.api.replaceurl(options.extend.edit_url, row, table);
  475. Fast.api.open(url, __('Edit'), $(this).data() || {});
  476. });
  477. table.on("click", "[data-id].btn-del", function (e) {
  478. e.preventDefault();
  479. var id = $(this).data("id");
  480. var that = this;
  481. Layer.confirm(
  482. __('Are you sure you want to delete this item?'),
  483. {icon: 3, title: __('Warning'), shadeClose: true},
  484. function (index) {
  485. Table.api.multi("del", id, table, that);
  486. Layer.close(index);
  487. }
  488. );
  489. });
  490. var id = table.attr("id");
  491. Table.list[id] = table;
  492. return table;
  493. },
  494. // 批量操作请求
  495. multi: function (action, ids, table, element) {
  496. var options = table.bootstrapTable('getOptions');
  497. var data = element ? $(element).data() : {};
  498. ids = ($.isArray(ids) ? ids.join(",") : ids);
  499. var url = typeof data.url !== "undefined" ? data.url : (action == "del" ? options.extend.del_url : options.extend.multi_url);
  500. var params = typeof data.params !== "undefined" ? (typeof data.params == 'object' ? $.param(data.params) : data.params) : '';
  501. options = {url: url, data: {action: action, ids: ids, params: params}};
  502. Fast.api.ajax(options, function (data, ret) {
  503. table.trigger("uncheckbox");
  504. var success = $(element).data("success") || $.noop;
  505. if (typeof success === 'function') {
  506. if (false === success.call(element, data, ret)) {
  507. return false;
  508. }
  509. }
  510. table.bootstrapTable('refresh');
  511. }, function (data, ret) {
  512. var error = $(element).data("error") || $.noop;
  513. if (typeof error === 'function') {
  514. if (false === error.call(element, data, ret)) {
  515. return false;
  516. }
  517. }
  518. });
  519. },
  520. // 单元格元素事件
  521. events: {
  522. operate: {
  523. 'click .btn-editone': function (e, value, row, index) {
  524. e.stopPropagation();
  525. e.preventDefault();
  526. var table = $(this).closest('table');
  527. var options = table.bootstrapTable('getOptions');
  528. var ids = row[options.pk];
  529. row = $.extend({}, row ? row : {}, {ids: ids});
  530. var url = options.extend.edit_url;
  531. Fast.api.open(Table.api.replaceurl(url, row, table), __('Edit'), $(this).data() || {});
  532. },
  533. 'click .btn-delone': function (e, value, row, index) {
  534. e.stopPropagation();
  535. e.preventDefault();
  536. var that = this;
  537. var top = $(that).offset().top - $(window).scrollTop();
  538. var left = $(that).offset().left - $(window).scrollLeft() - 260;
  539. if (top + 154 > $(window).height()) {
  540. top = top - 154;
  541. }
  542. if ($(window).width() < 480) {
  543. top = left = undefined;
  544. }
  545. Layer.confirm(
  546. __('Are you sure you want to delete this item?'),
  547. {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true},
  548. function (index) {
  549. var table = $(that).closest('table');
  550. var options = table.bootstrapTable('getOptions');
  551. Table.api.multi("del", row[options.pk], table, that);
  552. Layer.close(index);
  553. }
  554. );
  555. }
  556. },//单元格图片预览
  557. image: {
  558. 'click .img-center': function (e, value, row, index) {
  559. var data = [];
  560. value = value.toString().split(",");
  561. $.each(value, function (index, value) {
  562. data.push({
  563. src: Fast.api.cdnurl(value),
  564. });
  565. });
  566. Layer.photos({
  567. photos: {
  568. "start": $(this).parent().index(),
  569. "data": data
  570. },
  571. anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机(请注意,3.0之前的版本用shift参数)
  572. });
  573. },
  574. }
  575. },
  576. // 单元格数据格式化
  577. formatter: {
  578. icon: function (value, row, index) {
  579. if (!value)
  580. return '';
  581. value = value === null ? '' : value.toString();
  582. value = value.indexOf(" ") > -1 ? value : "fa fa-" + value;
  583. //渲染fontawesome图标
  584. return '<i class="' + value + '"></i> ' + value;
  585. },
  586. image: function (value, row, index) {
  587. value = value ? value : '/assets/img/blank.gif';
  588. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  589. return '<a href="javascript:"><img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" /></a>';
  590. },
  591. images: function (value, row, index) {
  592. value = value === null ? '' : value.toString();
  593. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  594. var arr = value.split(',');
  595. var html = [];
  596. $.each(arr, function (i, value) {
  597. value = value ? value : '/assets/img/blank.gif';
  598. html.push('<a href="javascript:"><img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" /></a>');
  599. });
  600. return html.join(' ');
  601. },
  602. content: function (value, row, index) {
  603. var width = this.width != undefined ? (this.width.match(/^\d+$/) ? this.width + "px" : this.width) : "250px";
  604. return "<div style='white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:" + width + ";'>" + value + "</div>";
  605. },
  606. status: function (value, row, index) {
  607. var custom = {normal: 'success', hidden: 'gray', deleted: 'danger', locked: 'info'};
  608. if (typeof this.custom !== 'undefined') {
  609. custom = $.extend(custom, this.custom);
  610. }
  611. this.custom = custom;
  612. this.icon = 'fa fa-circle';
  613. return Table.api.formatter.normal.call(this, value, row, index);
  614. },
  615. normal: function (value, row, index) {
  616. var colorArr = ["primary", "success", "danger", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
  617. var custom = {};
  618. if (typeof this.custom !== 'undefined') {
  619. custom = $.extend(custom, this.custom);
  620. }
  621. value = value === null ? '' : value.toString();
  622. var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
  623. var index = keys.indexOf(value);
  624. var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
  625. var display = index > -1 ? this.searchList[value] : null;
  626. var icon = typeof this.icon !== 'undefined' ? this.icon : null;
  627. if (!color) {
  628. color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
  629. }
  630. if (!display) {
  631. display = __(value.charAt(0).toUpperCase() + value.slice(1));
  632. }
  633. var html = '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
  634. if (this.operate != false) {
  635. html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
  636. }
  637. return html;
  638. },
  639. toggle: function (value, row, index) {
  640. var table = this.table;
  641. var options = table ? table.bootstrapTable('getOptions') : {};
  642. var pk = options.pk || "id";
  643. var color = typeof this.color !== 'undefined' ? this.color : 'success';
  644. var yes = typeof this.yes !== 'undefined' ? this.yes : 1;
  645. var no = typeof this.no !== 'undefined' ? this.no : 0;
  646. var url = typeof this.url !== 'undefined' ? this.url : '';
  647. var confirm = '';
  648. var disable = false;
  649. if (typeof this.confirm !== "undefined") {
  650. confirm = typeof this.confirm === "function" ? this.confirm.call(this, value, row, index) : this.confirm;
  651. }
  652. if (typeof this.disable !== "undefined") {
  653. disable = typeof this.disable === "function" ? this.disable.call(this, value, row, index) : this.disable;
  654. }
  655. return "<a href='javascript:;' data-toggle='tooltip' title='" + __('Click to toggle') + "' class='btn-change " + (disable ? 'btn disabled no-padding' : '') + "' data-index='" + index + "' data-id='"
  656. + 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>";
  657. },
  658. url: function (value, row, index) {
  659. value = value === null ? '' : value.toString();
  660. 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>';
  661. },
  662. search: function (value, row, index) {
  663. var field = this.field;
  664. if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
  665. value = row[this.customField];
  666. field = this.customField;
  667. }
  668. return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', value) + '" data-field="' + field + '" data-value="' + value + '">' + value + '</a>';
  669. },
  670. addtabs: function (value, row, index) {
  671. var url = Table.api.replaceurl(this.url, row, this.table);
  672. var title = this.atitle ? this.atitle : __("Search %s", value);
  673. return '<a href="' + Fast.api.fixurl(url) + '" class="addtabsit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  674. },
  675. dialog: function (value, row, index) {
  676. var url = Table.api.replaceurl(this.url, row, this.table);
  677. var title = this.atitle ? this.atitle : __("View %s", value);
  678. return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  679. },
  680. flag: function (value, row, index) {
  681. var that = this;
  682. value = value === null ? '' : value.toString();
  683. var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'};
  684. //如果字段列有定义custom
  685. if (typeof this.custom !== 'undefined') {
  686. colorArr = $.extend(colorArr, this.custom);
  687. }
  688. var field = this.field;
  689. if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
  690. value = row[this.customField];
  691. field = this.customField;
  692. }
  693. //渲染Flag
  694. var html = [];
  695. var arr = value.split(',');
  696. var color, display, label;
  697. $.each(arr, function (i, value) {
  698. value = value === null ? '' : value.toString();
  699. if (value == '')
  700. return true;
  701. color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
  702. display = typeof that.searchList !== 'undefined' && typeof that.searchList[value] !== 'undefined' ? that.searchList[value] : __(value.charAt(0).toUpperCase() + value.slice(1));
  703. label = '<span class="label label-' + color + '">' + display + '</span>';
  704. if (that.operate) {
  705. html.push('<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + field + '" data-value="' + value + '">' + label + '</a>');
  706. } else {
  707. html.push(label);
  708. }
  709. });
  710. return html.join(' ');
  711. },
  712. label: function (value, row, index) {
  713. return Table.api.formatter.flag.call(this, value, row, index);
  714. },
  715. datetime: function (value, row, index) {
  716. var datetimeFormat = typeof this.datetimeFormat === 'undefined' ? 'YYYY-MM-DD HH:mm:ss' : this.datetimeFormat;
  717. if (isNaN(value)) {
  718. return value ? Moment(value).format(datetimeFormat) : __('None');
  719. } else {
  720. return value ? Moment(parseInt(value) * 1000).format(datetimeFormat) : __('None');
  721. }
  722. },
  723. operate: function (value, row, index) {
  724. var table = this.table;
  725. // 操作配置
  726. var options = table ? table.bootstrapTable('getOptions') : {};
  727. // 默认按钮组
  728. var buttons = $.extend([], this.buttons || []);
  729. // 所有按钮名称
  730. var names = [];
  731. buttons.forEach(function (item) {
  732. names.push(item.name);
  733. });
  734. if (options.extend.dragsort_url !== '' && names.indexOf('dragsort') === -1) {
  735. buttons.push(Table.button.dragsort);
  736. }
  737. if (options.extend.edit_url !== '' && names.indexOf('edit') === -1) {
  738. Table.button.edit.url = options.extend.edit_url;
  739. buttons.push(Table.button.edit);
  740. }
  741. if (options.extend.del_url !== '' && names.indexOf('del') === -1) {
  742. buttons.push(Table.button.del);
  743. }
  744. return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
  745. }
  746. ,
  747. buttons: function (value, row, index) {
  748. // 默认按钮组
  749. var buttons = $.extend([], this.buttons || []);
  750. return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');
  751. }
  752. },
  753. buttonlink: function (column, buttons, value, row, index, type) {
  754. var table = column.table;
  755. type = typeof type === 'undefined' ? 'buttons' : type;
  756. var options = table ? table.bootstrapTable('getOptions') : {};
  757. var html = [];
  758. var hidden, visible, disable, url, classname, icon, text, title, refresh, confirm, extend,
  759. dropdown, link;
  760. var fieldIndex = column.fieldIndex;
  761. var dropdowns = {};
  762. $.each(buttons, function (i, j) {
  763. if (type === 'operate') {
  764. if (j.name === 'dragsort' && typeof row[Table.config.dragsortfield] === 'undefined') {
  765. return true;
  766. }
  767. if (['add', 'edit', 'del', 'multi', 'dragsort'].indexOf(j.name) > -1 && !options.extend[j.name + "_url"]) {
  768. return true;
  769. }
  770. }
  771. var attr = table.data(type + "-" + j.name);
  772. if (typeof attr === 'undefined' || attr) {
  773. hidden = typeof j.hidden === 'function' ? j.hidden.call(table, row, j) : (typeof j.hidden !== 'undefined' ? j.hidden : false);
  774. if (hidden) {
  775. return true;
  776. }
  777. visible = typeof j.visible === 'function' ? j.visible.call(table, row, j) : (typeof j.visible !== 'undefined' ? j.visible : true);
  778. if (!visible) {
  779. return true;
  780. }
  781. dropdown = j.dropdown ? j.dropdown : '';
  782. url = j.url ? j.url : '';
  783. url = typeof url === 'function' ? url.call(table, row, j) : (url ? Fast.api.fixurl(Table.api.replaceurl(url, row, table)) : 'javascript:;');
  784. classname = j.classname ? j.classname : 'btn-primary btn-' + name + 'one';
  785. icon = j.icon ? j.icon : '';
  786. text = typeof j.text === 'function' ? j.text.call(table, row, j) : j.text ? j.text : '';
  787. title = typeof j.title === 'function' ? j.title.call(table, row, j) : j.title ? j.title : text;
  788. refresh = j.refresh ? 'data-refresh="' + j.refresh + '"' : '';
  789. confirm = typeof j.confirm === 'function' ? j.confirm.call(table, row, j) : (typeof j.confirm !== 'undefined' ? j.confirm : false);
  790. confirm = confirm ? 'data-confirm="' + confirm + '"' : '';
  791. extend = j.extend ? j.extend : '';
  792. disable = typeof j.disable === 'function' ? j.disable.call(table, row, j) : (typeof j.disable !== 'undefined' ? j.disable : false);
  793. if (disable) {
  794. classname = classname + ' disabled';
  795. }
  796. 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>';
  797. if (dropdown) {
  798. if (typeof dropdowns[dropdown] == 'undefined') {
  799. dropdowns[dropdown] = [];
  800. }
  801. dropdowns[dropdown].push(link);
  802. } else {
  803. html.push(link);
  804. }
  805. }
  806. });
  807. if (!$.isEmptyObject(dropdowns)) {
  808. var dropdownHtml = [];
  809. $.each(dropdowns, function (i, j) {
  810. 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>');
  811. });
  812. html.unshift(dropdownHtml);
  813. }
  814. return html.join(' ');
  815. },
  816. //替换URL中的数据
  817. replaceurl: function (url, row, table) {
  818. var options = table ? table.bootstrapTable('getOptions') : null;
  819. var ids = options ? row[options.pk] : 0;
  820. row.ids = ids ? ids : (typeof row.ids !== 'undefined' ? row.ids : 0);
  821. //自动添加ids参数
  822. url = !url.match(/\{ids\}/i) ? url + (url.match(/(\?|&)+/) ? "&ids=" : "/ids/") + '{ids}' : url;
  823. url = url.replace(/\{(.*?)\}/gi, function (matched) {
  824. matched = matched.substring(1, matched.length - 1);
  825. if (matched.indexOf(".") !== -1) {
  826. var temp = row;
  827. var arr = matched.split(/\./);
  828. for (var i = 0; i < arr.length; i++) {
  829. if (typeof temp[arr[i]] !== 'undefined') {
  830. temp = temp[arr[i]];
  831. }
  832. }
  833. return typeof temp === 'object' ? '' : temp;
  834. }
  835. return row[matched];
  836. });
  837. return url;
  838. },
  839. // 获取选中的条目ID集合
  840. selectedids: function (table, current) {
  841. var options = table.bootstrapTable('getOptions');
  842. //如果有设置翻页记忆模式
  843. if (!current && options.maintainSelected) {
  844. return options.selectedIds;
  845. }
  846. return $.map(table.bootstrapTable('getSelections'), function (row) {
  847. return row[options.pk];
  848. });
  849. },
  850. //获取选中的数据
  851. selecteddata: function (table, current) {
  852. var options = table.bootstrapTable('getOptions');
  853. //如果有设置翻页记忆模式
  854. if (!current && options.maintainSelected) {
  855. return options.selectedData;
  856. }
  857. return table.bootstrapTable('getSelections');
  858. },
  859. // 切换复选框状态
  860. toggleattr: function (table) {
  861. $("input[type='checkbox']", table).trigger('click');
  862. },
  863. // 根据行索引获取行数据
  864. getrowdata: function (table, index) {
  865. index = parseInt(index);
  866. var data = table.bootstrapTable('getData');
  867. return typeof data[index] !== 'undefined' ? data[index] : null;
  868. },
  869. // 根据行索引获取行数据
  870. getrowbyindex: function (table, index) {
  871. return Table.api.getrowdata(table, index);
  872. },
  873. // 根据主键ID获取行数据
  874. getrowbyid: function (table, id) {
  875. var row = {};
  876. var options = table.bootstrapTable("getOptions");
  877. $.each(Table.api.selecteddata(table), function (i, j) {
  878. if (j[options.pk] == id) {
  879. row = j;
  880. return false;
  881. }
  882. });
  883. return row;
  884. }
  885. },
  886. };
  887. return Table;
  888. });