require-table.js 47 KB

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