require-table.js 47 KB

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