require-table.js 46 KB

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