require-table.js 46 KB

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